Let me elaborate a little more on the problem why using the config files/ConfigurationManager don't work at design time, especially when previewing a report and what is the right way of using connection strings from the config file that work both run time and design time.
The 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 being previewed will get all connection strings defined in the devenv.exe.config
Obviously this is not the right config file that one may expect, so the trick is to use the application settings
as we've previously advised. This is what the Report and Data Source Wizards actually do when instructed to save the selected connection string in the config file.
In this 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:
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:
- 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;
- 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 find no such connection strings and will return the default value from its metadata.
@Ashok: Please consider handling the SubReport.NeedDataSource event
that is raised when the nested report is about to be processed and still has no data source attached.
the Telerik team