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.
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.
Nice article. Thanks cybarlab.
thanks 4 shareing.
Your article is fine. For three parameter
ReportParameter rp1 = new ReportParameter(“StartDate”, TextBox1.Text);
ReportParameter rp2 = new ReportParameter(“EndDate”, TextBox2.Text);
ReportParameter rp3 = new ReportParameter(“Type”, “1”);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.ReportPath = “Report1.rdlc”;
ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp1, rp2, rp3 });
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();
Is it ok??
Is there any way if we want o hard core parameter values for two separate reports using same store procedure instead of passing it thorough Text box or any other method? i.e TextBox1 and TextBox2 values.
Please guide.
Thank you !!!
Good work!
Good work thank you!!!!
Good if snapshots were also with this article
THANK YOU
Welcome
Hi I have a problem by fill…error no overload method “Fill” takes “2” arguments.
Howdy! Quick question that’s completely off topic. Do you know how to make your site mobile friendly?
My blog looks weird when viewing from my iphone4. I’m trying to find a theme or plugin that might be able to resolve this issue.
If you have any recommendations, please share.
Many thanks!
It’s awesome to go to see this web site and reading the views of all colleagues
about this paragraph, while I am also zealous of getting experience.
this was awesome any simple.
i am new in RDLC reporting.
can you suggest how to add Dropdown parameter in report?
That is really fascinating, You’re an excessively
skilled blogger. I have joined your feed and stay
up for in search of extra of your great post. Also, I’ve shared your website in my social networks
Rashed – I am attempting to implement this code, along with some code to automatically render the report to Excel, in VS 2015 and run into a runtime error “An error occurred during local report processing.” with no further information given. I should point out that the export to Excel code used to work in VS 2012 so I’m guessing there’s some trickery to making it work in the new version (as well as your code perhaps?) Thanks in advance for any advice you can send me to help resolve these …
Is there a way to generate charts in RDLC dynamically based on input data . In my case the number of charts is not fixed as it always depends on business use case which will receive through some Json based data.
I am looking for a tweak where I can create a list control and add one chart control to it , later whenever input data receive the list will create & plot as many charts .
Also I want to know like if I can plot different chart types in single list . Please suggest approach or share some useful links. I am new to RDLC .
nice work
In you code
MyDataSetTableAdapters.TestProcedureTableAdapter ta = new MyDataSetTableAdapters.TestProcedureTableAdapter();
MyDataSet.TestProcedureDataTable dt = new MyDataSet.TestProcedureDataTable();
What is that?