Search results
PDF

How to change the data source dynamically

You have to use the reportOption.ReportModel.DataSourceCredentials available with the OnInitReportOptions method to dynamically change the data source in the web API controller. The following code sample shows how to change the connection string of the AdventureWorks data source in the report.

    [NonAction]
    public void OnInitReportOptions(ReportViewerOptions reportOption)
    {
        DataSourceCredentials dataSourceCredentials = new DataSourceCredentials();

        string connectionString = "Data Source = dataplatformdemodata.syncfusion.com; Initial Catalog = AdventureWorks; User ID = 'demoreadonly@data-platform-demo'; Password = 'N@c)=Y8s*1&dh'";

        //You have to provide the shared data source name used with the report or the data source name available with the report.
        dataSourceCredentials.Name = "AdventureWorks";
        dataSourceCredentials.ConnectionString = connectionString;
        reportOption.ReportModel.DataSourceCredentials = new List<DataSourceCredentials> { dataSourceCredentials };
    }

You can find the following help documentation for how to change data sources based on the application parameters in various platforms.