Adding Report Datasource On Runtime to a rdlc file on ReportViewer

27 Jul

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.      For this first we need to add the rdlc File

2.Add a Dataset for designing the report

3.Add a table adapte r with corresponding stored procedure or query.

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

5. Design the rdlc file using this dataset. After designing remove tha dataset from the project.

We can dynamicaaly assign the datasource from our bussiness logic at runtime to the rdlc file. For this e need to add  MicrosoftReportViwer into our form.

.

Here we can  set the datasource to the rdlc file using the following code.

 BLL.Class obj= new BLL.Class();

 DataTable dtTest = obj.SelectData();

 this.reportViewer1.RefreshReport();

 reportViewer1.Visible = true;

 ReportDataSource rds = new ReportDataSource();

 reportViewer1.Reset();

 reportViewer1.ProcessingMode = ProcessingMode.Local;

 LocalReport rep = reportViewer1.LocalReport;

 rep.Refresh();

 rep.ReportEmbeddedResource =“Report1.rdlc”;

 rds.Name = “DataSet1_Data”

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 });

Advertisements

4 Responses to “Adding Report Datasource On Runtime to a rdlc file on ReportViewer”

  1. Arun M November 28, 2012 at 11:21 am #

    Thank u mr nithesh, this is very helpful to me thank u

    • nithuan November 28, 2012 at 11:32 am #

      @Thx arun. happy to help you 🙂

  2. Anonymous September 25, 2013 at 2:35 pm #

    Thanks Nitesh, it`s wonderful demo, I found one more helpful demo i.e. http://www.mixedresponse.com/Blog/Details/10

Trackbacks/Pingbacks

  1. Automaticaly Printing RDLC in Asp.Net MVC 3 | TECH-ELITE - March 29, 2013

    […]             The following code is for dynamically setting the data source to rdlc. I already wrote about this here […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: