Skip to Content
0
Nov 09, 2017 at 08:56 PM

How to update the data source's Server Name of a crystal report that is using an OLE DB provider?

4530 Views

Hello, I have a dozen of crystal reports which have been designed to connect to a database on Development Server (for example, Database: XYZ and Server name: DEV). The reports use OLE DB provider to connect to the data source. Also, the report uses various database fields in the designer to print the data.

Now, to make these reports work for on the Test Server for the same database (for example, Database: XYZ and Server name: TEST), I open the reports and update the DataSource's Server Name from "DEV" to "TEST" (still using the OLE DB connection).

I use these reports in a .NET web application (C#) that uses the free Crystal Report Viewer 13 to run the report. When I am working on DEV server, I am using the set of reports whose DataSource's Server Name is set to "DEV", but when the application is moved to the TEST server I am having to use the other set of reports whose DataSource's Server Name is set to "TEST" otherwise, I am getting database logon failed error.

For each report, we fetch the server name, database name, userID, and Password from our application's database and set them to the ConnectionInfo as shown in the code snippet below:

var report = new ReportDocument();
report.Load(filename: Server.MapPath(p));
            
var ciReportConnection = new ConnectionInfo
{
    ServerName = zServer,
    DatabaseName = zDatabase,
    UserID = zUsername,
    Password = zPassword
};

foreach (CrystalDecisions.CrystalReports.Engine.Table table in report.Database.Tables)
{
    table.LogOnInfo.ConnectionInfo = ciReportConnection;
    table.ApplyLogOnInfo(table.LogOnInfo);
}

if (this.CrystalReportViewer1.LogOnInfo != null)
{
    TableLogOnInfos tlInfo = this.CrystalReportViewer1.LogOnInfo;
    foreach (TableLogOnInfo tbloginfo in tlInfo)
    {
        tbloginfo.ConnectionInfo = ciReportConnection;
    }
}
           
CrystalReportViewer1.ReportSource = report;
CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;

The above snippet makes sure to set the DataSource's Server Name dynamically to "DEV" if it is running on DEV server and to "TEST" if it is running on TEST server, but still when I move the code to TEST my reports are not working and I am getting a log on failed error. I am assuming that it is because the reports that have been configured with "DEV" server name are not working on the TEST server. On the other hand, the reports work fine on TEST server if I use the set of reports that have been configured with DataSource's server name as "TEST".

Is there any way, I can update the DataSource's server name dynamically? Any idea where I am going wrong with my C# code?

I cannot remove the data source and the database tables from the report as it will mess the design of the report. I am looking for a way to make these reports work on all the environments (DEV, TEST, and PROD) using OLE DB connection. FYI, the database name on all the servers is same. Any help would be appreciated! Thank you!