Back to Devexpress

MongoDB Data Source in an ASP.NET Web Forms App

dashboard-403113-web-dashboard-integrate-dashboard-component-aspnet-web-forms-dashboard-control-register-default-data-sources-mongodb-data-source.md

latest8.7 KB
Original Source

MongoDB Data Source in an ASP.NET Web Forms App

  • Jan 05, 2026
  • 3 minutes to read

This tutorial creates an in-memory data source and connects it to a local MongoDB database.

Prerequisites

Install the MongoDB.Driver NuGet package.

Create a MongoDB Data Source

Create a DashboardMongoDBDataSource instance with the specified connection name (mongoDataSourceConnection).

csharp
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardCommon;
using DevExpress.DataAccess.ConnectionParameters;

public static DataSourceInMemoryStorage CreateDataSourceStorage() {
    // ...
    DashboardMongoDBDataSource mongoDataSource = new DashboardMongoDBDataSource("MongoDB Data Source", "mongoDataSourceConnection");
}

Specify Connection Parameters

Handle the ASPxDashboard.ConfigureDataConnection event to specify connection parameters. You can use a connection string or configure connection parameters manually.

Use a Connection String

  1. Create a MongoDBCustomConnectionParameters object with the specified connection string.
  2. Assign the created MongoDBCustomConnectionParameters object to the e.ConnectionParameters event parameter.
csharp
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.ConnectionParameters;

private void ASPxDashboard1_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
    if (e.ConnectionName == "mongoDataSourceConnection") {
        e.ConnectionParameters = new MongoDBCustomConnectionParameters("mongodb://mongodb0.example.com:27017");
    }
}

Configure Connection Parameters

  1. Create a MongoDBConnectionParameters object.
  2. Use Hostname and Port properties to specify the hostname and port.
  3. Optional. Enable the IsSRVRecord option if the hostname is an SRV record.
  4. Optional. Use the object’s AuthenticationInfo property to specify authentication credentials.
  5. Assign the created MongoDBCustomConnectionParameters object to the e.ConnectionParameters event parameter.
csharp
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.ConnectionParameters;

private void ASPxDashboard1_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
    if (e.ConnectionName == "mongoDataSourceConnection") {
        // MongoDB without authentication credentials.
        e.ConnectionParameters = new MongoDBConnectionParameters("localhost", false, 27017);
    }
    if (e.ConnectionName == "mongoDataSourceConnectionWithAuth") {
        // MongoDB with authentication credentials.
        var infoScramSHA256 = new MongoDBScramSHA256AuthenticationInfo("UserName", "Password", "Northwind");
        e.ConnectionParameters = new MongoDBConnectionParameters("localhost", false, 27017, infoScramSHA256);
    }
}

Load a Connection String from the Project’s Configuration File

See the following help topic for more information: ConnectionName.

Implement a Custom Connection Service

See the following help topic for more information: LoadConnection(String).

Create Data Queries

An instance of the MongoDBQuery class is a query to a MongoDB database collection. You can create multiple queries for different collections. To create a query, initialize a MongoDBQuery object and set its DatabaseName and CollectionName properties.

csharp
using DevExpress.DataAccess.MongoDB;

public static DataSourceInMemoryStorage CreateDataSourceStorage() {
    // ...
    MongoDBQuery queryCategories = new MongoDBQuery() {
        DatabaseName = "Northwind",
        CollectionName = "Categories"
    };
    mongoDataSource.Queries.Add(queryCategories);

    MongoDBQuery queryProducts = new MongoDBQuery() {
        DatabaseName = "Northwind",
        CollectionName = "Products"
    };
    mongoDataSource.Queries.Add(queryProducts);
}

To filter collection items, set the query’s FilterString property. To apply conditional filters, add QueryParameter objects to the Parameters collection.

Each query name must be unique. The CollectionName property specifies the query name. To create multiple queries for the same collection, use the Alias property to specify different query names.

csharp
using DevExpress.DataAccess.MongoDB;
using DevExpress.DataAccess;

public static DataSourceInMemoryStorage CreateDataSourceStorage() {
    // ...
    MongoDBQuery queryProductsFiltered = new MongoDBQuery() {
        DatabaseName = "Northwind",
        CollectionName = "Products",
        FilterString = "[CategoryID] = ?CategoryID and [UnitPrice] > 30",
        Alias = "Filtered Products"
    };
    var queryParam = new DevExpress.DataAccess.MongoDB.QueryParameter("CategoryID", typeof(Expression), new Expression("?CategoryID", typeof(int)));
    queryProductsFiltered.Parameters.Add(queryParam);

    mongoDataSource.Queries.Add(queryProductsFiltered);
}

Register the MongoDB Data Source

Use the RegisterDataSource(String, XDocument) method to register the DataSourceInMemoryStorage object in the data source storage.

csharp
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardWeb;

public static DataSourceInMemoryStorage CreateDataSourceStorage() {    
    // ...
    DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();        
    dataSourceStorage.RegisterDataSource("mongoDataSource", mongoDataSource.SaveToXml());

    return dataSourceStorage;
}

Use the Data Source Storage

  1. Call the ASPxDashboard.SetDataSourceStorage method to use the configured data source storage in the Web Dashboard.
  2. Handle the ASPxDashboard.ConfigureDataConnection event to configure the connection. See the following section in this help topic for more information: Specify Connection Parameters.

Dashboard Data Source Wizard

Users can use the Dashboard Data Source Wizard to create a new MongoDB data source based on an existing connection.

See the following topic for details: Specify Data Source Settings (MongoDB).

See Also

ASP.NET Web Forms Security Considerations