The requested page is not available for the requested platform. You are viewing the content for Default platform.

Register Data Connections

  • 6 min to read

This document describes how to provide a set of data connections to the Web Report Designer. The Data Source Wizard displays these connections when end users create new data sources.

Requirements

The SQL 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:

Use the Default Connection String Provider

Use this approach to provide the Report Designer with connection strings registered in an application globally.

Register the Default Implementation

In ASP.NET Core applications, the default connection string provider implementation uses the IConfiguration service to read connection strings. Applications created based on ASP.NET Core templates call the CreateDefaultBuilder method in the Program.cs file to register this service. If the IConfiguration service is not registered, the default provider searches the appsettings.json file in the current directory and reads connection strings from the file’s ConnectionStrings section.

To enable the Report Designer to use this provider, call the static ReportDesignerConfigurationBuilder.RegisterDataSourceWizardConfigFileConnectionStringsProvider method at the application's startup.

using Microsoft.Extensions.DependencyInjection;
using DevExpress.AspNetCore;
using DevExpress.AspNetCore.Reporting;
// ...

 public class Startup {
    // ...
    public void ConfigureServices(IServiceCollection services) {
        // ...
        services.AddDevExpressControls();
        services.ConfigureReportingServices(configurator => {
            configurator.ConfigureReportDesigner(designerConfigurator => {
                designerConfigurator.RegisterDataSourceWizardConfigFileConnectionStringsProvider();
            });
        });
    }
}

Override the Default Implementation

You can override the default connection string provider to get connection strings from a set of different configuration sources, such as appsettings.Development.json, connectionStrings.json, in-memory collection, etc.

  1. Create a custom configuration and load connection strings from all the required sources. For instance, create the ConfigurationBuilder class instance and use its extension methods (AddJson, AddInMemoryCollection, etc.) See Configuration in ASP.NET Core for more information.

    using System.Collections.Generic;
    using Microsoft.Extensions.Configuration;
    // ...
    
    public IDictionary<string, string> GetGlobalConnectionStrings() {
      var connectionStrings = new Dictionary<string, string> {
        [$"ConnectionStrings:VehiclesInMemory"] = "XpoProvider=SQLite;Data Source=Data/vehicles.db",
        [$"ConnectionStrings:CarsInMemory"] = "XpoProvider=SQLite;Data Source=Data/cars.db;"
      };
      return new ConfigurationBuilder()
        .SetBasePath(hostingEnvironment.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
        .AddInMemoryCollection(connectionStrings)
        .AddEnvironmentVariables()
        .Build()
        .GetSection("ConnectionStrings")
        .AsEnumerable(true)
        .ToDictionary(x => x.Key, x => x.Value);
    }
    
  2. Call the static DefaultConnectionStringProvider.AssignConnectionStrings method at the application's startup after the UseDevExpressControls method to register connection strings globally.

    using DevExpress.DataAccess;
    
    public class Startup {
      public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
          // ...
          app.UseDevExpressControls();
          DefaultConnectionStringProvider.AssignConnectionStrings(GetGlobalConnectionStrings());
          // ...
      }
    
    }
    
  3. Call the static ReportDesignerConfigurationBuilder.RegisterDataSourceWizardConfigFileConnectionStringsProvider method at the application's startup as shown above.

Important

When the SQL Data Source wizard obtains connection strings from configuration sources, only the connection names are serialized with the report definition. This provides data connection security. You can also allow passing unencrypted connection parameters to the client.

Build a Custom Configuration for the Designer Only

This approach allows you to provide the Report Designer with an individual set of connection strings. You can also make the Designer read these strings from multiple configuration sources.

  1. Create the ConfigurationBuilder class instance and use its extension methods (AddJson, AddInMemoryCollection, etc.) to add configuration sources. See Configuration in ASP.NET Core for more information.

    using System.Collections.Generic;
    using Microsoft.Extensions.Configuration;
    
    public IConfigurationSection GetConnectionStrings() {
      var connectionStrings = new Dictionary<string, string> {
        [$"ConnectionStrings:VehiclesInMemory"] = "XpoProvider=SQLite;Data Source=Data/vehicles.db",
        [$"ConnectionStrings:CarsInMemory"] = "XpoProvider=SQLite;Data Source=Data/cars.db;"
      };
      return new ConfigurationBuilder()
        .SetBasePath(hostingEnvironment.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
        .AddInMemoryCollection(connectionStrings)
        .Build()
        .GetSection("ConnectionStrings");
    }
    
  2. Call the ReportDesignerConfigurationBuilder.RegisterDataSourceWizardConfigurationConnectionStringsProvider method at the application's startup after the AddDevExpressControls method call.

    using DevExpress.AspNetCore;
    using DevExpress.AspNetCore.Reporting;
    using Microsoft.Extensions.DependencyInjection;
    
    public void ConfigureServices(IServiceCollection services) {
        services.AddDevExpressControls();
        services.ConfigureReportingServices((builder) => {
            builder.ConfigureReportDesigner(designer => {
                designer.RegisterDataSourceWizardConfigurationConnectionStringsProvider(GetConnectionStrings());
            });
        });
    }
    

Note that if you registered connection strings at the Designer level, the SQL Data Source Wizard does not use the strings registered globally. The latter strings are available for the Report Designer's Preview to fill a report's data source and generate the resulting document.

Implement a Custom Connection String Provider

Use this approach to implement complex logic for specifying connection strings.

To configure connection strings based on strings from the appsettings.json file, follow the steps below.

  1. Implement the IDataSourceWizardConnectionStringsProvider interface as demonstrated below to create a custom connection string provider.

    using System.Collections.Generic;
    using System.Linq;
    using DevExpress.DataAccess.ConnectionParameters;
    using DevExpress.DataAccess.Native;
    using DevExpress.DataAccess.Web;
    // ...
    
    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("Cars");
            connections.Add("Custom Connection", "Custom SQL Connection");
            return connections;
        }
    
        public DataConnectionParametersBase GetDataConnectionParameters(string name) {
            // Return custom connection parameters for the custom connection. 
            if (name == "Custom Connection") {
                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 appsettings.json 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 ReportDesignerConfigurationBuilder.RegisterDataSourceWizardConnectionStringsProvider<T> method at the application's startup to register the custom connection string provider.

    using Microsoft.Extensions.DependencyInjection;
    using DevExpress.AspNetCore;
    using DevExpress.AspNetCore.Reporting;
    // ...
    
    public class Startup {
        // ...
        public void ConfigureServices(IServiceCollection services) {
            // ...
            services.ConfigureReportingServices(configurator => {
                configurator.ConfigureReportDesigner(designerConfigurator => {
                    designerConfigurator.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 ASP.NET Core data protection service encrypts these parameters. You can also implement the ISecureDataConverter interface to provide a custom data protection mechanism.

    using Microsoft.Extensions.DependencyInjection;
    using DevExpress.AspNetCore;
    using DevExpress.AspNetCore.Reporting;
    // ...
    
    public class Startup {
        // ...
        public void ConfigureServices(IServiceCollection services) {
            // ...
            services.ConfigureReportingServices(configurator => {
                configurator.ConfigureReportDesigner(designerConfigurator => {
                    designerConfigurator.RegisterDataSourceWizardConnectionStringsProvider<MyDataSourceWizardConnectionStringsProvider>(true);
                });
            });
        }
    }
    

    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 appsettings.json file.

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.

See Also