Register Data Connections

This document describes how to provide a Web Report Designer with a set of data connections. These connections are displayed for end-users in the SQL Data Source Wizard when creating new data sources.

Provide Data Connections

Important

When the SQL Data Source wizard obtains connection strings from the Web.config file, only the connection name is serialized with the report definition.

You can register a custom connection string provider. Based on the registration mode you choose, all the connection parameters or the connection name only are serialized along with a report data source. When the report serialized with connection parameters is passed to the client, these parameters are encrypted by applying the MachineKey algorithm. To provide a custom encryption mechanism, use the ISecureDataConverter interface.

To provide data connections to a Web Report Designer, use one of the following approaches.

Use Project's Connection Strings

To enable the Web Report Designer to use connection strings specified in the project's Web.config file, register a default connection strings provider, by calling the DefaultReportDesignerContainer.RegisterDataSourceWizardConfigFileConnectionStringsProvider static method on application start as shown in the following code sample.

using DevExpress.XtraReports.Web.ReportDesigner;
using System;
// ...

public class Global_asax : System.Web.HttpApplication {
    void Application_Start(object sender, EventArgs e) {
        // ...
        // Register a connection strings provider.
        DefaultReportDesignerContainer.RegisterDataSourceWizardConfigFileConnectionStringsProvider();
    } 
    // ...
}
Note

The SQL Data Source Wizard can use only connection strings that contain the XpoProvider key. Otherwise, the following error occurs: "Schema loading failed. Unable to open a database."

Do one of the following to resolve this error:

  • Insert the XpoProvider key to the existing connection strings.
  • Add the same connection strings but with the XpoProvider key to the Web.config file and implement a custom connection strings 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 which data connections will be available to an end-user when configuring a report data source using the SQL Data Source Wizard.

  1. Create a custom connection string provider class by implementing the IDataSourceWizardConnectionStringsProvider interface, as demonstrated in the code sample below.

    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("CustomMdbConnection", "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 == "CustomMdbConnection") {
                return new Access97ConnectionParameters("|DataDirectory|nwind.mdb", "", "");
            }
            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. To provide connection parameters for a custom data connection, the IDataSourceWizardConnectionStringsProvider.GetDataConnectionParameters method is implemented.

  2. Register the custom connection strings provider by calling the DefaultReportDesignerContainer.RegisterDataSourceWizardConnectionStringsProvider<T> static method as shown below.

    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>();
        }
        // ...
    }
    

    Alternatively, you call this method with the overrideWebConfigConnections parameter. When this parameter value is set to true, only the connection name is serialized with the report definition (no data connection parameters will ever be saved along with report layouts, providing proper data connection security). When generating a report document for the Web Document Viewer or the Report Designer's Preview, connection parameters will be requested from the custom connection string provider, instead of the predefined one, which 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) {
            // ...
            // Register the custom connection strings provider.
            DefaultReportDesignerContainer.RegisterDataSourceWizardConnectionStringsProvider<MyDataSourceWizardConnectionStringsProvider>(true);
        }
        // ...
    }
    
Note

The IDataSourceWizardConnectionStringsProvider interface allows you to fill the connection string list for the SQL 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.

Provide a Custom Database Schema

After registering a connection strings provider (using either of the approaches described in the previous section), you can specify which tables, views and stored procedures can be included into a new data source created using the SQL Data Source Wizard. To provide this functionality, do the following.

  1. Create a custom database schema provider (named MyDBSchemaProvider) by implementing the IDBSchemaProviderEx interface as shown below.

    //Add a reference to the DevExpress.DataAccess assembly
    using DevExpress.DataAccess.Sql;
    using DevExpress.Xpo.DB;
    using System.Linq;
    // ...
    
    public class MyDBSchemaProvider : IDBSchemaProviderEx {
        DBSchemaProviderEx provider;
        public MyDBSchemaProvider() {
            this.provider = new DBSchemaProviderEx();
        }
    
        public DBTable[] GetTables(SqlDataConnection connection, params string[] tableList) {
            return provider.GetTables(connection, tableList)
                .Where(table => table.Name.StartsWith("C"))
                .ToArray();
        }
    
        public DBTable[] GetViews(SqlDataConnection connection, params string[] viewList) {
            return provider.GetViews(connection, viewList)
                .Where(view => view.Name.StartsWith("Order"))
                .ToArray();
        }
    
        public DBStoredProcedure[] GetProcedures(SqlDataConnection connection, params string[] procedureList) {
            return provider.GetProcedures(connection, procedureList)
                .Where(storedProcedure => storedProcedure.Arguments.Count == 0)
                .ToArray();
        }
    
        public void LoadColumns(SqlDataConnection connection, params DBTable[] tables) {
            provider.LoadColumns(connection, tables);
        }
    }
    
  2. Next, create a custom database schema provider factory (named MyDataSourceWizardDBSchemaProviderFactory) by implementing the IDataSourceWizardDBSchemaProviderExFactory interface as shown below. In the following code sample, the IDataSourceWizardDBSchemaProviderExFactory.Create method creates a new custom data store schema provider (MyDBSchemaProvider) that will be used by the SQL Data Source Wizard to obtain a custom database schema.

    using DevExpress.DataAccess.Sql;
    using DevExpress.DataAccess.Web;
    // ...
    
    public class MyDataSourceWizardDBSchemaProviderFactory : IDataSourceWizardDBSchemaProviderExFactory {
        public IDBSchemaProviderEx Create() {
            return new MyDBSchemaProvider();
        }
    }
    
  3. Register the custom database schema provider factory, by calling the DefaultReportDesignerContainer.RegisterDataSourceWizardDBSchemaProviderExFactory<T> static method, as shown below.

    using DevExpress.DataAccess.Web;
    using System;
    // Add a reference to the DevExpress.XtraReports.Web assembly
    using DevExpress.XtraReports.Web.ReportDesigner
    // ...
    
    public class Global_asax : System.Web.HttpApplication {
        void Application_Start(object sender, EventArgs e) {
            // ...                          
            // Register a connection strings provider.
            // ...
            // Register the custom provider factory.
            DefaultReportDesignerContainer.RegisterDataSourceWizardDBSchemaProviderExFactory<MyDataSourceWizardDBSchemaProviderFactory>();
        }
        // ...
    }
    

See Also