Again, another common requirement when working with Crystal Reports. So just where is the official documentation? ...
When using the CrystalReportViewer control it is possible to override the report's default database connection information with your own. To do this with SQL Server is well catered for, but what about us poor developers lumbered with ODBC? Perhaps we want to store and deal with connection strings from the web.config too!
Thankfully it is possible to get Crystal Reports to use a custom connection string for ODBC as this code snippet shows:
// Get document
ReportDocument doc = this.CrystalReportSource1.ReportDocument;
// Set connection string from config in existing LogonProperties
doc.DataSourceConnections[0].LogonProperties.Set("Connection String",
ConfigurationManager.AppSettings["connectionString"]);
// Add existing properties to a new collection
NameValuePairs2 logonProps = new NameValuePairs2();
logonProps.AddRange(doc.DataSourceConnections[0].LogonProperties);
// Set our new collection to be the defaults
// This causes Crystal Reports to actually use our changed properties
doc.DataSourceConnections[0].SetLogonProperties(logonProps);
How Does It Work?
The key section is the call to SetLogonProperties which causes Crystal Reports to use the new connection properties. Despite being modifiable the existing LogonProperties of a DataSourceConnection are actually read only.