We ship several built-in data access connectors that are seamlessly integrated into the Report Designer interface and make up a part of the report layout: Bind a Report to a Data Source. One of the common tasks is about updating report datasources at runtime in code. For example, imagine a scenario where you have test and production databases which you quickly change for previewing data. This scenario splits into two tasks: creating reports and then updating them at some point later in the code.
Requirement: easily share REPX files between two applications without having to update them manually.
Bad approach: using the default Report Designer implementation which saves connection details right in the REPX file.
Why bad? Those details are hardcoded and they present a security issue if REPX gets leaked.
Good approach: registering special services for managing available connections in the Report Designer. Now, only the connection name is saved in the REPX.
Service for bringing a custom list of available connections in the designer | Service for saving custom connection an end-user types in manually | What ensures that only the connection name is saved in a REPX file | |
---|---|---|---|
Desktop | IConnectionStorageService | IConnectionStorageService | IConnectionStorageService.GetConnections method has the SqlDataConnection.StoreConnectionNameOnly property set to true |
Web | IDataSourceWizardConnectionStringsProvider (designer) and IConnectionProviderFactory (viewer) | N/A | Pass "true" to the RegisterDataSourceWizardConnectionStringsProvider method parameter |
Runtime (no UI) | N/A | N/A | Specify only SqlDataSource.ConnectionName, not SqlDataSource.ConnectionParameters |
Service for resolving connection settings based on their connection name | |
---|---|
Desktop | IConnectionProviderService |
Web | IDataSourceWizardConnectionStringsProvider |
Runtime (no UI) | IConnectionProviderService |
ASP.NET Core specificity:
To inject with services, see IDataSourceWizardConnectionStringsProvider requires parameterless constructor realization when use in RegisterDataSourceWizardConnectionStringsProvider<T> method
Note 1: If you just need to specify a different configuration source (development/production), the easiest solution would be DefaultConnectionStringProvider.
Note 2: legacy reports that already have their connection settings serialized won't use the required services. To verify that, open a REPX file in Notepad and decode the base64 string representing the data source information (Base 64 Encoding Decoding In Notepad++). To resolve this issue, update existing layouts in a batch using the following code:
C#XtraReport1 report = new XtraReport1();
UpdateDataSources(report);
void UpdateDataSources(XtraReport report) {
UniqueDataSourceEnumerator enumerator = new UniqueDataSourceEnumerator();
var datasources = enumerator.EnumerateDataSources(report, false);
foreach (var datasource in datasources) {
if (datasource is SqlDataSource sds)
{
if (sds.ConnectionParameters != null)
{
sds.ConnectionParameters = null;
}
}
}
}
Use the DataSourceManager class: