views:

654

answers:

2

I'm running CR XI, and accessing .RPT files through a ReportViewer in my ASP.NET pages.

I've already got the following code, which is supposed to set the Report Datasource dynamically.

            rptSP = New ReportDocument
            Dim rptPath As String = Request.QueryString("report")
            rptSP.Load(rptPath.ToString, 0)
            Dim SConn As New System.Data.SqlClient.SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString)
            rptSP.DataSourceConnections(SConn.DataSource, SConn.InitialCatalog).SetConnection(SConn.DataSource, SConn.InitialCatalog, SConn.UserID, SConn.Password)
            Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo
            myConnectionInfo.ServerName = SConn.DataSource
            myConnectionInfo.DatabaseName = SConn.InitialCatalog
            myConnectionInfo.UserID = SConn.UserID
            myConnectionInfo.Password = SConn.Password
            'Two new methods to loop through all objects and tables contained in the requested report and set
            'login credentials for each object and table.  
            SetDBLogonForReport(myConnectionInfo, rptSP)
            SetDBLogonForSubreports(myConnectionInfo, rptSP)


            Me.CrystalReportViewer1.ReportSource = rptSP

But when I go into each .RPT file, and open up the Database Expert section, there is obviously still servernames hardcoded in there, and the code listed above doesn't seem to be able to change the servernames that are hardcoded there.

I say this because I have training and production environments. When the .RPT file is hardcoded with my production server, and I open it on my training server with the code above (and the web.config has the training server in the connection string), I get the ol:

Object reference not set to an instance of an object.

And then if I go into the .RPT file, and change over the datasource to the training server, and try to open it again, it works fine. Why doesn't the code above overwrite the .RPT files datasource?

How can I avoid having to open up each .RPT and change the datasource when migrating reports from server to server? Is there a setting in the .RPT file I'm missing or something?

A: 

I know it may sound weird, but I experienced such problems using parameters with reports. I had to set the parameter object before setting the value to them.

This could mean that you perhaps need to set your report object to your source before you configure it. Have you ever tried that?

Will Marcouiller
I'm not sure what you're suggesting. Do you have some code perhaps?
Albert
+1  A: 

Hi,

Are you calling the Crystal method ApplyLogOnInfo? This is a code snippet that I use that works fine.

        //Set database details
        TableLogOnInfo oLogOn;
        foreach (CrystalDecisions.CrystalReports.Engine.Table tbCurrent in report.Database.Tables)
        {
            oLogOn = tbCurrent.LogOnInfo;
            oLogOn.ConnectionInfo.DatabaseName = databaseName;
            oLogOn.ConnectionInfo.UserID = userName;
            oLogOn.ConnectionInfo.Password = pwd;
            oLogOn.ConnectionInfo.ServerName = serverName;
            oLogOn.ConnectionInfo.Type = ConnectionInfoType.SQL;
            tbCurrent.ApplyLogOnInfo(oLogOn);
        }

You would have to change report to rptSP

Thanks

Barry