I am writing a Windows Forms desktop application that uses Crystal Reports to display information from a SQL database, but I am having trouble getting my code to work in both my development and production environments. I set a connection string in the app.config file that is used to connect to a SQL database, and set the logon information for a Crystal Report dynamically in C# before loading it in the application. My code is as follows:
public ReportDocument getReport()
{
ReportDocument doc = new ReportDocument();
string filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Reports", crReportName);
doc.Load(filename);
//Set Connection Info For Each Table
doc.DataSourceConnections.Clear();
foreach (Table crTable in doc.Database.Tables)
{
TableLogOnInfo crTableLogonInfo = crTable.LogOnInfo;
crTableLogonInfo.ConnectionInfo = getReportConnectionInfo();
crTable.ApplyLogOnInfo(crTableLogonInfo);
}
return doc;
}
private static ConnectionInfo getReportConnectionInfo()
{
System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();
builder.ConnectionString = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
ConnectionInfo crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = (string)builder["Data Source"];
crConnectionInfo.DatabaseName = (string)builder["Initial Catalog"];
crConnectionInfo.UserID = (string)builder["User Id"];
crConnectionInfo.Password = (string)builder["Password"];
crConnectionInfo.IntegratedSecurity = false;
return crConnectionInfo;
}
When I run this code in my development environment, everything works smoothly, and the report is displayed on the screen with the correct information. However, when I run this code in my production environment, I get a "Database Login" popup with the correct server name and login ID, but with a blank database name. I've run variations of this code to set each ConnectionInfo property directly to a string, and I've uninstalled/reinstalled different (32 and 64 bit) versions of the Crystal Reports runtime, but I continue to get the same dialog with a blank database name. What's even more puzzling is that I've successfully deployed this application to another environment (using the same version of the CR Runtime) and everything works fine. Is there something that I'm not doing in my code? Has anyone experienced a problem like this in the past?