代码无法与报告链接 [英] Code fails to link with report
问题描述
我的代码无法链接到我的报告。我需要将参数T-ID传递给报告,但出于某种原因,当我点击按钮时我无法得到任何数据。
在Crystal Reports中,我可以输入参数后获取数据,但从程序运行时无法生成。我该如何解决这个问题?
My code fails to link to my report. I need to pass the parameter "T-ID" to the report but for some reason I cant get any data when I click the button.
In Crystal Reports I can get data after inputting the parameter, but it fails to generate when run from my program. How can I fix this?
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
protected void Button1_Click(object sender, EventArgs e)
{
ReportDocument test = new ReportDocument();
test.Load(Server.MapPath("On Job Training 1.rpt"));
test.SetParameterValue("T-ID", TextBox1.Text);
CrystalReportViewer1.ReportSource = test;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.ReportSource = test;
}
我的尝试:
我的代码无法链接到我的报告。我需要将参数T-ID传递给报告,但由于某种原因,当我点击按钮时我无法获得任何数据。
在Crystal Reports中我可以在输入参数后获取数据,但是从我的程序运行时无法生成。我该如何解决这个问题?
What I have tried:
My code fails to link to my report. I need to pass the parameter "T-ID" to the report but for some reason I can't get any data when I click the button.
In Crystal Reports I can get data after inputting the parameter, but it fails to generate when run from my program. How can I fix this?
推荐答案
我建议报告没有使用适当的凭据来连接数据库。
这需要要设置并传递给报告,否则它将通过当前上下文运行,该上下文取决于运行应用程序池的帐户。
以下代码创建一个Crystal ConnectionInfo&将其应用于报告
I would suggest that the report is not using appropriate credentials to connect to the DB.
This needs to be set and passed to the report else it will run via the current context which is dependent on the account the Application Pool is running under.
The below code creates a Crystal ConnectionInfo & applies it to the Report
CrystalDecisions.Shared.ConnectionInfo connCrystal = new CrystalDecisions.Shared.ConnectionInfo();
connCrystal.ServerName = "MyServername";
connCrystal.DatabaseName = "MyDbName";
connCrystal.IntegratedSecurity = false;
connCrystal.UserID = "MyUserName";
connCrystal.Password = "MyPassword";
ReportDocument rptDoc = new ReportDocument();
rptDoc.Load(Server.MapPath("Path.rpt")):
CrystalDecisions.CrystalReports.Engine.Tables tblsReport = rptDoc.Database.Tables;
for(int i = 0; i < tblsReport.Count; i++)
{
CrystalDecisions.CrystalReports.Engine.Table tblRpt = tblsRpt[i];
TableLogOnInfo infoTable = tblRpt.LogOnInfo;
infoTable.ConnectionInfo = connCrystal;
tblRpt.ApplyLogOnInfo(infoTable);
}
// if you have sub reports you need to loop through them and apply the same log on info
if(rptDoc.SubReports.Count > 0)
{
for(int i = 0; i < rptDoc.SubReports.Count; i++)
{
ReportDocument subReport = rptDoc.OpenSubReport(rptDoc.Subreports[i].Name);
CrystalDecisions.CrystalReports.Engine.Tables tblsSubReport = subReport.Database.Tables;
for(int ii = 0; ii < tblsSubReport.Count; ii++)
{
CrystalDecisions.CrystalReports.Engine.Table tblSubRpt = tblsSubReport[ii];
TableLogOnInfo infoSubTable = tblsSubReport.LogOnInfo;
infoSubTable.ConnectionInfo = connCrystal;
tblsSubReport.ApplyLogOnInfo(infoSubTable);
}
}
亲切的问候
Kind Regards
这篇关于代码无法与报告链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!