Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Using the ReportViewer Control in a WebForm with Parameters

By Darren Herbold-493573,

In this article, I will be demonstrating how to use the ReportViewer Control in a web page that allows a user to enter a report parameter, then passes that parameter to the report, and renders it to the screen.  

Those of you who have been using Reporting Services will be familiar with the Report Manager portal that installs with the product and provides some nice functionality for managing your reporting environment. One obvious restriction though, is that it doesn't allow you to customize the portal to fit the branding of your organization.

Report Viewer to the rescue!   

The Report Viewer Control is a free control provided by Microsoft to display reports on a web page or windows form. To add the Report Viewer Control to your web form, simply right-click on the Toolbox and select "Choose Items..." Check the checkbox for the Report Viewer that contains the namespace: Microsoft.Reporting.WebForms.

 

 You will then see the ReportViewer Control added to your ToolBox:

Drag the ReportViewer Control on your WebForm and size it accordingly.

You will notice that a property box will appear to allow you to set the properties of the ReportViewer at that time. Setting these properties now won't hurt anything, but we will be setting them in code anyway.

In this example, we will be setting the ReportViewer in Remote Mode which means that the actual report file we will point to, will reside in the ReportServer. So, the report that you create for viewing by the ReportViewer in Remote Mode will need to be deployed the the ReportServer. The Report Server URL property points to the Report Server instance and the Report Path property specifies the path in which the report exists.

** To deploy your report that you created to a Report Server, all you have to do is open your report project in Visual Studio,
right click on the report, and click Deploy. As an alternative, you can launch the Report Manager site and upload your
report from there. That's it, easy!

*** 3 Very Important dlls!! ***

There are 3 dlls that your project will need to reference. You must place these dlls in the bin folder of your project.

 

If for some reason you have any difficulty locating these dlls, you can download them here:

Microsoft.ReportViewer.Common.dll

Microsoft.ReportViewer.ProcessingObjectModel.dll

Microsoft.ReportViewer.WebForms.dll

 

Now, on the WebForm I've placed a textbox that will allow the user to enter the parameter required by the report and a button to render the report. I've also added a label control to the left of the textbox with the text "Search Employee by Job Title".

 

Let's take a step back and focus on the Report for a moment. The report that I created takes in a parameter called jobTitle and returns a list of employees and some demographic information for a particular job title from the AdventureWorks database. Below is the query that I'm using to populate the report.

SELECT e.loginid, e.title, a.addressline1, a.city, a.postalcode
FROM HumanResources.Employee e
INNER JOIN HumanResources.EmployeeAddress ea on e.employeeId = ea.employeeId
INNER JOIN Person.Address a on ea.addressId = a.addressId
WHERE e.title LIKE '%' + @jobTitle + '%'

The jobTitle parameter in the Reporting Services Designer has the "Hidden" property checked. This means that the report won't prompt the user for the parameter. We will be setting that parameter manually in code. You will want to set all of your parameters to "Hidden" if you plan to set those manually in code.

 

 

Now let's take a look at some code!

Below is the code-behind in the web page that contains the ReportViewer:

protected void Page_Load(object sender, EventArgs e)

{

ReportViewer1.Visible =
false; ReportViewer1.ShowZoomControl = false;

ReportViewer1.ShowFindControls = false;

ReportViewer1.ShowDocumentMapButton = false;

}

protected void btnSearch_Click(object sender, EventArgs e)

{

      string txtTitle = txtSearch.Text;

      RenderReport(txtTitle);

}

protected void RenderReport(string txtTitle)

{

  try

    {

      ReportViewer1.ProcessingMode =
ProcessingMode.Remote;

      ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://localhost/reportserver");

      ReportViewer1.ServerReport.ReportPath = "/Employee/MyReport";

      List<ReportParameter> paramList = new List<ReportParameter>();

      paramList.Add(new ReportParameter("jobTitle", txtTitle, false));

      this.ReportViewer1.ServerReport.SetParameters(paramList);

     ReportViewer1.Visible = true;

}

     catch (Exception ex)

     {

     }

}

On the Page_Load event, I'm setting a few properties on the ReportViewer to hide some items we aren't interested in.

On the btnSearch_Click event, I'm grabbing the parameter value from the web page and calling my RenderReport method.

In RenderReport, I'm doing several things. First, I'm setting the processing mode to Remote. Second, I'm setting the location of the ReportServer. Third, I'm setting the location of the Report within the ReportServer (visible in ReportManager). This is the /<FolderName>/<ReportName>. Fourth, I'm packaging up the parameter for the report, and setting it to the report by calling the SetParamters method which will render the report.

Below is a screen-shot of what the final results should look like:

  That's it! A Reporting Services report being rendered through a ReportViewer Control on a web page!

Did you like this article?
This topic and many more will be covered
in my new Reporting Services class.

Check it out, this class is AWESOME!
http://www.endtoendtraining.com/rs.aspx

I also do on-site training for SSIS, SSAS, and SSRS
www.PragmaticWorks.com

Please visit my blog site at:

http://pragmaticworks.com/community/blogs/

 

 

Total article views: 8775 | Views in the last 30 days: 28
 
Related Articles
FORUM

Passing Parameters Between ReportViewers Controls

Passing Parameters Between ReportViewers Controls on two Seprate Asp.net Pages

FORUM

ReportViewer (web control) - parameter change refresh

ReportViewer (web control) - parameter change refresh

FORUM

missing parameter controls

ReportViewer does not return (perhaps does not receive) controls to set report parameters

FORUM

Reportviewer Control

Reportviewer Control - in Web application Error

FORUM

Exporting Report from reportviewer control throws error of Session timeout

Exporting Report from reportviewer control throws error of Session timeout

 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones