Skip to main content

Register Default Data Connections for Web Dashboard

  • 4 minutes to read

You can supply the Web Dashboard with a set of data connections so that users can create new data sources. The predefined data connections are available in the Dashboard Data Source Wizard.

WebDesigner_DefaultDataConnections

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

To avoid saving the credentials in the dashboard xml file, you can pre-configure the connection string 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"
    }
}

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" />
    </connectionStrings>
</configuration>

The Web Dashboard control does not pass connection string names to the client. If users create a new data source in the Data Source Wizard, an exception occurs on an attempt to load connection strings. Pass an instance of the predefined data connection strings provider instance as the DashboardConfigurator.SetConnectionStringsProvider method’s parameter to allow users to create new data sources based on available connection strings from the configuration file. Use the following classes based on the platform:

The following code snippets show how to 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 add custom logic to your application. To use a custom connection provider, implement the IDataSourceWizardConnectionStringsProvider interface and pass the new provider to the SetConnectionStringsProvider method call.

This example shows how to implement a custom provider of connection strings by implementing the IDataSourceWizardConnectionStringsProvider interface.

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, pass this class’s instance 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());
        }
        // ...
    }
}

For more information on how to specify connection strings depending on the user’s access rights, refer to the following article: Manage Multi-Tenancy.

See Also