Combining ASP.Net MVC With SQL Server Reporting Services

Introduction

There is no control for Report Viewer in MVC, so we cannot view a report using MVC applications. Most business applications have report requirements. In article, I will explain the work around to make it happen. Here I will assume that we already have a report server with some test report.

Report server folder structure:

                              test report

MVC does not provide a Report Viewer control so we need to add a classic web form page to our MVC application. The recommendation is to place this form outside of the view folder so that we don't need to register or ignore the route.

                              Report viewer

In this classic web form page, we need to add the following two controls:

  1. Script manger
  2. Report Viewer control

ReportViewer.aspx code

  1. <form id="form1" runat="server">  
  2.     <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>  
  3.     <div>  
  4.         <rsweb:reportviewer id="rptViewer" runat="server" height="503px" width="663px">  
  5.         </rsweb:reportviewer>  
  6.     </div>  
  7. </form>  
The following rocedure is required to write a report render function: 
  1. Set processing mode to remote.
  2. Pass report server URL and report path.
  3. Pass the parameters if any.
  4. Pass Credential of Report Server if required.
  5. Refresh the report.

The following is the show report function:

  1. private void ShowReport()  
  2. {  
  3.     try  
  4.     {  
  5.         //report url  
  6.         string urlReportServer = "http://Servername/Reportserver";  
  7.   
  8.         // ProcessingMode will be Either Remote or Local  
  9.         rptViewer.ProcessingMode = ProcessingMode.Remote;   
  10.   
  11.         //Set the ReportServer Url  
  12.         rptViewer.ServerReport.ReportServerUrl = new Uri(urlReportServer);   
  13.   
  14.         // setting report path  
  15.        //Passing the Report Path with report name no need to add report extension   
  16.         rptViewer.ServerReport.ReportPath = "/Jignesh/TestReport";   
  17.   
  18.          //Set report Parameter  
  19.         //Creating an ArrayList for combine the Parameters which will be passed into SSRS Report  
  20.         //ArrayList reportParam = new ArrayList();  
  21.         //reportParam = ReportDefaultPatam();  
  22.   
  23.         //ReportParameter[] param = new ReportParameter[reportParam.Count];  
  24.         //for (int k = 0; k < reportParam.Count; k++)  
  25.         //{  
  26.         //    param[k] = (ReportParameter)reportParam[k];  
  27.         //}  
  28.   
  29.         // pass credential as if any... no need to pass anything if we use windows authentication  
  30.         //rptViewer.ServerReport.ReportServerCredentials =   
  31.         //  new ReportServerCredentials("UserName", "Password", "domainname");  
  32.   
  33.         //pass parameters to report  
  34.         //rptViewer.ServerReport.SetParameters(param);   
  35.         rptViewer.ServerReport.Refresh();  
  36.     }  
  37.     catch (Exception ex)  
  38.     {  
  39.         throw ex;  
  40.     }  
  41. }  
In the page load event of the Report Viewer page, call the preceding function to render the report. In this example, we are passing the hardcoded report name. Instead of this we can pass the report name using any state management technique.

Report Viewer page Code:
  1. public partial class ReportViewer : System.Web.UI.Page  
  2. {  
  3.    protected void Page_Load(object sender, EventArgs e)  
  4.    {  
  5.       ShowReport();  
  6.    }  
  7. }  
Now we need to call this view from our controllers. For that I have created an ActionResult and called it from the menu.

Controller Code:
  1. public ActionResult ShowReport()  
  2. {  
  3.    return Redirect("../ReportViewer/ReportViewer.aspx");  
  4. }  
Output:

Output

Summary

Using this work around, we are able to view a SSRS report in a MVC application. I hope you enjoy the article.

Up Next
    Ebook Download
    View all
    Learn
    View all