Register Default Data Connections

  • 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 specify a 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 the DashboardConnectionStringsProvider / ConfigFileConnectionStringsProvider 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:

ASP.NET Core:

using DevExpress.DashboardWeb;
// ...
public void ConfigureServices(IServiceCollection services) {    
    services.AddScoped<DashboardConfigurator>((IServiceProvider serviceProvider) => {
        DashboardConfigurator configurator = new DashboardConfigurator();
        // ...
        configurator.SetConnectionStringsProvider(new DashboardConnectionStringsProvider(Configuration));

        return configurator;
    });
    // ...
}

ASP.NET MVC:

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

Manage Connection String Access

You can specify connection strings depending on the user’s access rights. Use the DashboardConfigurator.SetConnectionStringsProvider method to specify a data connection provider.

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;
// ...
public void ConfigureServices(IServiceCollection services) {
    services.AddScoped<DashboardConfigurator>((IServiceProvider serviceProvider) => {
        DashboardConfigurator configurator = new DashboardConfigurator();
        configurator.SetConnectionStringsProvider(new MyDataSourceWizardConnectionStringsProvider());
        // ...
        return configurator;
    });
    // ...
}

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

Get more information: Manage Multi-Tenancy.

See Also