Dynamically setting the Report Data source for rdlc in Report viewer

While using client report definition (rdlc) for reporting in a .net application we need to create a dataset for its datatsource. It may cause a problem of diiferent connections for each report are there in a single application . For avoiding  this we can dynamically set datasource for report during run time.

  1. To begin with we need to add a new rdlc file into the project.
Add a new rdlc file

2. Then add a Dataset for designing the report

Add a dataset

3. To populate the dataset add a table adapter with corresponding stored procedure or a query to fetch the data.

Dataset

4. After creating the data adapter we can access this in our report definition (rdlc)

5. Once the data set s added, design the report(rdlc) based on your using this dataset . After designing remove this dataset from the project.

6. We need to add  MicrosoftReportViwer into our form/aspx page .

7. Then we can dynamically assign the data source from the data access or business logic in the code as detailed below.

 BLL.Class obj= new BLL.Class();
 // Getting the data 
 DataTable dtTest = obj.SelectData();

 this.reportViewer1.RefreshReport();
 reportViewer1.Reset();
 reportViewer1.ProcessingMode = ProcessingMode.Local;
 LocalReport rep = reportViewer1.LocalReport;
 rep.Refresh();
 ReportDataSource rds = new ReportDataSource(dtTest);
 rep.ReportEmbeddedResource ="Report1.rdlc";
 rds.Name = "DataSet1_Data";
 this.reportViewer1.DataSource.Add(rds);
 reportViewer1.RefreshReport();

reportViewer1 : Name of the MicrosoftReportViwer control

We can also pass report parameters to rdlc as follows

//Report parameter
ReportParameter rp = new ReportParameter("content","HELLO");//(Param name,value)
this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });