Friday, 29 May 2015

Displaying SSRS Report in MVC using MvcReportViewer -Part II

Embedding a Local Report


In my previous post we discussed how to install the MVCReportViewer package via Nuget into your application to avail of the helpers to embed SSRS reports into your MVC views.

In this article we will discuss using the MVCReportViewer  package and helpers to embed local reports.

Most often people want to embed SSRS reports from the server into thier views, but sometimes we just want to create a simple local report in Visual Studio and display that in our view. This is the scenario we will discuss below.

Creating reports in Visual Studio 2012/2013

Visual Studio 2012/2013 comes with reporting capibilites out of the box. Simply right click in your solution explorer in Visual Studio and Add Item/ Reporting and you can see you have the option to add a Report or use the Report Wizard.



















This provides you with good basic reports, it's like a pared down version of the tools and controls you get with real SSRS. If this suits your needs you need go no further. However if you need more capilibilites than whats on offer here you will need to install the SQL Server Data Tools – Business Intelligence tools  for Visual Studio 2012/2013.

SQL Server Data Tools – Business Intelligence for Visual Studio 

Currently the URL to download Microsoft Visual Studio Tools for Applications component for Visual Studio 2012 can be found here:

http://www.microsoft.com/en-ca/download/details.aspx?id=38807

Once you have install it, you will be able to create and work with SSRS reports within Visual Studio. Here are the basic steps for setting up a new SSRS report project:

Open up Visual Studio

  • Select File –> New –> Project
  • You will now see a new project template option called Business Intelligence
  • Expand this project template and select the Reporting Services option.
  • You can now either choose to use the setup wizard or just a plain project template.


Regardless of whether your requirements necessitate the use of the Business Intelligence tools or the standard out of the box reporting tools in Visual Studio, lets now disucss adding a basic report to your project so you can embed it in your MVC view.

DataSets

Before creating your report, you need to define your dataset. Lets create a Report folder in our project. It is a good idea to organise your folder structure within your application in a logical manner , so we will use this folder for all our Datasets and reports.

Using the standard out of the box reporting functionality in Visual Studio 2012/2013, right-click in your Report folder and Add New item.

Go to Data-> and select Dataset. Call it 

Displaying SSRS Report in MVC using MvcReportViewer -Part I

In this article we will discuss installing the MVCReportViewer package into your MVC application to get it ready to embed reports. 

In Part 2 , we will discusss embedding local reports into your MVC application and in Part 3 we will discuss the changes needed to embed reports from yourn SSRS server.

Embedding SSRS or indeed local RDLC reports into a  view in an ASP.NET MVC application is easy using the MVCReportViewer package , and the HTML Helpers it comes with.

The first step to get the MVCReportViewer package installed into your project , to do this you can use the Package manager console in Visual Studio , using the following command:

PM> Install-Package MvcReportViewer
This will add the relevant scripts, Web.Config app settings and report viewer appx to your MVC Application which are needed to embed your reports into your views.

After sucessfully installing the MVCReportViewer package you will notice some changes to your application. 

The following App settings for the report viewer have been added to your Web.Config:


<add key="MvcReportViewer.AspxViewer" value="/MvcReportViewer.aspx" />
<add key="MvcReportViewer.AspxViewerJavaScript" value="~/Scripts/MvcReportViewer.js" />
<add key="MvcReportViewer.ErrorPage" value="~/MvcReportViewerErrorPage.html" />
<add key="MvcReportViewer.ShowErrorPage" value="true" />

<add key="MvcReportViewer.ReportServerUrl" value="http://localhost/Reportserver_SQLEXPRESS" />
<add key="MvcReportViewer.Username" value="" />
<add key="MvcReportViewer.Password" value="" />
<add key="MvcReportViewer.EncryptParameters" value="false" />
<add key="MvcReportViewer.IsAzureSSRS" value="false" />

<add key="MvcReportViewer.LocalDataSourceProvider" value="MvcReportViewer.SessionLocalDataSourceProvider, MvcReportViewer" />


Key settings to note here , are the AspxViewer setting, which points to the aspx page which has also been installed by the NuGet package . It is this page that will be used to display our report in an iframe. 

The ReportServerUrl and LocalDataSourceProvider settings are important depending on wehther you want to embed a server report or a local one. I will use both later on when i give examples of both scenarios.

And that's it, now you are ready to embed a server or local report into your MVC views. See the following articles for examples of both.