Provide the Data Source credential information in code behind

2 Jun 20211 minute to read

Use SetDataSourceCredentials to provide the data source credential for the Report Viewer. Use this method in a ReportLoaded event to avoid unexpected issues.

  • C#
  • this.reportViewerControl.ReportLoaded += new ReportLoadedEventHandler (reportViewerControl_ReportLoaded);
    void reportViewerControl_ReportLoaded(object sender, EventArgs e)
    {
        var dataSources = this.reportViewerControl.GetDataSources();
        List<DataSourceCredentials> credentials=new List<DataSourceCredentials> ();
        foreach (var dataSource in dataSources)
        {
            DataSourceCredentials credential= new DataSourceCredentials ();
            credential.Name = dataSource.Name; // Sets the credential based on the data source.
            credential.UserId = "userName";
            credential.Password = "password";
            credentials.Add (credential);
        }
        this.reportViewerControl.SetDataSourceCredentials (credentials);
    }