Passing a Parameter to the ReportViewer Control

Posted Wednesday, October 11, 2006 1:22 PM by C-Dog's .NET Tip of the Day

When you build reports they often need paramters passed to them. One common case of using the ReportViewer control is the need to pass a parameter via QueryString. This isn't terribly difficult, but I figure giving a code example, might be useful.

First it is necessary to ccreate a ReportParameter object (be sure you have a reference to Microsoft.Reporting.WebForms). The contructor takes two string parameters one for the Name and one for the Value. A new parameter would look like the code below.

ReportParameter myReportParameter = new ReportParameter("Id",
Request.QueryString["Id"]);

Next, parameters on the ReportViewer are set using the SetParameters method. It takes an array of ReportParameter, so you would have syntax like the following.

MyReportViewer.ServerReport.SetParameters(
new ReportParmeter[] { myReportParameter });

That's all there is to it. Typically you would want to set that in the same method as you set credentials (i.e.: OnLoad).

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=313