Skip to main content

Register SQL Data Connections

  • 5 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.config file, only the connection names are serialized with the report definition.

Note

The Data Source Wizard can use only connection strings that contain the XpoProvider key. Otherwise, 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.

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.

  1. 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.

  2. 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 MachineKey algorithm is used to encrypt these parameters. You can implement a custom data protection mechanism using the ISecureDataConverter or IDataSourceProtectionService interface.

    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 IDataSourceWizardConnectionStringsProvider interface allows you to fill the connection string list for the Data Source Wizard only.

If you print or export reports in code outside the Web Report Designer’s context, implement the cross-platform IConnectionProviderService interface instead.

See Also