Ticket T940374
Visible to All Users

SqlDataSource - Best Practices for Managing Connection Settings in XtraReports

created 5 years ago (modified 2 years ago)

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:

prior to v20.2.5
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; } } } }
v20.2.5+

Use the DataSourceManager class:

C#
var dataSources = DataSourceManager.GetDataSources(report, true); foreach (var dataSource in dataSources) { if(dataSource is SqlDataSource sds && !String.IsNullOrEmpty(sds.ConnectionName)) { sds.ConnectionParameters = null; } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.