Skip to main content
All docs
V25.2
  • Restore Data Connections

    • 3 minutes to read

    When you load reports from REPX files, the file may contain a connection string name. In this case, the Document Viewer attempts to create a connection string using this connection string name and retrieve data. You need to implement a service that resolves a connection name to a valid connection.

    SQL Data Sources

    To create this service for SQL data connections, implement the IConnectionProviderFactory service that creates the IConnectionProviderService. The IConnectionProviderService retrieves a connection string from the application configuration file and returns the string to the Document Viewer.

    Follow the steps below:

    1. Add a connection string to the appsettings.json file:

      "MyConnectionStrings": {
        "NWindConnectionString": "XpoProvider=SQLite;Data Source=|DataDirectory|/Data/nwind.db"
      }
      
    2. Create the DataConnectionService.cs file in the Services folder with the following code that implements the services:

      using DevExpress.DataAccess.Sql;
      using DevExpress.DataAccess.Web;
      using DevExpress.DataAccess.Wizard.Services;
      
      public class ReportDataConnectionProviderFactory : IConnectionProviderFactory {
          private readonly IConfiguration _configuration;
          public ReportDataConnectionProviderFactory(IConfiguration configuration) {
              this._configuration = configuration;
          }
          public IConnectionProviderService Create() {
              return new ReportDataConnectionProviderService(_configuration);
          }
      }
      
      public class ReportDataConnectionProviderService : IConnectionProviderService {
          private readonly IConfiguration _configuration;
          public ReportDataConnectionProviderService(IConfiguration configuration) {
              this._configuration = configuration;
          }
          public SqlDataConnection LoadConnection(string connectionName) {
              string localConnectionName = "DefaultConnection";
              return new SqlDataConnection {
                  Name = connectionName,
                  ConnectionString = _configuration.GetConnectionString(localConnectionName)
              };
          }
      }
      

      The service returns the DefaultConnection connection string for any data connection to which a report is bound.

    3. Register the IConnectionProviderFactory and IConnectionProviderService services at application startup:

      using DevExpress.DataAccess.Wizard.Services;
      // ...
      builder.Services.AddScoped<IConnectionProviderFactory, ReportDataConnectionProviderFactory>();
      builder.Services.AddScoped<IConnectionProviderService, ReportDataConnectionProviderService>();
      

    JSON Data Sources

    To create such a service for JSON data connections, implement the IJsonDataConnectionProviderFactory service that creates the IJsonDataConnectionProviderService. The IConnectionProviderService retrieves a connection string from the application configuration file and returns the string to the Document Viewer.

    Follow the steps below:

    1. Add a connection string to the appsettings.json file:

      "MyCustomConnectionStrings": {
          "JsonConnection": "Uri=Data/nwind.json"
      }
      
    2. Create the DataConnectionService.cs file in the Services folder with the following code that implements the services:

      using DevExpress.DataAccess.Json;
      using DevExpress.DataAccess.Web;
      
      public class CustomJsonConnectionProviderFactory : IJsonDataConnectionProviderFactory
      {
          readonly IJsonDataConnectionProviderService connectionProviderService;
          public CustomJsonConnectionProviderFactory(IJsonDataConnectionProviderService connectionProviderService)
          {
              this.connectionProviderService = connectionProviderService;
          }
          public IJsonDataConnectionProviderService Create()
          {
              return connectionProviderService;
          }
      }
      
      public class CustomJsonConnectionProviderService : IJsonDataConnectionProviderService
      {
          readonly IConfiguration configuration;
      
          public CustomJsonConnectionProviderService(IConfiguration configuration)
          {
              this.configuration = configuration;
          }
      
          JsonDataConnection IJsonDataConnectionProviderService.GetJsonDataConnection(string name)
          {
              var customConnectionStrings =
                 configuration
                 .GetSection("MyCustomConnectionStrings")
                 .AsEnumerable(true)
                 .ToDictionary(x => x.Key, x => x.Value);
              if (!customConnectionStrings.ContainsKey(name))
              {
                  throw new KeyNotFoundException($"Connection string '{name}' was not found.");
              }
              return new JsonDataConnection(customConnectionStrings[name]);
          }
      }
      
    3. Register the IConnectionProviderFactory and IConnectionProviderService services at application startup:

      using DevExpress.DataAccess.Wizard.Services;
      // ...
      
      builder.Services.AddScoped<IJsonDataConnectionProviderService, CustomJsonConnectionProviderService>();
      builder.Services.AddScoped<IJsonDataConnectionProviderFactory, CustomJsonConnectionProviderFactory>();
      

    Object Data Sources

    The report definition file (REPX file) contains the following ObjectDataSource information: the type of an object used for the object data source, the object’s property or method name used to retrieve data, and the parameters. This information is Base-64 encoded.

    When the Document Viewer loads a report, it attempts to recreate the data source and use it to retrieve data. If the object that serves as the data source is not defined or referenced in your project, the attempt to recreate the data source fails. In this situation, implement a custom IReportProvider / IReportProviderAsync service that assigns a data source to the report before the Document Viewer displays it.