Skip to Content
0
Apr 22, 2020 at 07:46 PM

Unable to change database at runtime

188 Views Last edit Apr 23, 2020 at 11:44 AM 2 rev

We are using Crystal Reports for Visual Studio CRRuntime_32bit_13_0_22 with Visual Studio 2017 and DB2 Windows Database.

We need to change the database connection at run time (server, database, user password). Our application has sample reports that we distribute to our customers with our application.

When trying to preview the report (we are using the WPF viewer, if that makes any difference) an exception is thrown with the following message:

Unable to connect: incorrect log on parameters.

The exception is occurring at table.ApplyLogOnInfo(table.LogOnInfo);

When we load the report, before the preview, the following code is executed to change the connection information. I based it on samples from here: https://wiki.scn.sap.com/wiki/display/BOBJ/Crystal+Reports+for+.NET+SDK+Samples?original_fqdn=wiki.sdn.sap.com#CrystalReportsfor.NETSDKSamples-Database

I am not using subreports, so I am not looping through the list of subreports.

Here is my code:

 private void ChangeConnectionInfo(ReportDocument boReportDocument)
 {


	boReportDocument.SetDatabaseLogon(databaseUser, databasePassword, databaseServer, databaseName);


	TableLogOnInfo logOnInfo;
	foreach (CrystalDecisions.CrystalReports.Engine.Table table in boReportDocument.Database.Tables)
	{
		logOnInfo = table.LogOnInfo;
		logOnInfo.ReportName = boReportDocument.Name;
		logOnInfo.ConnectionInfo.ServerName = databaseServer;
		logOnInfo.ConnectionInfo.DatabaseName = databaseName;
		logOnInfo.ConnectionInfo.UserID = databaseUser;
		logOnInfo.ConnectionInfo.Password = databasePassword;
		logOnInfo.TableName = table.Name;
		if (schemaName.IsNotNullOrEmpty())
			table.Location = $"{schemaName}.{table.Name}";
		else
			table.Location = table.Name;


		table.ApplyLogOnInfo(table.LogOnInfo);
	}
}


The one odd thing I noticed when in debug is that table is null, even though tables.count is 6 and I can look at the entire list in the debugger. I don't know if that is indicative of the problem or just a debugger anomaly.

Any help would be appreciated.

Eric