Articles in this section

How to Dynamically Change the Data Source Connection String in Report Viewer

Published:
Updated:

In some applications, reports need to connect to different databases depending on the logged-in user, selected organization, business unit, or runtime configuration.

For example, a multi-tenant application may maintain separate databases for different customers. When users switch between customers or organizations, the report should retrieve data from the corresponding database without requiring changes to the report definition.

The Bold Reports Report Viewer allows you to dynamically change the data source connection string on the server side using the OnInitReportOptions method.

This approach enables applications to reuse the same report definition while connecting to different databases based on runtime requirements.

When Should You Change the Data Source Dynamically?

Dynamic data source configuration is useful in the following scenarios:

  • Multi-tenant applications: Connect to different customer databases based on the logged-in user.
  • Organization switching: Change database connections when users select a different organization.
  • Runtime database selection: Allow applications to determine database connections based on user selection.
  • Environment switching: Connect reports to development, testing, or production databases based on application configuration.
  • Regional databases: Retrieve data from different regional databases depending on business requirements.

Implementation: Change the Data Source Connection String Dynamically

You can dynamically change the data source connection string by configuring the DataSourceCredentials property in the server-side report initialization event.

Step 1: Configure the Report Viewer

Configure the Report Viewer to use your reporting Web API service.

The report viewer communicates with the server-side reporting service to initialize and process reports.

Step 2: Override the OnInitReportOptions Method

In your Web API controller, override the OnInitReportOptions method to dynamically assign the database connection string.

Example:

[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
    // Retrieve the database connection string dynamically.
    string databaseConnectionString =
        ConfigurationManager.ConnectionStrings["CustomerDB"].ConnectionString;

    // Create data source credentials.
    DataSourceCredentials dataSourceCredential =
        new DataSourceCredentials();

    // Specify the data source name defined in the report
    // or the shared data source linked to the report.
    dataSourceCredential.Name = "DataSource1";

    // Assign the dynamically retrieved connection string.
    dataSourceCredential.ConnectionString = databaseConnectionString;

    // Apply the updated data source credentials to the report.
    reportOption.ReportModel.DataSourceCredentials =
        new List<DataSourceCredentials>()
        {
            dataSourceCredential
        };
}

Note:

  • Replace CustomerDB with the appropriate connection string configuration name.
  • Replace DataSource1 with the actual data source name defined in the report or the shared data source linked to the report.
  • Ensure that the connection string corresponds to the database provider and configuration used by the report.

Step 3: Change the Connection String Based on Runtime Selection

To support dynamic database connections, retrieve the connection string based on the selected customer, organization, or application context.

For example, consider an application that allows users to switch between different organizations.

Organization Database
Organization A OrganizationA_DB
Organization B OrganizationB_DB
Organization C OrganizationC_DB

You can retrieve the appropriate connection string based on the selected organization.

Example:

[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
    // Retrieve the selected organization from the authenticated context.
    string organizationId = GetCurrentOrganizationId();

    // Retrieve the corresponding database connection string.
    string databaseConnectionString =
        GetConnectionStringForOrganization(organizationId);

    // Create data source credentials.
    DataSourceCredentials dataSourceCredential =
        new DataSourceCredentials();

    // Specify the data source name defined in the report
    // or the shared data source linked to the report.
    dataSourceCredential.Name = "DataSource1";

    // Assign the dynamically retrieved connection string.
    dataSourceCredential.ConnectionString = databaseConnectionString;

    // Apply the updated data source credentials to the report.
    reportOption.ReportModel.DataSourceCredentials =
        new List<DataSourceCredentials>()
        {
            dataSourceCredential
        };
}

Here, GetCurrentOrganizationId() and GetConnectionStringForOrganization() represent application-specific methods that retrieve the authenticated user’s organization and its corresponding database connection string.

Note: Retrieve the organization information from a trusted server-side authentication context. Validate the user’s authorization before assigning the corresponding database connection string.

Change the Data Source Based on Organization or Tenant Changes

Consider a multi-tenant application where reports typically use the database associated with the user’s organization. If the user’s organization changes or the tenant configuration is updated, the report must connect to the corresponding database.

The application should:

  1. Identify the updated organization or tenant context.
  2. Retrieve the corresponding database connection string.
  3. Update the server-side connection configuration.
  4. Reinitialize the report using the updated connection string.
  5. Display data retrieved from the corresponding database.

Expected Behavior:

Scenario Report Data Source Expected Result
Initial report loading OrganizationA_DB Displays Organization A data
Organization or tenant changes OrganizationB_DB Displays Organization B data

This approach allows the application to accommodate occasional organization or tenant changes while reusing the existing report definition.

Security Recommendations

  • Store database connection strings in secure server-side configuration.
  • Never expose database credentials in the client-side Report Viewer.
  • Validate user permissions before switching database connections.
  • Retrieve tenant-specific connection strings using authenticated application context.
  • Use database accounts with appropriate permissions for report execution.

Summary

Using the OnInitReportOptions method, you can dynamically configure report data source connection strings on the server side.

This approach is particularly useful for multi-tenant applications, organization-based database switching, and applications that need to select databases dynamically at runtime.

When the user or tenant context changes, retrieve the corresponding database connection string on the server and reinitialize the report with the updated connection string. The Report Viewer will then retrieve and display data from the database associated with the updated connection.

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied