Dynamically change a Crystal Report connection

I use CrystalReportViewer and CrystalReportSource to load and display a .rpt file in my application.

My situation is as follows:

Say the person created the crystal report outside of my application and set its data source to database A. Then I use this .rpt file in my application, but I need to link it to another database (identical to the original one in terms of table structure and names columns, but with a different connection string using a different username and password). How to do it in C #?

I am currently downloading a report using:

this.CrystalReportSource1.ReportDocument.Load(reportsSubfolder + report.ReportFileName);
//it is here that I need to change the connection data of the report.
+5
source share
3 answers

, .

private void SetDBLogonForReport(CrystalDecisions.Shared.ConnectionInfo connectionInfo, CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument)
{
    CrystalDecisions.CrystalReports.Engine.Tables tables = reportDocument.Database.Tables;

    foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
    {
        CrystalDecisions.Shared.TableLogOnInfo tableLogonInfo = table.LogOnInfo;

        tableLogonInfo.ConnectionInfo = connectionInfo;
        table.ApplyLogOnInfo(tableLogonInfo);
    }
}

ConnectionInfo . , .

+3

VB:

    Dim report = New ReportDocument

    Try
        'open report
        report.Load(filename, OpenReportMethod.OpenReportByTempCopy)

        'do this for each distinct database connection, rather than for table
        report.SetDatabaseLogon("user", "password", "server", "database")

    Catch ex As Exception
        'preserve the stack trace information
        Throw

    End Try
+2

You can use the following code to apply specific connection information to a report at run time.

Please use this method immediately after loading the rpt report file and pass the necessary connection information to the method, it will extract the report data from the transmitted connection information.

    public static void CrystalReportLogOn(ReportDocument reportParameters,
                                          string serverName,
                                          string databaseName,
                                          string userName,
                                          string password)
    {
        TableLogOnInfo logOnInfo;
        ReportDocument subRd;
        Sections sects;
        ReportObjects ros;
        SubreportObject sro;

        if (reportParameters == null)
        {
            throw new ArgumentNullException("reportParameters");
        }

        try
        {
            foreach (CrystalDecisions.CrystalReports.Engine.Table t in reportParameters.Database.Tables)
            {
                logOnInfo = t.LogOnInfo;
                logOnInfo.ReportName = reportParameters.Name;
                logOnInfo.ConnectionInfo.ServerName = serverName;
                logOnInfo.ConnectionInfo.DatabaseName = databaseName;
                logOnInfo.ConnectionInfo.UserID = userName;
                logOnInfo.ConnectionInfo.Password = password;
                logOnInfo.TableName = t.Name;
                t.ApplyLogOnInfo(logOnInfo);
                t.Location = t.Name;
            }
        }
        catch
        {
            throw;
        }

        sects = reportParameters.ReportDefinition.Sections;
        foreach (Section sect in sects)
        {
            ros = sect.ReportObjects;
            foreach (ReportObject ro in ros)
            {
                if (ro.Kind == ReportObjectKind.SubreportObject)
                {
                    sro = (SubreportObject)ro;
                    subRd = sro.OpenSubreport(sro.SubreportName);
                    try
                    {
                        foreach (CrystalDecisions.CrystalReports.Engine.Table t in subRd.Database.Tables)
                        {
                            logOnInfo = t.LogOnInfo;
                            logOnInfo.ReportName = reportParameters.Name;
                            logOnInfo.ConnectionInfo.ServerName = serverName;
                            logOnInfo.ConnectionInfo.DatabaseName = databaseName;
                            logOnInfo.ConnectionInfo.UserID = userName;
                            logOnInfo.ConnectionInfo.Password = password;
                            logOnInfo.TableName = t.Name;
                            t.ApplyLogOnInfo(logOnInfo);
                        }
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
        }
    }
+1
source

All Articles