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.
type="Microsoft.Reporting.WebForms.HttpHandler,Microsoft.ReportViewer.WebForms, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
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).
Assembly="Microsoft.ReportViewer.WebForms, Version=188.8.131.52, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
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.
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 + "/" +
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
New ReportParameter(p.Name, Request.Params(p.Name))
Then, you need to set the parameters in paramList to the ReportViewer control with below code.
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)
Generate a PDF output file programmatically
Figure 1 - Report generated in Report Viewer from web application
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(
Response.Buffer = True
Response.ContentType = mimeType
"." + Request.Params("exportformat")
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.
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.