xtrareports-401883-web-reporting-asp-net-webforms-reporting-document-viewer-in-asp-net-webforms-reporting-bind-to-data-update-database-connection.md
You may need to update the database connections at runtime in the following situations:
appsettings.json) should be filtered or replaced at runtime.When you create a data source with the Data Source Wizard, the Wizard creates a connection string in the application configuration file and specifies the connection name in the SqlDataSource.ConnectionName property. The saved report layout (.REPX file) contains only the connection name. When the report layout is loaded, the connection name is resolved to a connection string, and you can implement a custom resolution service (IConnectionProviderService), as described later.
You cannot use the custom IConnectionProviderService if a serialized connection contains connection parameters. The IConnectionProviderService.LoadConnection method is not called when a report loads a layout that contains data source connections with embedded parameters.
If you have a saved report with serialized connection parameters, open the report, clear connection parameters, and save the report. The following code snippet uses the DataSourceManager to clear connection parameters:
XtraReport report;
var dataSources = DataSourceManager.GetDataSources(report, true);
foreach (var dataSource in dataSources) {
if(dataSource is SqlDataSource sds && !String.IsNullOrEmpty(sds.ConnectionName)) {
sds.ConnectionParameters = null;
}
}
Another option is the SqlDataSource.ConfigureDataConnection event. Handle the ConfigureDataConnection event and use the e.ConnectionParameters property to access and modify connection parameters. You can register the WebDocumentViewerOperationLogger service and subscribe to that event in the WebDocumentViewerOperationLogger.ReportOpening method.
Implement the IConnectionProviderFactory service:
Implement the IConnectionProviderService class:
Register the MyConnectionProviderFactory service at application startup before the ASPxWebDocumentViewer.StaticInitialize method:
protected void Application_Start() {
// ...
DevExpress.XtraReports.Web.WebDocumentViewer.DefaultWebDocumentViewerContainer.RegisterConnectionProviderFactory<MyConnectionProviderFactory>();
//...
DevExpress.XtraReports.Web.ASPxWebDocumentViewer.StaticInitialize();
// ...
}
Protected Sub Application_Start()
' ...
DevExpress.XtraReports.Web.WebDocumentViewer.DefaultWebDocumentViewerContainer.RegisterConnectionProviderFactory(Of MyConnectionProviderFactory)()
' ...
DevExpress.XtraReports.Web.ASPxWebDocumentViewer.StaticInitialize()
' ...
End Sub