Register SQL Data Connections in ASP.NET Web Forms Reporting
- 6 minutes to read
This document describes how to provide a set of SQL data connections to the Web Report Designer. The Data Source Wizard displays these connections when end users create new SQL data sources.
#Use a Project’s Connection Strings
You can enable the Report Designer to use connection strings that you specified in the application’s configuration file (Web.config).
Call the static DefaultReportDesignerContainer.RegisterDataSourceWizardConfigFileConnectionStringsProvider method at the application’s startup to register the default connection string provider as shown below.
using DevExpress.XtraReports.Web.ReportDesigner;
using System;
// ...
public class Global_asax : System.Web.HttpApplication {
void Application_Start(object sender, EventArgs e) {
// ...
DefaultReportDesignerContainer.RegisterDataSourceWizardConfigFileConnectionStringsProvider();
}
// ...
}
Important
When the Data Source wizard obtains connection strings from the Web.
#Implement a Custom Connection Strings Provider
This approach allows you to define data connections to display in the Data Source Wizard when end users configure a report’s data source.
Implement the IDataSourceWizardConnectionStringsProvider interface as demonstrated below to create a custom connection string provider.
using DevExpress.DataAccess.ConnectionParameters; using DevExpress.DataAccess.Native; using DevExpress.DataAccess.Web; using System.Collections.Generic; // ... public class MyDataSourceWizardConnectionStringsProvider : IDataSourceWizardConnectionStringsProvider { public Dictionary<string, string> GetConnectionDescriptions() { Dictionary<string, string> connections = AppConfigHelper.GetConnections().Keys.ToDictionary(x => x, x => x); // Customize the loaded connections list. connections.Remove("LocalSqlServer"); connections.Add("CustomDbConnection", "Custom DB Connection"); connections.Add("CustomSqlConnection", "Custom SQL Connection"); return connections; } public DataConnectionParametersBase GetDataConnectionParameters(string name) { // Return custom connection parameters for the custom connection(s). if (name == "CustomDbConnection") { return new SQLiteConnectionParameters() { FileName = "nwind.db", Password = null }; } else if (name == "CustomSqlConnection") { return new MsSqlConnectionParameters("localhost", "dbName", "userName", "password", MsSqlAuthorizationType.SqlServer); } return AppConfigHelper.LoadConnectionParameters(name); } }
In this code sample, the IDataSourceWizardConnectionStringsProvider.GetConnectionDescriptions method obtains existing connection strings from the Web.config file, removes an undesired data connection, and adds a new custom connection with a description.
The IDataSourceWizardConnectionStringsProvider.GetDataConnectionParameters method provides connection parameters for a custom data connection.
Call the static DefaultReportDesignerContainer.RegisterDataSourceWizardConnectionStringsProvider<T> method at the application’s startup to register the custom connection string provider.
using DevExpress.XtraReports.Web.ReportDesigner; using System; // ... public class Global_asax : System.Web.HttpApplication { void Application_Start(object sender, EventArgs e) { // ... // Register the custom connection strings provider. DefaultReportDesignerContainer.RegisterDataSourceWizardConnectionStringsProvider<MyDataSourceWizardConnectionStringsProvider>(); } // ... }
This method has another overload with the overrideWebConfigConnections parameter. When you call the method without parameters or with the parameter set to false, data connection parameters are serialized with the report data source when the report is passed to the client.
Note
The Machine
Key algorithm is used to encrypt these parameters. You can implement a custom data protection mechanism using the ISecureData or IDataConverter Source interface.Protection Service If you pass true as the method parameter, only the connection name is serialized with the report definition (data connection parameters are not saved). When the Web Document Viewer and the Report Designer’s Preview generate a report document, they request connection parameters from the custom connection string provider instead from the predefined provider that gets connection information from the Web.config file.
using DevExpress.XtraReports.Web.ReportDesigner; using System; // ... public class Global_asax : System.Web.HttpApplication { void Application_Start(object sender, EventArgs e) { // ... DefaultReportDesignerContainer.RegisterDataSourceWizardConnectionStringsProvider<MyDataSourceWizardConnectionStringsProvider>(true); } // ... }
Note
The IData
If you print or export reports in code outside the Web Report Designer’s context, implement the cross-platform IConnection
#Connection String Examples
The Data Source Wizard can use only connection strings that contain the XpoProvider used to identify a data source provider. If you don not specify, the “Schema loading failed.” error occurs.
You can use one of the following approaches to avoid this error:
- Add the XpoProvider key to the existing connection strings.
- Duplicate the connection strings and add the XpoProvider key to the copied strings. You should also implement a custom connection string provider to display the strings with this key only. See the next document section for implementation details.
See connection string examples in the table below:
Relational Database System | Connection String Sample |
---|---|
Microsoft SQL Server | Xpo The MS SQL Server data provider is automatically specified if you set the “Initial Catalog” parameter and do not use the word “Provider” (for instance, the “Xpo |
Microsoft Access | Xpo Xpo |
Microsoft SQL Server CE | Xpo |
Oracle Database | Xpo Xpo Xpo |
Amazon Redshift | Xpo |
Google Big | Xpo Xpo |
Teradata | Xpo |
SAP HANA | Xpo |
SAP Sybase Advantage | Xpo |
SAP Sybase ASE | Xpo |
SAP SQL Anywhere | Xpo |
IBM DB2 | Xpo |
Firebird | Xpo |
My | Xpo |
Pervasive PSQL | Xpo |
Postgre | Xpo |
Vista | Xpo Xpo |
SQLite | Xpo |
XML file | Xpo |
Note
The connection strings demonstrated above are examples. You can specify alternative variants.