Skip to main content
All docs
V25.2
  • MongoDB Data Source in an ASP.NET Web Forms App

    • 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).

    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.
    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.
    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.

    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.

    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.

    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.

      using System;
      using DevExpress.DashboardWeb;
      using DevExpress.DataAccess.MongoDB;
      
      protected void Page_Load(object sender, EventArgs e) {
          // ...
          ASPxDashboard1.SetDataSourceStorage(dataSourceStorage);
          ASPxDashboard1.ConfigureDataConnection += ASPxDashboard1_ConfigureDataConnection;
      }
      

    Tip

    Users can create MongoDB data sources in the Dashboard Data Source Wizard. See the following help topic for more information: Create a MongoDB Data Source.

    Create a MongoDB Data Source - Web Dashboard, DevExpress

    See Also