Click here to Skip to main content
Click here to Skip to main content

Integrating Reporting Services 2005 Into a Web Application

By , 4 Sep 2006
 

Introduction

With release of Visual Studio 2005, Microsoft has made it much easier to integrate its SQL Reporting Services into ASP.NET web applications. Most desired functionality of Reporting Services can be achieved through its ReportViewer control, which is the focus of this article. For those of you who need very fine and detailed customization of embedding RS reports into applications, I would recommend that you seek out articles that discuss the RS object model and its soap interface. Otherwise, this article should provide you with a good tutorial on how to integrate Report Services into a web application using Visual Studio 2005.

Step-by-Step Instructions

Go ahead and start up Visual Studio 2005. Click on the “New \ New Website” item in the file menu. In the dialog box select the ASP.NET Website, your language preference (C# is used in this article), and where you would like your website to be built. Click on OK.

One of most difficult tasks in embedding the ReportViewer control is ensuring that it fills 100% of the space you want it to fill, no more, no less. You also want the ReportViewer to use its own scroll bars, not the browser’s.

The code I have written is basically a template that you can use which sets an area for a header, a custom control bar, and the report itself.

Since the ReportViewer has difficulty taking up 100% of the space in a table (probably not 100% XHTML), you are going need to remove any XHTML settings.

In the ASP.NET code, you will need to delete (or change) this line:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

You may also probably need to change the validation from a non-XHTML setting to a setting such as HTML 4.01.

validation

If your target audience will only be using the IE browser, change the body tag so it looks like this:

<body style="vertical-align: bottom; height: 100%; margin-right: 0px; 
    margin-top: 0px; margin-bottom: 0px; margin-left: 0px; 
    padding-bottom: 0px; padding-top: 0px">

If any of the users will also using the Mozilla browser, change the body tag so that the height is 99.3%. This is to avoid a double vertical scroll bar effect where you see both the ReportViewer’s scroll bar and the browser’s scroll bar. There will be an almost unnoticeable space at the bottom of Mozilla browser. Take note, that the contents within the ReportViewer may not display correctly (nor will they in Report Manager). So develop reports for Mozilla at your own risk at this time.

<body style="vertical-align: bottom; height: 99.3%; margin-right: 0px; 
    margin-top: 0px; margin-bottom: 0px; margin-left: 0px; 
    padding-bottom: 0px; padding-top: 0px">

Next go ahead and replace any existing code between the <form> tags with the below code:

<table style="vertical-align: bottom; border-width: 0px; margin-top: 0px; 
   margin-bottom: 0px; width: 100%; height: 100%; padding: 0px,0px,0px,0px;" 
   cellspacing="0" cellpadding="0">
    <tr>
        <td>
            HEADER
        </td>
    </tr>
    <tr>
        <td>
            <div style="border-top: black 1px solid; 
                background-color: #ece9d8; border-bottom-width: 1px; 
                border-bottom-color: #d4d0c8; padding-bottom: 10px;">
                Custom Controls
            </div>
       </td>
    </tr>
    <tr>
        <td style="height: 100%;">
            <%-- This is where the ReportViewer control will go. --%>
        </td>
    </tr>
</table>

Go ahead and switch to the “Design” view since it is now time to add the ReportViewer control.

In the “Data” section within the Toolbox, drag the ReportViewer control onto the last row of your table, which should be taking up most of the page. Next you want to set the control’s properties so that it points to the right server and the right report. Under properties, go to the “Server Report” section. Right above the section you should see a property called “ProcessingMode”. In most cases you are going to want use an instance of a Report Server. So for the purposes of this tutorial, set this property to “Remote”, even if the Report Server instance is on the same computer as your web app. Next, you want to set the “ServerReport” settings.

First, type in address of the Report server in the “ReportSeverUrl” field.

The syntax is:

http:// NameOfServer / reportserver

i.e. http://ServerOne/reportserver

Next type in the location of the report in the “ReportPath” field.

i.e. /MyReports/TheReport

The location is easy to know since it is the same path structure that you see in the Report Manager. Also, always be sure the put a forward slash first before the actual path.

Properties

Unless you want the ReportViewer to be an absolute size, go ahead and set the height and width settings under the “Layout” section to be 100%. The ReportViewer will by default show controls to set the parameters for the report. These often look a little ugly, and I would recommend most developers to create their own parameter controls. This can be done in the control bar row of the template provided above. To get rid of the ReportViewer’s parameter controls set the “ShowParameterPrompts” in the properties underneath “Appearance” to false. In, the next section I’ll show how you can set the report parameters with your own code.

Those should be all the settings you need to make for the ReportViewer to display correctly, unless your version of Visual Studio has other default settings. Double check to make sure the ASP.NET looks similar to this code:

<rsweb:ReportViewer ID="ReportViewer1" runat="server" Height="100%" 
    ProcessingMode="Remote" ShowParameterPrompts="False" Width="100%">
    <ServerReport ReportPath="/MyReports/TheReport" />
</rsweb:ReportViewer>

Now, the report is ready to be processed. You can go ahead and preview your website and the report should generate just fine.

Displaying a Report through the C#/VB Code

In many cases, you may need to pass parameters to a report or your code (or the end users) may need to have control over which report is generated. For example you may have a Drop-Down-List that lets the user select which report they wish to view. They can then click on a button that generates the report that accepts a parameter for the current time. (The Button and Drop-Down-List can be placed in the custom control bar section of the template.)

So the code inside the “Click-Button-Event-Handler” function is where we will need to put in our custom ReportViewer code. The code in this article is C#, but the VB code shouldn’t be too much different.

First, if you didn’t set the Report Server Url via the visual designer, you can do it here.

this.ReportViewer1.ServerReport.ReportServerUrl = 
    new System.Uri("http://ServerOne/reportserver");

One problem/bug that is rather common in the ReportViewer control is when you change to a different report via the website itself coming from a drill-through report, an ASP.NET error can occur. This is because the ReportServer is thinking that the new report is still a drill-through when it is not. To get around this problem use the following code:

while (this.ReportViewer1.ServerReport.IsDrillthroughReport)
{
    this.ReportViewer1.PerformBack();
}

This code will set whichever report you want to display next to be the parent.

Next you want to set the new report path:

// Could also be set to the selection of a ListBox.
string strReport = "/MyReports/TheReport";
this.ReportViewer1.ServerReport.ReportPath = strReport;

If parameters are needed, you are going need to first declare an array of ReportParameter objects.

Microsoft.Reporting.WebForms.ReportParameter[] RptParameters = 
    new Microsoft.Reporting.WebForms.ReportParameter[1];

In this case, we only need one parameter.

Next, assign the first index (0) of the RptParameters array to a new ReportParameter object where the constructor function takes the syntax:

ReportParameter(ReportParameterName, Value);

i.e.

string strTime = System.DateTime.Now.ToShortTimeString();

RptParameters[0] = 
    new Microsoft.Reporting.WebForms.ReportParameter("CurrentTime", strTime);

Then, you need set the RptParameters object to the ReportViewer control.

this.ReportViewer1.ServerReport.SetParameters(RptParameters);

Finally, to put these changes into action, call the Refresh() function:

this.ReportViewer1.ServerReport.Refresh();

So in its entirety, the code inside the button-clicked event-handler function should look like this:

this.ReportViewer1.ServerReport.ReportServerUrl = 
    new System.Uri("http://serverone/reportserver");

while(this.ReportViewer1.ServerReport.IsDrillthroughReport)
{
    this.ReportViewer1.PerformBack();
}

// Could also be set to the selection of a ListBox.
string strReport = "/MyReports/TheReport";
this.ReportViewer1.ServerReport.ReportPath = strReport;
Microsoft.Reporting.WebForms.ReportParameter[] RptParameters = 
    new Microsoft.Reporting.WebForms.ReportParameter[1];

string strTime = System.DateTime.Now.ToShortTimeString();
RptParameters[0] = 
    new Microsoft.Reporting.WebForms.ReportParameter("CurrentTime", strTime);

this.ReportViewer1.ServerReport.SetParameters(RptParameters);
this.ReportViewer1.ServerReport.Refresh();

If you like you can delete the ReportPath from the properties in the visual designer. This will ensure that a report does not load until your own code loads a report itself. However, this will also remove the ReportViewer control bar, which you may wish to keep so that the website looks more structured. What I usually do is set the default ReportPath to point to a report that is basically a welcome screen. From there, the end user can choose parameters from my selection controls, and then when ready, the user can click on a view button that generates the desired report.

Conclusion

The ASP.NET code I have provided is code that I have used myself in different projects. Although I am personally more focused on software programming than web design, I do try to make my web pages 100% XHTML. Unfortunately, I was unable to completely stick to that standard because of the ReportViewer’s own HTML coding. If anyone has a 100% XHTML solution, which allows the DOCTYPE tag to be set to at least XHTML transitional, that can display RS reports sized to 100% inside a table, and allow the ReportViewer control to use its own scroll bars, I would be glad to revise this article. Otherwise, the template I have provided, should allow you to embed a nicely displayed RS report in a web page that can be process by the IE-based browsers .

Furthermore, this is only a solution for the IE browser. The layout template will work fine with Mozilla, but the ReportViewer's own HTML coding will not. This, of course, may change with new versions of the Mozilla browser.

If you wish deviate a little from this article in areas such as showing the ReportViewer parameters prompts, then the website may not show visually want you want. However, it should be easy enough to take my HTML code and tweak it (i.e. adding/removing border lines) so that it suits your needs.

As for the C# code, the only thing that may be confusing is the while loop to drill back to the parent report. Seemingly the Refresh() function should do this automatically, and yet, it will only give you an ASP.NET error. Hopefully, this will be fixed in later versions of the ReportViewer control, but for now the loop is a good go-around for this problem.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

DDarren
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to subscribe the server report through code?memberPrashant.IGT15 Dec '08 - 22:05 
Hi DDarren,
 
I have uploaded several reports on the Report server and calling them from via web application (ASP.Net with VB.Net).
As you know about one of a great feature of Report server is to subscribe the report. I want to give this feature also to the user of this applications.
So is this possible to code in such a way that a user can subscribe report according to his/her schedule?
 
Please reply.
 
Thanks
 
Prashant kumar
GeneralUsing IFramememberNoman Aftab14 Jul '08 - 18:53 
<iframe id="ReportFrame" runat="server" frameborder="0" scrolling="auto" width="100%" height="490"></iframe>
 
Set the src attribute in the code behind:
 
ReportFrame.Attributes["src"] = "http://pcName/ReportServerName?%2fReportPath/ReportName&amp;rs:Command=Render";
 
Noman Muhammad Aftab,
Software Mechanic
GeneralRe: Using IFramememberMember 209783428 Mar '09 - 1:53 
Hi,
I'm facing a problem under IE6.
 
I use IFrame and set the src path programmaticcaly.
My problem: I don't want the toolbar so I put rc:Toolbar = false, in IE6, the image of my report is not load.
I've no image. I haven't this problem under Firefox.
 
If I set the Toolbar at true, the report is rendered good under IE6.
 
How can I render the report in an iframe without the toolbar under IE6 ??
Generalsame code in VB.net doesn't workmemberlbearn10 Jul '08 - 8:01 
I tried the code and it works fine. When I translated it into VB.net, it doesn't work.
I got error message on this line: ReportViewer1.ServerReport.SetParameters(RptParameters)
The error message is: Value cannot be null.
 
Can anybody see anything wrong in the VB.net version?
 
This is the same code in VB.net:
 
ReportViewer1.ServerReport.ReportServerUrl = New System.Uri("http://myServer/ReportServer")
 
While (ReportViewer1.ServerReport.IsDrillthroughReport)
ReportViewer1.PerformBack()
End While
 
Dim strTerminal As String
strTerminal = DropDownList1.SelectedValue
' Could also be set to the selection of a ListBox.

Dim strReport As String
strReport = "/TIMStest/mainReport"
ReportViewer1.ServerReport.ReportPath = strReport
 
Dim RptParameters(1) As Microsoft.Reporting.WebForms.ReportParameter
RptParameters(0) = New Microsoft.Reporting.WebForms.ReportParameter("terminal", strTerminal)
 
ReportViewer1.ServerReport.SetParameters(RptParameters)
ReportViewer1.ServerReport.Refresh()
GeneralRe: same code in VB.net doesn't workmemberDominic Fitzpatrick22 Oct '09 - 4:12 
I doubt you are monitoring this any more but as I just ran into the same problem.
 
Change:
Dim RptParameters(1) As Microsoft.Reporting.WebForms.ReportParameter
RptParameters(0) = New Microsoft.Reporting.WebForms.ReportParameter("terminal", strTerminal)
 
To:
Dim RptParameters(0) As Microsoft.Reporting.WebForms.ReportParameter
RptParameters(0) = New Microsoft.Reporting.WebForms.ReportParameter("terminal", strTerminal)
 
RptParameters(1) creates an array with two elements and since only one is added, the second is null, which raises the exception. This is probably sooooooo obvious to many but I have just started trying VB.net (and VB in general). I spotted it in the VWD Express debugger.
QuestionEvent Handlers for Server Reports?memberLarry Whipple11 Apr '08 - 11:35 
Smile | :) First,
Thanks! You saved my bacon. I have built a relatively simple web-based report viewer and was going along fine until I drilled into a report. When I tried to to reassign the ReportPath to show another top-level report I got the "in this state" error and was pulling hair out! Thanks again for finding that. I also now know that I can't assign the ServerReport to a variable and check that after running the PerformBack. As long as I fully qualify the ReportViewer and don't try to assign any portion of it, it works just fine!
I do have another question though. Is there a way to plug into the event handler when the reports are running server-side? I want to capture the drilldown request and be able to do some client-side updates based on the new report (I'm building a breadcrumb control).
It appears I should be able to get to the event stack by setting up my own handler - ala...
 
ReportViewer.Drillthrough += new DrillthroughEventHandler(MyDrillthroughEventHandler);
 
But it doesn't seem to get hit. Suggestions, or am I just missing something simple?
 
I've also been told that most of the reports were designed as Subreports but the event stack for the SubreportProcessing appears to be all clientside (ReportViewer.LocalReport.SubreportProcessing).
 
Any suggestions of how to wire into these two event stacks and other resources I could review for additional enlightenment would be greatly appreciated.
 
Thanks!
 
Larry
GeneralSql Server 2005 Reporting Services in ASP.NET with C#memberShankar Naspuri19 Mar '08 - 20:12 
Hi,
i am new to ''Sql Server 2005 Reporting Services''
how to Create the Reporting Services in Asp.net 2.0 with C#.(Please Clear Explanation with Screen Shots)
i am using the asp.net 2.0 ,sql server 2005 with SP2 in XP.
Please help me on this(Urgent)
 
shankar Naspuri
 
Shankar
 
shankar_srinu@hotmail.com

GeneralDOCTYPE TagmemberIT2666 Nov '07 - 16:13 
Removing DOCTYPE Tag does fix the problem of not taking up 100% of space in a TD but it seems to affect AJAX modal pop up to display propably. Is there any workaround?
GeneralError passing parametermemberBigBlueEye13 Jun '07 - 7:39 
Thanks for the article.
 
I've created a SSRS report that take a parameter. When I run the .rdl it works great. I select the value from the parameter drop-down and it works great. Now I'm trying to build a web client to run the report but my code below says the parameter doesn't exist (line 9). I've verified that the parameter value that I pass in from the drop-down list (line 2) has the correct value.
 
Please help a noob! Thanx!
 

 
1. Protected Sub lnkRun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkRun.Click
 
2. Dim intPortalId As Integer = DropDownList1.SelectedValue
 
3. ReportViewer1.ServerReport.ReportServerUrl = New Uri(ConfigurationManager.AppSettings("Key_ReportServer"))
 
4. ReportViewer1.ServerReport.ReportPath = "/AuditsReports/VendorsStatus"
 
4. ReportViewer1.ShowToolBar = False
 
6. ReportViewer1.ShowParameterPrompts = False
 
7. Dim params(0) As ReportParameter
 
8. params(0) = New ReportParameter("PortalId", intPortalId, True)
 
9. ReportViewer1.ServerReport.SetParameters(params) >> ERROR HERE: Parameter 'PortalId' does not exist on this report <<
 
10. ReportViewer1.ServerReport.Refresh()
 
11. End Sub

GeneralRe: Error passing parametermemberDDarren16 Jun '07 - 15:19 
I almost never program in VB, but it looks like you're not instantiating the array.
 
'Create an array of ReportParameter objects with one element
Dim params() as ReportParemeter = new ReportParameter(0)
'take the first element and assign it a ReportParameter object
params(0) = New ReportParameter("PortalId", intPortalId, True)
 
I think that was the problem, let me know if it works.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 4 Sep 2006
Article Copyright 2006 by DDarren
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid