Friday, June 3, 2011

Server side web reports using Reportviewer

Scenario:


I had to show SSRS report in my web application. I have used the MicrosoftReportviewer 9.0 control and add little bit of code can make it happen.

Solution:


Its very easy to do it in few steps.

1. Add MicrosoftReportViewer control to the web form. This will add the control and necessary @ Register directives and assembly to the references.

2. Add the below code during any of the page events like load or Init.

        Before start the code add the namespace Microsoft.Reporting.WebForms

        a. Start with Reset command of the Reportviewer and set ProcessingMode as Remote.

        ReportViewer1.Reset();
        ReportViewer1.ProcessingMode = ProcessingMode.Remote;
        
        b. Create Uri based on the SSRS server name. 
        Uri ReportURL = new Uri("http://ServerName/ReportServer");


        c. Instantiate the ServerReport to assign the Report server and report path
        ServerReport sr = ReportViewer1.ServerReport;
        sr.ReportServerUrl = ReportURL;
        sr.ReportPath = "/ReportFileName";


        d. Define ReportParameter array to hold parameter to the report.
        ReportParameter[] rp = new ReportParameter[1];


        e. Create and assign parameter name and value. Repeat for all the parameters.
        rp[0] = new ReportParameter();
        rp[0].Name = "EmpNo";
        rp[0].Values.Add(Convert.ToString(Session["EmpNo"]));
        
        f. Assign Report parameter array to ServerReport
        sr.SetParameters(rp);


        g. Finally show the report and can manipulate the session timeout and zoom modes.
        ReportViewer1.ServerReport.Timeout = Session.Timeout;
        ReportViewer1.ZoomMode = ZoomMode.PageWidth;
        ReportViewer1.Visible = true;


Conclusion:


I believe this code will help you to show the SSRS reports in web form.

Happy Coding


No comments:

Post a Comment