How to pass parameter in rdlc report?

The .rdlc is a report format developed by Microsoft Corporation. The RDLC offer different types of report. This article describes how to pass parameter in rdlc report in C#? Summary of the article:

  • Design the RDLC Report
  • Add Parameter to the rdlc Report
  • Pass Parameter to the RDLC Report

Design the RDLC Report
At first we need to design or create the rdlc report. Suppose we are using a Stored Procedure (SP) and it has only one parameter. It’s name is “Parameter1” and it’s data type is integer.

Add Parameter to the rdlc Report
From the Report Data panel right click on Parameters and select Add Parameters.. From the Report Parameter Properties window set the parameter name, data types and click Ok.
Remember that the parameter name should be as like as the parameter name used in Stored Procedure(SP). We can also set other options like available values, default values etc. In this way we will add all the parameters used in SP.
rdlc Report Parameter Properties

rdlc Report Data

Pass Parameter to RDLC Report
The steps to pass parameter to rdlc report are given bellow:

Step 1
Select the form or page that will display the report. Add a ScriptManager on the top of the page.

Step 2
Design the UI according to parameters format. This example has only one parameter and its data type is integer. That’s why we are using one TextBox and Button.

Step 3
From the Toolbox add a ReportViewer control to the Web page or form.Set the Size and position of the control on the page or form.

Step4
Write the following C# code under the click event of Button1.

MyDataSetTableAdapters.TestProcedureTableAdapter ta = new MyDataSetTableAdapters.TestProcedureTableAdapter();
MyDataSet.TestProcedureDataTable dt = new MyDataSet.TestProcedureDataTable();
ta.Fill(dt, Convert.ToInt16(TextBox1.Text));
ReportDataSource rds = new ReportDataSource();
rds.Name = "DataSet1";
rds.Value = dt;

ReportParameter rp = new ReportParameter("Parameter1", TextBox1.Text.ToString());

ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";
ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();

If the number of parameter is more then one, we need to change the code as like:

/ Suppose we have 2 parameters
MyDataSetTableAdapters.TestProcedureTableAdapter ta = new MyDataSetTableAdapters.TestProcedureTableAdapter();
MyDataSet.TestProcedureDataTable dt = new MyDataSet.TestProcedureDataTable();
ta.Fill(dt, Convert.ToInt16(TextBox1.Text), Convert.ToInt16(TextBox2.Text));
ReportDataSource rds = new ReportDataSource();
rds.Name = "DataSet1";
rds.Value = dt;

ReportParameter rp1 = new ReportParameter("Parameter1", TextBox1.Text.ToString());
ReportParameter rp2 = new ReportParameter("Parameter2", TextBox2.Text.ToString());

ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";
ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp1, rp2 });
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();

We may include Microsoft.Reporting.WebForms namespace.
To preview the report build or deploy the application and browse that page or form.

Comments

  1. By Mikel

    Reply

  2. By Soman

    Reply

  3. By Ronjit Chandro Sil

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *