Skip to main content
All docs
V25.2
  • Register MongoDB Data Connections (ASP.NET Web Forms)

    • 3 minutes to read

    This document describes how to define MongoDB data connections available in the Web Report Designer. The Data Source Wizard displays these connections when users create new MongoDB data sources.

    Important

    Install the MongoDB.Driver NuGet package to work with MongoDB in the Data Source Wizard.

    The Data Source Wizard allows you to create a new MongoDB data source if a MongoDB data connection provider is registered in the application. You can register a built-in connection provider or implement and register a custom provider:

    Built-in connection provider
    The provider obtains connections from the application’s configuration file (Web.config).
    Custom connection provider
    The provider allows you to create connection strings at runtime and use a custom storage to store/load connection strings and credentials.

    Use the Application Configuration File

    Follow the steps below to use connection strings from the application configuration file:

    1. Add data connections to the application’s configuration file (Web.config).

      <!-- ... -->
      <connectionStrings>
          <add name="MongoDBConnection" connectionString="mongodb://localhost:27017/" />
      </connectionStrings>
      
    2. Register the connection string provider. For this, call the static DefaultReportDesignerContainer.RegisterDataSourceWizardConfigFileMongoDBConnectionStringsProvider method at application startup.

      using DevExpress.XtraReports.Web.ReportDesigner;
      // ...
      public class Global : System.Web.HttpApplication
      {
          void Application_Start(object sender, EventArgs e)
          {
              // ...
              DefaultReportDesignerContainer.RegisterDataSourceWizardConfigFileMongoDBConnectionStringsProvider();
              // ...
          }
      // ...
      }    
      

    Implement a Custom Connection String Provider

    A custom connection provider allows you to manage MongoDB connections accessible to users:

    • Modify connections at runtime (for example, depending on the user)
    • Allow users to create new connections
    • Validate connections and keep them in a separate storage
    • Store credentials securely

    To use a custom MongoDB connection strings provider, follow the steps below:

    1. Implement the IDataSourceWizardMongoDBConnectionStringsProvider interface. The following code snippet shows a sample implementation:

      using DevExpress.DataAccess.ConnectionParameters;
      using DevExpress.DataAccess.Native;
      using DevExpress.DataAccess.Web;
      using System.Collections.Generic;
      using System.Linq;
      
      public class MyMongoDBConnectionStringsProvider : IDataSourceWizardMongoDBConnectionStringsProvider {
          public Dictionary<string, string> GetConnectionDescriptions() {
              Dictionary<string, string> connections = AppConfigHelper.GetConnections().Keys.ToDictionary(x => x, x => x);
              connections.Add("MyMongoDBConnection", "MyMongoDBConnection");
              return connections;
          }
      
          public DataConnectionParametersBase GetDataConnectionParameters(string name) {
              var mongoDBPasswordAuthInfo = new MongoDBPasswordAuthenticationInfo(
                  username: "name",
                  password: "password",
                  authenticationDatabase: "admin"
              );
              if (name == "MyMongoDBConnection") {
                  return new MongoDBConnectionParameters(
                      hostName: "localhost",
                      isSRV: true,
                      port: 27017,
                      info: mongoDBPasswordAuthInfo
                  );
              }
              return AppConfigHelper.LoadConnectionParameters(name);
          }
      }
      
    2. Call the RegisterDataSourceWizardMongoDBConnectionStringsProvider method at application start to register the custom connection strings provider:

      public class Global_asax : System.Web.HttpApplication {
          void Application_Start(object sender, EventArgs e) {
              //...
              // Register the custom connection strings provider for MongoDB.
              DefaultReportDesignerContainer.RegisterDataSourceWizardMongoDBConnectionStringsProvider<MyMongoDBConnectionStringsProvider>();
          }
          //...
      }