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.
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).
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
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.
Now, get all report parameters of report in variable Paramlist.
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.
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)
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.
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.