Crystal Reports: Updating the database connection information at runtime

Posted: 09/10/2010

If you've used Crystal Reports with .Net then at some point you've probably had a need to update the connection information in the report at runtime. This is especially true if you have unit, regression and production web-sites that these reports are generated from. The problem, all the code on the web and the methods provided by SAP/Business Objects only work on certain connection types.

If you use an RDO connection with an ODBC connection string, you're out of luck. I've have tried for years off and on every which way from Sunday to update this type of connection at runtime and nothing works (and I've never found anyone online whose got it to work). It's just buggy. However, there is still hope. The code I speak of above that you find everywhere (including the VS snippets editor) will update the connection information at runtime if you use an ADO/OLEDB connection. This requires of course that what you're connecting to have an OLEDB provider. In most cases, this shouldn't be an issue (at least not for me).

If this doesn't work for you then you're other option is to build a DataSource in .Net and then pass that to the Crystal Report (which I've used before and also works well). A simple System.Data.DataTable or anything that implements IDataReader does the trick though I assume you can also use a DataSet if needed. In this case, you'll need to make an XML schema file that outlines what's in that DataTable for Crystal, then you can lay your report out from those fields. It's imperative that the XML file you create match the contents of the DataSource that you pass to Crystal.