Home   |  Articles   |  Resources   |  Humor   |  Feedback       

  Login   Register 

Ads Via DevMavens


SQL Reporting Services and the ReportViewer Component

Posted by on Wednesday, May 19, 2004

Using ReportViewer and URL Access to embed a Reporting Services Report in an ASP.Net Web application

If you are using Reporting Services, the chances are sooner or later you’re going to need to invoke a Report from an ASP.Net Web application. In this article, we’re going to look at how to do this using URL Access. Fortunately for us, Microsoft has made this really easy and provided us a sample Report Viewer control. This is a Server Control which uses URL access to render reports within a web browser.
Both C# and VB versions of the source code are available, and if you used all the default installation options, they are installed under in C:\Program Files\Microsoft SQL Server\MSSQL\Reporting Services\Samples\Applications\ReportViewer\vb and C:\Program Files\Microsoft SQL Server\MSSQL\Reporting Services\Samples\Applications\ReportViewer \cs respectively. Open up the solution file ReportViewer.sln, (in your language of choice) in  VS.Net and build the application. Now we are ready to create our web application using the ReportViewer Control. Create a new ASP.Net Web application project. This can be done in either VB or C#.
 

The next step after creating our project, is to add a reference to the ReportViewer.dll we created in the earlier step. To do this, click Tools --> Add/Remove Toolbox Items. In the Customize Toolbar dialog box click the .NET Framework Components tab, browse to the location where you created ReportViewer.dll and click open. The Report Viewer component has now been added to your toolbox and you can drag it into your web form just like you can any other control in the toolbox.

Having dragged the Report Viewer Control into our web form, you will change some of its properties. To do this, right on the control and click on the Properties tab. For the purpose of this article I am going to embed the Sample Reports provided by Microsoft within this web form. I have already opened the SampleReports solution, built it, and deployed it to my Report Server which in my case is on the same computer.  I set the ReportPath property to my Report Server path ‘http://localhost/reportserver’  and the ReportUrl property to point to the Report I want to embed, in this case the Company Sales report ‘Samples/Company Sales’ It also makes sense to change the width and height properties of the Report Viewer control so that the report has sufficient real-estate in which to render. Compile and run the ASP.NET Web application to render the report in your Web browser.
Let’s take a look at what our ASPX page looks like

<body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
     <cc1:ReportViewer id="ReportViewer1"
             runat="server" ReportPath="/SampleReports/Company Sales"
             ServerUrl="http://localhost/ReportServer"
             Width="95%" Height="95%">
      </cc1:ReportViewer>
   </form>
</body>

The Company Sales Report has no input parameters so we are going to change our code to render the FoodMart sales Report which does have parameters.

<body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
     <cc1:ReportViewer id="ReportViewer1"
             style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 56px"
             runat="server" ReportPath="/SampleReports/FoodMart Sales"
             ServerUrl="http://localhost/ReportServer"
             Width="95%" Height="95%">
      </cc1:ReportViewer>
   </form>
</body>


If we leave our code as is, the report will render using the default parameter for ‘Product Family’ namely ‘Drink’. However we can also specify a different parameter

<body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
      <cc1:reportviewer id="ReportViewer1" 
              runat="server"

              Height="95%" Width="95%"
              ServerUrl="http://localhost/ReportServer"
              ReportPath="/SampleReports/FoodMart  Sales&amp;

rs:Command=Render&amp;

rc:Parameters=false&amp;ProductFamily=Food">
    </cc1:reportviewer>
  </form>
</body>

Now if you run this application, you will see that it renders using ‘Food’ as the default parameter.

The next step is to how to specify this in code. Our web form, may display Reports based on User selection, or specify report parameters based on user access level. Lets look at another example

<body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
      <cc1:reportviewer id="ReportViewer1"
               runat="server" Height="95%" Width="95%" >
    </cc1:reportviewer>
  </form>
</body>

In this case we have not specified the Report or the location of the Report Server in the ASPX page. Our code behind file looks something like this

protected Microsoft.Samples.ReportingServices.ReportViewer ReportViewer1;
private void Page_Load(object sender, System.EventArgs e)
{
  // Put user code to initialize the page here
 }

We can put the following code, in our Page_Load method.

ReportViewer1.ServerUrl="http://localhost/ReportServer";
ReportViewer1.ReportPath="/SampleReports/Sales Order Detail";

Compile and run the application and the Sales Order Detail Report will render using the default Sales Order Number ‘SO8436’. Well the next obvious question is, what if we want to set another Sales Order Number in our code behind file.
Its worthwhile at this point to take examine and get atleast basic familiarity with the code in the ReportViewer sample. I am not going to go through it here, but I am going to add another method to ReportViewer Server Control  – SetQueryParameter

  public void SetQueryParameter(string ParamName, string ParamValue)
    {
  SetParameter(ParamName,ParamValue);
    }

 Rebuild ReportViewer.cs, and now we can set parameters in our web page. Add the following to the Page_Load method of your ASPX form.

ReportViewer1.SetQueryParameter("SalesOrderNumber","SO5018")

Now when your run the application the Sales Order Report will render using ‘SO5018’ as the Sales Order Number.

There are many other properties you can set using both the VS designer and through code, and you can check them out at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rsprog/htm/rsp_prog_urlaccess_959e.asp. Hope this helps those of you who are starting of with reporting services understand the basics of how to embed a Report in a web application using URL access.


Update: Since there have been so many email requests we have recieved about this article - we have added another one detailing how to embed the Report Viewer in a Web form

 

by Poonam Lall


Comments:

please help me
By zsww on 5/31/2004
the code:
public void SetQueryParameter(string ParamName, string ParamValue)
{ SetParameter(ParamName,ParamValue); }


write where?

please help me
By zsww on 5/31/2004
the code:
public void SetQueryParameter(string ParamName, string ParamValue)
{ SetParameter(ParamName,ParamValue); }

write where?

my code: (but is wrong)

protected Microsoft.Samples.ReportingServices.ReportViewer ReportViewer1;
public void SetQueryParameter(string ParamName, string ParamValue)
{ SetParameter(ParamName,ParamValue); }
private void Page_Load(object sender, System.EventArgs e)
{
ReportViewer1.ServerUrl="http://localhost/ReportServer";
ReportViewer1.ReportPath="/SampleReports/Sales Order Detail";
ReportViewer1.SetQueryParameter("SalesOrderNumber","SO5018");
}

SetQueryParameter method
By poonam on 5/31/2004
You need to add the method to the ReportViewer Server control - ReportViewer.cs is this case or its VB counterpart if thats what you are using

thanks for your help
By zsww on 6/1/2004
i have resolved the problem.

1.but i want to know: if i have 2 or 3 parameters,like http://localhost/a.aspx?year=2004&month=6,i write below code(and i have set 2 or 3 parameters in the rdl) is write?

private void Page_Load(object sender,System.EventArgs e)
{
string ParamYear=Request["year"];
string ParamMonth=Request["month"];

this.ReportViewer1.ServerUrl="http://localhost/reportserver";
this.ReportViewer1.ReportPath="/upholster reports/Measure";

this.ReportViewer1.SetQueryParameter("Year",ParamYear);
this.ReportViewer1.SetQueryParameter("Month",ParamMonth);
}


2.when i write in the .cs code like this:
this.ReportViewer1.Parameters=?(i dont know what to write);
this.ReportViewer1.ToolBar=?

and i dont know if can i write this? or only set this in the property page of the ReportViewer control(Default,false,true)?

printing from report viewer
By alexmihai on 6/10/2004
Hello!

What about printing the report from the report viewer?

Thanks,
Alex

customizing toolbar
By alexmihai on 6/10/2004
And another question: It is possible to customize the toolbar? If not, how can I pass the find, zoom, export parameters to the report from my own toolbar? The main reason I can not use the standard toolbar is that the text within should be localized.
Thanks again.

URL-access
By Tih on 6/11/2004
Hi
This question might be slightly out of the context in this article but I try anyway!

I want to run a report using URL-access and issues the following statment :
http://sfkdw/ReportServer?%2fSFK+Indk%c3%b8bsrekv%2fRekvisition&REKVNR=175440&SPROG=0&rs%3aCommand=Render&rs%3AFormat=PDF

which i OK. The output is rendered as an PDF-file. My problem is that i cant find any command where i can set the name and directory of the output-file in the URL-statement. The user has to take action and manually name and save the PDF-file. My intention is that there should be absolutely no user-dialog. The output file should be named and saved automaticly.

Thanks

Thomas

Printing and Customizing the ToolBar
By poonam on 6/11/2004
hi alexmihai,

There is no direct way to print a report, you can however export it to pdf or tiff etc., and print from there. Another solution, Reporting Services is totally extensible so you could write your own printing extension. Personally I'm not sure whether the time and effort would be worth it.
As of now the Toolbar cannot be customized. SP1 which is scheduled to be released later this month is going to have some toolbar customization - http://odetocode.com/Blogs/scott/archive/2004/05/31/266.aspx. You also might want to check out http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rsadmin/htm/drp_deploying_v1_0vaa.asp - it has alot of good information regarding localization

RE: URL-access
By poonam on 6/11/2004
Hi Tih,

Being able to push a file directly from the server to a client's hard drive is a security risk, so unless the user takes some action on thier own you cant do this in a web application.

You could write a control that the user would download and trust,(ActiveX or .NET) that uses the web service API of Reporting Services to generate the PDF and save it to a file or write a win forms app.

Question for ReportPath
By jessie on 6/16/2004
I am new to report service and have question like to ask you help. Thank you in advance!

Is ReportPath = projectname/drl filename
I try to use sample example/reportviewer to access the report(want to the same thing this article - "SQL Reporting Services and the ReportViewer Component" do,
when I type Reportpath="/samplereports/company sales" it works fine.

But for all the other report, for example FoodMart Sales reprot, when I type ReportPath="/sampleReports/FoodMart Sales", it returns "The item '/SampleReports/FoodMart Sales' cannot be found. (rsItemNotFound)"

Vb version of code required
By rajeshkrao on 7/8/2004
Can you provide VB part code for below module
SetQueryParameter()
SetParameter()
BuildUrlString()

SQL Server Reporting Services
By afshinlee on 8/10/2004
Hi everybody
I work on a .Net project with C#
and I have some report in my project(using
Reporting Services)
I want to add a Assembly Reference and then a Class
in to my report in References Tab
my class is simply:

using System;

namespace ClassLibrary1
{
public class Class1
{
public Class1()
{
}
}
}

when I compile my project I recive Error Message
"error in loading module ClassLibrary1"

please help me.
Afshin

I also want to know answer of your question.
By gaurav on 8/12/2004
I also want to know answer of your question.

Reporting services.
By hemant on 9/6/2004
The SetQuery parameter is defined in .cs file but still it giving the error "report viewer does not contain a definition of SetQueryParameter" what cud be the reason.

Cant make the SetQuerryparameter work
By paulghorra on 9/24/2004
HI All,
I have added the code for SetQuerryparameter in the method for the report viewer, i v rebuild the project and added the new viewer to my project, i have tried it in VB and donloaded the C# version you provide, and whene ever i go to the behind code and try to call it it doesnt work it is not in the list
Can some one please Help.
Thanks,
Paul

Automatic Logon to view Report in ReportViewer
By chris_lasater on 11/23/2004
What is the command to send username and password to the report during runtime? Or what is the best way to authenticate and send authentication information like username and password?

Query In Article
By alvinajm on 1/9/2005
Hi !

I came across your article but there's this portion which i was not sure .

ReportPath="/SampleReports/FoodMart Sales& rs:Command=Render& rc:Parameters=false&ProductFamily=Food">

I dun understand the meaning behind these 3 lines of codes. Hope there's someone who can enlighten me. Thanks .

Regards
Alvin

Answer
By a222493 on 1/24/2005
Has this been answered?

Authentication
By fen911 on 3/8/2005
I've noticed you can set authentication using "Credentials" within the web service method (provided with the ReportExplorer solution). However, it appears this is only being used to access the file / folder structure and the same code from ReportViewer is used to render the reports. Is there a way (within ReportViewer) to set the authentication for viewing the reports? To save maintenance, we are hoping to use our existing application security layer, which in turn, calls all reports using the application generic logon.

VB versions
By scott on 3/23/2005
I hope to someday have VB versions of the code - but I rarely have the time to port things. If you search on the web you'll find some converters.

Copyright 2004 OdeToCode.com 


The Blogs
Subscribe to the OdeToCode blogs for the latest news, downloads, new articles, and quirky commentary.
New Articles
Databinding in Silverlight
This article will cover data binding features in Silverlight, including binding expressions, validation, converters, and binding modes.

The Standard LINQ Operators
This article will cover the standard LINQ operators provided by LINQ for filtering, grouping, joining, converting, projecting, and more.

C# 3.0 and LINQ
C# 3.0 introduced a number of new features for LINQ. In this article we'll examine the new features like extension methods, lambda expressions, anonymous types, and more.

Most Popular Articles
Table Variables In T-SQL
Table variables allow you to store a resultset in SQL Server without the overhead of declaring and cleaning up a temporary table. In this article, we will highlight the features and advantages of the table variable data type.

ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
MasterPages are a great addition to the ASP.NET 2.0 feature set, but are not without their quirks. This article will highlight the common problems developers face with master pages, and provide tips and tricks to use master pages to their fullest potential.

AppSettings In web.config
In this article we will review a couple of pratices to keep your runtime configuration information flexible.

Contribute Code
Privacy
Consultancy