Showing posts with label Microsoft Report. Show all posts
Showing posts with label Microsoft Report. Show all posts

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


Wednesday, March 3, 2010

Windows ReportViewer - Change rdlc report files during runtime

Problem

I had faced the problem of assigning different rdlc report files to a single Reportviewer. It always shows the report which opened first.

Solution
  1. Clear the datasources of the LocalReport or ServerReport in ReportViewer
  2. Reset the ReportViewer control in the beggining of the function.
  3. Do assign the datasources Name and Value. The Datasources name from the dataset as string and value.
  4. Finally add the datasource to the reportviwer datasource in localreport or serverreport.
Sample c# code snippet to solve this problem

this.reportViewer1.Reset();
this.reportViewer1.LocalReport.DataSources.Clear();
reportDataSource.Name = "DataSet_usp_EmpFoodCouponMonthly";
reportDataSource.Value = this.DataSet.usp_EmpFoodCouponMonthly;
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.RefreshReport();