但是,当我嵌入具有子报告的水晶报告时,凭据不会自动传递到子报告.当我调试解决方案时,初始报告打开正常,当我单击项目打开子报告时,水晶报告查看器会要求我提供数据库登录凭据.
如何在代码中自动传递这些凭据,以便用户在水晶报表查看器中查看时无需输入代码.
下面是我在default.aspx.cs页面中使用的代码.它包含连接字符串.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.sqlClient; namespace CFIBInventory { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { sqlConnection con = new sqlConnection("Data Source=HPL-WTS;Initial Catalog=Enterprise32;Persist Security Info=True;User ID=sa;Password=********"); DataSet1 ds = new DataSet1(); sqlDataAdapter adapter = new sqlDataAdapter("SELECT dbo.Material.MaterialCode,dbo.Material.CategoryCode,dbo.Material.Description,dbo.MaterialOnHand.LocationCode,dbo.Material.ValuationMethod,dbo.MaterialOnHand.Quantity FROM dbo.Material INNER JOIN dbo.MaterialOnHand ON dbo.Material.MaterialCode = dbo.MaterialOnHand.MaterialCode WHERE (dbo.Material.CategoryCode = 'CFIB3') AND (dbo.Material.ValuationMethod = 1) AND (dbo.Material.InactiveFlag = 0)",con); adapter.Fill(ds.cfibInventory); CrystalReport1 report = new CrystalReport1(); report.SetDataSource(ds); CrystalReportViewer1.ReportSource = report; CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None; } } }@H_502_9@<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CFIBInventory._Default" %> <%@ Register assembly="CrystalDecisions.Web,Version=13.0.2000.0,Culture=neutral,PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> <Meta http-equiv="X-UA-Compatible" content="IE=9"/> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <p> </p> <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" /> </asp:Content>@H_502_9@顺便说一句,数据库连接到Windows Server 2005计算机.我不相信它有公共访问设置,例如它自己的IIS.以前的报告没有子报告,安装在较新的2012 Server上,就像数据库所在的那样.不确定机器是否与crystal report viewer中的其他登录提示有关.
任何帮助都会很棒.先感谢您!
编辑:从Nimesh实施解决方案后
好吧,我添加了连接的连接类型,但是,visual studio强调“报告”说:在声明之前不能使用局部变量’report’.
foreach(report.Database.Tables中的CrystalDecisions.CrystalReports.Engine.Table CrTable)
和
foreach(report.Subreports中的ReportDocument子报告)
我注意到我在下面宣布报告为新的crystalreport1.所以我将该声明移到Nimesh代码块之上,并且’report’的红色下划线消失了,但随后所有6个’crtablelogoninfo’实例都以红色加下划线并出现错误:当前上下文中不存在名称’crtablelogoninfo’
任何进一步的帮助将不胜感激.
以下是我的代码现在的样子:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.sqlClient; using CrystalDecisions.Shared; using CrystalDecisions.CrystalReports.Engine; namespace CFIBInventory { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { //Nimesh code ConnectionInfo crConnectionInfo = new ConnectionInfo(); crConnectionInfo.ServerName = "HPL-WTS"; crConnectionInfo.DatabaseName = "Enterprise32"; crConnectionInfo.UserID = "sa"; crConnectionInfo.Password = "*********"; foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in report.Database.Tables) { crTablelogoninfo = CrTable.logonInfo; crtablelogoninfo.ConnectionInfo = crConnectionInfo; CrTable.ApplylogonInfo(crtablelogoninfo); } foreach (ReportDocument subreport in report.Subreports) { foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in subreport.Database.Tables) { crtablelogoninfo = CrTable.logonInfo; crtablelogoninfo.ConnectionInfo = crConnectionInfo; CrTable.ApplylogonInfo(crtablelogoninfo); } } // nimesh code end //Old connection string,i assume this shouldnt be here since nimesh code is for connecting //sqlConnection con = new sqlConnection("Data Source=HPL-WTS;Initial Catalog=Enterprise32;Persist Security Info=True;User ID=sa;Password=123qwerTy987"); DataSet1 ds = new DataSet1(); sqlDataAdapter adapter = new sqlDataAdapter("SELECT dbo.Material.MaterialCode,con); adapter.Fill(ds.cfibInventory); CrystalReport1 report = new CrystalReport1(); // OLD CODE //report.SetDataSource(ds); CrystalReportViewer1.ReportSource = report; CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None; } } }@H_502_9@
解决方法
report.SetDataSource(ds);@H_502_9@当您使用SetDataSource方法时,必须提供水晶报告文档中包含的所有表.在这里,您只在数据集中传递了一个表.您必须传递所有表,包括子报表.
我建议你使用子报表然后使用连接的数据源方法,如果断开数据源(report.SetDataSource()).在Connected数据源中,您必须在显示报告之前设置登录信息.private void PrintReport() { ReportDocument report = new ReportDocument(); report.Load("ReportPath"); ConnectionInfo crConnectionInfo = new ConnectionInfo(); crConnectionInfo.ServerName = "HPL-WTS"; crConnectionInfo.DatabaseName = "Enterprise32"; crConnectionInfo.UserID = "sa"; crConnectionInfo.Password = "*********"; TablelogonInfo crTablelogoninfo = new TablelogonInfo(); foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in report.Database.Tables) { crTablelogoninfo = CrTable.logonInfo; crTablelogoninfo.ConnectionInfo = crConnectionInfo; CrTable.ApplylogonInfo(crTablelogoninfo); } foreach (ReportDocument subreport in report.Subreports) { foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in subreport.Database.Tables) { crTablelogoninfo = CrTable.logonInfo; crTablelogoninfo.ConnectionInfo = crConnectionInfo; CrTable.ApplylogonInfo(crTablelogoninfo); } } CrystalReportViewer1.ReportSource = report; CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None; }@H_502_9@如果您想使用断开连接的数据源,即使您没有包含子报表,也可以使用单个命令而不是多个表.这样可以轻松实现更好的性能.因为你只需要将一个表传递给SetDataSource方法.但是,不要忘记在DataTable中设置tablename,否则将不会显示报告.