Search code examples
asp.netcrystal-reports

How to create dynamic connection string for Crystal Report in Asp.Net C# in my case?


I have multiple iis, all the iis webpages are connecting with different database. I have too many Crystal Reports, once I upload it to all multiple iis, it has to connect to that corresponding database.

I don't have any idea to make connection string in crystal report.

Here is my ReportViewer code

protected void CrystalReportViewer1_Init(object sender, EventArgs e)
    {
        ReportDocument _rdStudents = new ReportDocument();

        Trace.Write("/CrystalReportFiles/RMS/Inventory/");
        string reportPath = Server.MapPath("~" + Request.QueryString["filename"].ToString());

        _rdStudents.Load(reportPath);
        CrystalReportViewer1.ReportSource = _rdStudents;
    }

How can i make connection string here. I saw some of this link. But I didn't get the point.


Solution

  • Follow this.

    TableLogOnInfos TableLogOnInfos = new TableLogOnInfos();
    TableLogOnInfo TableLogOnInfo = new TableLogOnInfo();
    ConnectionInfo ConnectionInfo = new ConnectionInfo();
    Tables Tables;
    ConnectionInfo.ServerName = "ServerName";
    ConnectionInfo.DatabaseName = "Database";
    ConnectionInfo.UserID = "UserId";
    ConnectionInfo.Password = "Password";
    
    ReportDocument _rdStudents = new ReportDocument();
    Trace.Write("/CrystalReportFiles/RMS/Inventory/");
    string reportPath = Server.MapPath("~" + Request.QueryString["filename"].ToString());
    
    _rdStudents.Load(reportPath);
    
    Tables = _rdStudents.Database.Tables;
        foreach (CrystalDecisions.CrystalReports.Engine.Table table in Tables)
        {
            TableLogOnInfo = table.LogOnInfo;
            TableLogOnInfo.ConnectionInfo = ConnectionInfo;
            table.ApplyLogOnInfo(TableLogOnInfo);
        }
     CrystalReportViewer1.RefreshReport();
     CrystalReportViewer1.ReportSource = _rdStudents;