0

I am using SSRS for reporting purpose in my application.In SSRS i am using Embedded DataSource for fetching data.

I want to pass dynamic connection string in c# code to that datasource so based on connection string parameter it will fetch data assordingly.

Is that possible ?

Dilip Oganiya
  • 1,504
  • 12
  • 20

1 Answers1

0

//Here you can fetch Database Name dynamically and assign to Initial Catalog for preparing dynamic connection string.

string conStr = @"Data Source=DILIPPC\SQLEXPRESS;Initial Catalog=Student";

// Fetch all datasources
 var dataSources = this.reportViewer1.ServerReport.GetDataSources();
 List<DataSourceCredentials> credentials = new List<DataSourceCredentials();
 foreach (var dataSource in dataSources)
 {
    DataSourceCredentials credential = new DataSourceCredentials();
                  credential.Name = dataSource.Name;
                  credential.UserId = " "; // Wirte username of your connection
                  credential.Password = " "; // Wirte password of your connection
                  credentials.Add(credential);
 }
 this.reportViewer1.ServerReport.SetDataSourceCredentials(credentials);

 // Added ConStr parameter and pass this parameter to SSRS report.
ReportParameter rptPar = new ReportParameter("ConStr", conStr);
reportViewer1.ServerReport.SetParameters(rptPar);
Dilip Oganiya
  • 1,504
  • 12
  • 20