blog

Home / DeveloperSection / Blogs / Combining ASP.Net MVC With SQL Server Reporting Services

Combining ASP.Net MVC With SQL Server Reporting Services

Anonymous User7456 03-Feb-2015

Hi everyone in this blog I’m explaining about Reporting Services in ASP.NET MVC.

Description:

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.

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.

Combining ASP.Net MVC With SQL Server Reporting Services

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

  1. Script manger
  2. Report Viewer control

 

ReportViewer.aspx code:
 

<formid="form1"runat="server">
        <asp:ScriptManagerID="ScriptManager1"runat="server"></asp:ScriptManager>
        <div>
            <rsweb:ReportViewerID="rptViewer"runat="server"Height="503px"Width="663px">
            </rsweb:ReportViewer>
        </div>
    </form>

 

The following Procedure 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:

privatevoid ShowReport()
        {
            try
            {
                //report url 
                string urlReportServer = "http://localhost:57370//Reportserver";
 
                rptViewer.ProcessingMode = ProcessingMode.Remote;
 
                rptViewer.ServerReport.ReportServerUrl = newUri(urlReportServer);
 
                rptViewer.ServerReport.ReportPath = "~/Home/Report/TestReport.txt";
 
                rptViewer.ServerReport.Refresh();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

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: 

publicpartialclassReportViewer : System.Web.UI.Page
    {
        protectedvoid Page_Load(object sender, EventArgs e)
        {
            ShowReport();
        }
    }

 

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:

publicActionResult Index()
        {
            return View();
        }
        publicActionResult ShowReport()
        {
            return Redirect("~/ReportViewer/ReportViewer.aspx");
        } 

Output:

Combining ASP.Net MVC With SQL Server Reporting Services

in my next post i'll explain about Introduction Angularjs


I am a content writter !

Leave Comment

Comments

Liked By