Search results
Suggest a FeaturePDF

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 <database> data source in the report.

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

        string connectionString = "Data Source = <instancename>; Initial Catalog = <database>; User ID = '<username>'; Password = '<password>'";

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