Skip to main content

Register Data Connections for Web Dashboard

  • 4 minutes to read

Register data connections to allow users to create data sources in the Dashboard Data Source Wizard. To create a data connection, add a connection string to the configuration file or implement a custom data connection provider.

Add a Connection String to the Configuration File

For security reasons, specify connection strings in the application’s configuration file (appsettings.json for ASP.NET Core and Web.config for ASP.NET MVC).

ASP.NET Core

{
    "ConnectionStrings": {
        "MS SQL Connection": "XpoProvider=MSSqlServer; data source=localhost; initial catalog=Northwind; Integrated Security=SSPI; Persist Security Info=True;",
        "XML Connection": "XpoProvider=InMemoryDataStore;Read Only=true;Data Source=Data\\nwind.xml;",
        "JSON Connection to URI": "uri=https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json",
        "MongoDB (local)": "mongodb://localhost:27017/"
    }
}

ASP.NET MVC

<configuration>
    <connectionStrings>
        <add name="MsSqlConnection" connectionString="XpoProvider=MSSqlServer; data source=localhost; initial catalog=Northwind; integrated security=SSPI;" />
        <add name="XmlConnection" connectionString="XpoProvider=InMemoryDataStore;Read Only=true;Data Source=Data\\nwind.xml;" />
        <add name="JsonConnection" connectionString="uri=https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json" />
        <add name="MongoDB (local)" connectionString="mongodb://localhost:27017/" />
    </connectionStrings>
</configuration>

The Web Dashboard control does not expose connection string names to the client. If a user creates a data source in the Data Source Wizard, the control throws an exception when it attempts to load connection strings. To resolve this behavior, call the DashboardConfigurator.SetConnectionStringsProvider method and pass the predefined connection strings provider instance.

Use the following providers based on the platform:

The following code snippets use the default provider:

ASP.NET Core

using DevExpress.DashboardWeb;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDevExpressControls();
builder.Services.AddScoped<DashboardConfigurator>((IServiceProvider serviceProvider) => {
    DashboardConfigurator configurator = new DashboardConfigurator();
    // ...
    configurator.SetConnectionStringsProvider(new DashboardConnectionStringsProvider(Configuration));

    return configurator;
});

var app = builder.Build();

ASP.NET MVC

DashboardConfigurator.Default.SetConnectionStringsProvider(new DevExpress.DataAccess.Web.ConfigFileConnectionStringsProvider());

Create a Custom Data Connection Strings Provider

Use the DashboardConfigurator.SetConnectionStringsProvider method to specify a data connection provider.

A custom data connection strings provider allows you to inject application-specific logic. Implement the IDataSourceWizardConnectionStringsProvider interface and pass the provider instance to the SetConnectionStringsProvider method.

The following example implements a custom connection strings provider.

using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Web;

// ...

public class MyDataSourceWizardConnectionStringsProvider : IDataSourceWizardConnectionStringsProvider {
    public Dictionary<string, string> GetConnectionDescriptions() {
        Dictionary<string, string> connections = new Dictionary<string, string>();

        // Customize the loaded connections list.  
        connections.Add("jsonUrlConnection", "JSON URL Connection");
        connections.Add("msSqlConnection", "MS SQL Connection");
        return connections;
    }

    public DataConnectionParametersBase GetDataConnectionParameters(string name) {
        // Return custom connection parameters for the custom connection.
        if (name == "jsonUrlConnection") {
            return new JsonSourceConnectionParameters() { 
                JsonSource = new UriJsonSource(
                    new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json")) 
                };
        } else if (name == "msSqlConnection") { 
            return new MsSqlConnectionParameters("localhost", "Northwind", "", "", MsSqlAuthorizationType.Windows);
        } 
        throw new System.Exception("The connection string is undefined.");
    }
}

After you implement a custom connection strings provider, create its instance and pass it to the DashboardConfigurator.SetConnectionStringsProvider method:

ASP.NET Core

using DevExpress.DashboardWeb;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDevExpressControls();
builder.Services.AddScoped<DashboardConfigurator>((IServiceProvider serviceProvider) => {
    DashboardConfigurator configurator = new DashboardConfigurator();
    configurator.SetConnectionStringsProvider(new MyDataSourceWizardConnectionStringsProvider());
    // ...
    return configurator;
});

var app = builder.Build();

ASP.NET MVC

using DevExpress.DashboardWeb;
// ...
public class Global : System.Web.HttpApplication {
    protected void Application_Start(object sender, EventArgs e) {
        // ...
        DashboardConfigurator.Default.SetConnectionStringsProvider(new MyDataSourceWizardConnectionStringsProvider());
    }
    // ...
}

See the following help topic for more information: Manage Multi-Tenancy.

See Also