Click here to Skip to main content
15,915,800 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.NET
<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" />

C#
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;
}


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?
Posted
Updated 11-Aug-16 15:17pm

1 solution

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
C#
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900