Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
SSRS 3
MSBI 3
BI 1
SQL Server 1
ASP.NET 1
#SSRS 1
#DOTNET 1
Reporting 1
Report Viewer 1
#BI 1

Archive · View All
July 2010 1
March 2010 1
April 2010 1

Generating and exporting SSRS reports programatically using Report Viewer Control

Jul 9 2010 1:09AM by Viral   

I have picked very simple but useful topic here. Clients usually want to generate pdf / Excel file direct from web / desktop application.

Introduction to Report Viewer

“Reportviewer” is good tool to generate report file on fly in pdf / excel format. I have tried here with asp.net web application.

Requirement : Here, requirement is like this. Web application should have invoice list. There would be button against each button and that button’s click event would generate invoice in PDF form.

Solution : I have created one SSRS project, created report named Invoice.rdl and deploy it on my local report server and I would call same page from my application and generate report file. I have narrated this solution below.

Make changes in Web.config related to web.config :

First, I need to use ReportViewer control in web application to generate report. Report viewer control includes an HTTP handler , which needs to be used to display images or to export report . So first we need to add below code in web.config in httphandler.

<httpHandlers>
	<add 
		verb="*"  
		path="Reserved.ReportViewerWebControl.axd"             
		type="Microsoft.Reporting.WebForms.HttpHandler,Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</httpHandlers>

Add Report Viewer in application : Now, in this web application, I have page having invoice list with “Print Invoice” button against each invoice. I need to create one aspx file say “reports.aspx” , which generate PDF file for me based on parameters (Instead of, to write whole code on button’s click event, I have opted new page to make it generic)

Now, I had opened this page “reports.aspx” and then look in the Toolbox window, under the Data section, for the ReportViewer control. If you do not see the control, you can download , reference it and can use it. Either I can drag control from toolbox and place in my aspx page or need to add the following HTML to the “reports.aspx” (need to register control and add it in page).

<%@ Register 
	Assembly="Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" 
    TagPrefix="rsweb" 
%>
<rsweb:ReportViewer 
	ID="ReportViewer" 
	ShowPrintButton="false"
	runat="server"  
	Height="600px" 
	Width="100%">
</rsweb:ReportViewer>

Now, what I need to do is that, in page of invoice list, I need to call this “reports.aspx” (on click of print invoice button) and I will pass required values (e.g. InvoiceID, reportname, required file type, like PDF here) in query string. You can also go for different options like hiddenfield or session etc.

I need to work on code behind of reports.aspx. First of all I need to import reporting.webforms library.

import Microsoft.Reporting.WebForms  

On page load event of this reports.aspx, I need to write script to run report and generate PDF from report server on fly. I have assigned reportname and reportServerURL below

Dim ReportServerUrl As String
Dim ReportName As String
Dim ReportProject As String
        
ReportName = Request.Params("ReportName")
ReportServerUrl = "http://localhost/ReportServer"
ReportProject = "Report Project1"

See here, for reportname, I have passed value in query string from my invoie list page. ReportProject is folder where, my report exist (its SSRS project name, so when you deploy it on reportserver, it makes folder with same name)

I have used variable ReportProject and ReportName to set property of ReportPath like below.

ReportViewer.ServerReport.ReportPath = "/" +
                          ReportProject + "/" + 
                          ReportName

Now, get all report parameters of report in variable Paramlist.

Dim paramList As Generic.List(Of ReportParameter)
paramList = New Generic.List(Of ReportParameter)

Now, I need to set all report parameters values from querystring values. I will get all parameters from report using “GetParameters()” and set value from query string to parameters.

Dim pinfo As ReportParameterInfoCollection
ReportParameterInfoCollection = paramList.GetParameters()
For Each p As ReportParameterInfo In pinfo
   paramList.Add(
      New ReportParameter(p.Name, Request.Params(p.Name))
   )
Next

Then, you need to set the parameters in paramList to the ReportViewer control with below code.

Me.ReportViewer.ServerReport.SetParameters(paramList)

Just make sure here that If you have sharehosting or windows authentication is disable, you must need to use “ReportServerCredentials()”to pass your cretdential info , other wise , you might get error like “The request failed with HTTP status 401: Unauthorized“. but I have excluded same in this post here just to make it simple currently.

Above code has set parameters for invoice report, so "http://<yourDomainName>/reports.aspx?orderID=<@OrderID>&ReportName=<@ReportName>" would display report viewer and user can export this invoice in required format e.g. pdf / csv / excel etc. (Figre 1)

SSRS Report Viewer example
Figure 1 - Report generated in Report Viewer from web application

Generate a PDF output file programmatically

We can also offer another option like, not to show report viewer and direct open invoice file in specific format like pdf / csv / excel, I needed to use “render” method of report viewer.

Dim returnValue as byte()
Dim mimeType As String = ""
Dim returnValue As Byte()
Dim encoding As String = ""
Dim streams As String()
Dim warnings As Microsoft.Reporting.WebForms.Warning()


If Request.Params("exportformat") <> "" Then
    returnValue = ReportViewer.ServerReport.Render(
            Request.Params("exportformat").ToString,                   
            Nothing, 
            mimeType, 
            Encoding,                  
            Request.Params("exportformat").ToString,                 
            streams, 
            warnings)
    Response.Buffer = True
    Response.Clear()

    Response.ContentType = mimeType

    Response.AddHeader(
        "content-disposition", 
        "attachment;filename=" + 
             Request.Params("ReportName") + 
             "." + Request.Params("exportformat")
        )

    Response.BinaryWrite(returnValue)
    Response.Flush()
    Response.End()
End If

So, now as I will click on print button in my application page, it should redirect page to: “http://<yourDomainName>/reports.aspx?orderID=<@OrderID>&ReportName=<@ReportName>&exportformat=<@FileExtension>” and this page would show modalDialogBox of “File Download” ask user for option either to save it or open it (Fig. 2). I have passed here in exportformat=pdf, but I can pass "csv" or "excel" file format as per requirement here. You can now generate report from anywhere in your web application by calling this page with required parameters.

SSRS save as dialog for PDF output
Figure 2 - Generate PDF using report viewer from application.

I will come up with same topic, but along with detail related to pass credential info to report server in some next post.

Tags: BRH, MSBI, SSRS, DOTNET, #BI, Report Viewer, Reporting, #DOTNET, #SSRS, ASP.NET, SQL Server, BI,


Viral
276 · 0% · 157
2 Readers Liked this
Jacob Sebastian Liked this on 1/3/2012 6:25:00 AM
Profile · Blog · Facebook · Twitter
Himani Liked this on 3/14/2012 4:40:00 AM
Profile
2
Liked
 
0
Lifesaver
 
 
0
Learned
 
0
Incorrect



Submit

2  Comments  

  • Can we achieve this without using Report Viewer ?

    commented on Jul 24 2013 8:11AM
    deshmukhshivkumar
    3057 · 0% · 2
  • Hi Viral, I have created some reports in SSRS 2008 and deployed on server. Now there is need to show those reports on webpage. I am trying Report Viewer control in Visual studio 2010 in order to show SSRS reports on web. But All SSRS reports are parameterized reports meaning that they are getting populated on parameter selection only. Now I am facing problem to handle the parameters when using report viewer control. Please provide anything regarding this if u can, any help would be appreciated.

    commented on Aug 12 2013 4:29AM
    Nilesh
    2834 · 0% · 3

Your Comment


Sign Up or Login to post a comment.

"Generating and exporting SSRS reports programatically using Report Viewer Control" rated 5 out of 5 by 2 readers
Generating and exporting SSRS reports programatically using Report Viewer Control , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]