Home / Community & Support / Knowledge Base / Telerik Reporting / Designing Reports / Using connectionStrings from configuration file

Using connectionStrings from configuration file

Article Info

Rating: 4

Article information

Article relates to

Telerik Reporting 2.x+

Created by

Steve, Telerik

Last modified

28 August, 2009

Last modified by

Steve, Telerik


PROBLEM
Using the ConfigurationManager/configuration files to setup a connection does not work at design time e.g. when previewing a report. What is the right way of using connection strings from the configuration file, so that it would work both in run time and design time.

SOLUTION
Telerik Report Designer operates inside the Visual Studio application. When previewing a report, the project that contains the report is compiled (if needed) and the report class is instantiated through Reflection, and passed to the previewer. This means that the report constructor is executed in the context of the Visual Studio application. As described in MSDN,  the ConfigurationManager.ConnectionStrings property "gets the  ConnectionStringsSection data for the current application's default configuration".

In the case of report designer/preview the current application is devenv.exe so reading the  ConfigurationManager.ConnectionStrings from the report that is previewed will get all connection strings defined in the devenv.exe.config file.

Obviously this is not the right configuration file that one may expect, so the trick is to use the application settings. This is what the Report and Data Source Wizards actually do when instructed to save the selected connection string in the config file.

In such case the connection strings are not only added to the config file but a specific settings are registered in the Settings.settings XML file (and the corresponding code behind). To access the connection string this way one should use the Settings class like this:

C#:
global::ClassLibrary2.Properties.Settings.Default.AdventureWorksConnection 

VisualBasic.NET:
My.Settings.AdventureWorksConnection  

At design time, during preview of report that uses a connection string from the Settings object, as there is no such connection string in devenv.exe.config, a value from the settings metadata will be returned. At runtime the Settings object will return the connection string from the "correct" config file as it is the current application's config file.

There are two things to consider:
  1. Always modify the application settings at design time through its editor and never change it through the config file because the changes won't be recorded in the settings metadata;
  2. If the reports are defined in a class library and you add settings for this class library, always remember to copy the corresponding connection string entries from the class library's config file to the application's config file. Otherwise at runtime when the application's config file is respected the Settings object will not be able to find such connection strings and will return the default value from its metadata.

Comments

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.