SSRS Report In ASP.NET MVC 5

Introduction

In this post, I will show you how to create SSRS Report in ASP.NET MVC5. I hope you will like this.

Prerequisites

As I said earlier, we are going to use Report Viewer in our MVC application. For this, you must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.

SQL Database part

Here, find the scripts to create database and table.

Create Database

  1. USE [master]  
  2. GO  
  3.   
  4. /****** Object: Database [DbEmployee] Script Date: 9/29/2016 2:37:24 AM ******/  
  5. CREATE DATABASE [DbEmployee]  
  6. CONTAINMENT = NONE  
  7. ON PRIMARY   
  8. NAME = N'DbEmployee', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DbEmployee.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
  9. LOG ON   
  10. NAME = N'DbEmployee_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DbEmployee_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)  
  11. GO  
  12.   
  13. ALTER DATABASE [DbEmployee] SET COMPATIBILITY_LEVEL = 110  
  14. GO  
  15.   
  16. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
  17. begin  
  18. EXEC [DbEmployee].[dbo].[sp_fulltext_database] @action = 'enable'  
  19. end  
  20. GO  
  21.   
  22. ALTER DATABASE [DbEmployee] SET ANSI_NULL_DEFAULT OFF   
  23. GO  
  24.   
  25. ALTER DATABASE [DbEmployee] SET ANSI_NULLS OFF   
  26. GO  
  27.   
  28. ALTER DATABASE [DbEmployee] SET ANSI_PADDING OFF   
  29. GO  
  30.   
  31. ALTER DATABASE [DbEmployee] SET ANSI_WARNINGS OFF   
  32. GO  
  33.   
  34. ALTER DATABASE [DbEmployee] SET ARITHABORT OFF   
  35. GO  
  36.   
  37. ALTER DATABASE [DbEmployee] SET AUTO_CLOSE OFF   
  38. GO  
  39.   
  40. ALTER DATABASE [DbEmployee] SET AUTO_CREATE_STATISTICS ON   
  41. GO  
  42.   
  43. ALTER DATABASE [DbEmployee] SET AUTO_SHRINK OFF   
  44. GO  
  45.   
  46. ALTER DATABASE [DbEmployee] SET AUTO_UPDATE_STATISTICS ON   
  47. GO  
  48.   
  49. ALTER DATABASE [DbEmployee] SET CURSOR_CLOSE_ON_COMMIT OFF   
  50. GO  
  51.   
  52. ALTER DATABASE [DbEmployee] SET CURSOR_DEFAULT GLOBAL   
  53. GO  
  54.   
  55. ALTER DATABASE [DbEmployee] SET CONCAT_NULL_YIELDS_NULL OFF   
  56. GO  
  57.   
  58. ALTER DATABASE [DbEmployee] SET NUMERIC_ROUNDABORT OFF   
  59. GO  
  60.   
  61. ALTER DATABASE [DbEmployee] SET QUOTED_IDENTIFIER OFF   
  62. GO  
  63.   
  64. ALTER DATABASE [DbEmployee] SET RECURSIVE_TRIGGERS OFF   
  65. GO  
  66.   
  67. ALTER DATABASE [DbEmployee] SET DISABLE_BROKER   
  68. GO  
  69.   
  70. ALTER DATABASE [DbEmployee] SET AUTO_UPDATE_STATISTICS_ASYNC OFF   
  71. GO  
  72.   
  73. ALTER DATABASE [DbEmployee] SET DATE_CORRELATION_OPTIMIZATION OFF   
  74. GO  
  75.   
  76. ALTER DATABASE [DbEmployee] SET TRUSTWORTHY OFF   
  77. GO  
  78.   
  79. ALTER DATABASE [DbEmployee] SET ALLOW_SNAPSHOT_ISOLATION OFF   
  80. GO  
  81.   
  82. ALTER DATABASE [DbEmployee] SET PARAMETERIZATION SIMPLE   
  83. GO  
  84.   
  85. ALTER DATABASE [DbEmployee] SET READ_COMMITTED_SNAPSHOT OFF   
  86. GO  
  87.   
  88. ALTER DATABASE [DbEmployee] SET HONOR_BROKER_PRIORITY OFF   
  89. GO  
  90.   
  91. ALTER DATABASE [DbEmployee] SET RECOVERY SIMPLE   
  92. GO  
  93.   
  94. ALTER DATABASE [DbEmployee] SET MULTI_USER   
  95. GO  
  96.   
  97. ALTER DATABASE [DbEmployee] SET PAGE_VERIFY CHECKSUM   
  98. GO  
  99.   
  100. ALTER DATABASE [DbEmployee] SET DB_CHAINING OFF   
  101. GO  
  102.   
  103. ALTER DATABASE [DbEmployee] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )   
  104. GO  
  105.   
  106. ALTER DATABASE [DbEmployee] SET TARGET_RECOVERY_TIME = 0 SECONDS   
  107. GO  
  108.   
  109. ALTER DATABASE [DbEmployee] SET READ_WRITE   
  110. GO  
Create Table
  1. USE [DbEmployee]  
  2. GO  
  3.   
  4. /****** Object: Table [dbo].[Employee_tbt] Script Date: 9/29/2016 2:38:05 AM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[Employee_tbt](  
  15. [id] [int] IDENTITY(1,1) NOT NULL,  
  16. [Name] [varchar](50) NULL,  
  17. [Designation] [varchar](50) NULL,  
  18. [Gender] [varchar](50) NULL,  
  19. [JoinDate] [dateNULL,  
  20. [Salary] [floatNULL,  
  21. [City] [varchar](50) NULL,  
  22. [State] [varchar](50) NULL,  
  23. [Zip] [intNULL,  
  24. CONSTRAINT [PK_Employee_tbt] PRIMARY KEY CLUSTERED   
  25. (  
  26. [id] ASC  
  27. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  28. ON [PRIMARY]  
  29.   
  30. GO  
  31.   
  32. SET ANSI_PADDING OFF  
  33. GO  
After creating the table, you can add some records as shown below for demo.



Create your MVC application

Open Visual Studio and select File >> New Project.

The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.



Now, new dialog will pop up for selecting the template. We are going choose MVC template and click OK button.



After creating our project, we are going to add DataSet.

Create DataSet

In order to add DataSet component, right click on Reports folder > Add > New Item > Select DataSet > click Add button.





Next, click on Server Explorer link.



Now, Server Explorer section will be shown as given below. Right click on Data connections > Select Add Connection…



As you can see below, we need to select server name, then via drop down list in connect to a database panel. You should choose your database name. Finally, click OK.



Here, we will work with Employee_tbt table. For this, the next step is to drag our table, as shown below.



Create Report

For creating a report, right click on Reports folder > Add > New Item > Select Reporting. Here, we have three components. Select Report, finally click Add.






After clicking on Add, new window will pop up. We need to name our Dataset, and choose data source (in this case, via dropdown list, select MyDataSet, which has been created previously).

Next, we will design a table. Specify all fields that you want to display in your report.



Note - In order to start, you will need to install the ReportViewer for MVC. Run the following command in the Package Manager Console -

PM> Install-Package ReportViewerForMvc

Create a Controller

Now, we are going to create a Controller. Right click on the Controllers folder > Add > Controller> selecting MVC 5 Controller – Empty > click Add.



Enter Controller name (‘EmployeeController’).



EmployeeController.cs
  1. using Microsoft.Reporting.WebForms;  
  2. using ReportViewerMVC5.Reports;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Data.SqlClient;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Web.Mvc;  
  9. using System.Web.UI.WebControls;  
  10.   
  11. namespace ReportViewerMVC5.Controllers  
  12. {  
  13.     public class EmployeeController : Controller  
  14.     {  
  15.         // GET: Employee  
  16.         public ActionResult Index()  
  17.         {  
  18.             return View();  
  19.         }  
  20.   
  21.   
  22.         MyDataSet ds = new MyDataSet();  
  23.         public ActionResult ReportEmployee()  
  24.         {  
  25.             ReportViewer reportViewer = new ReportViewer();  
  26.             reportViewer.ProcessingMode = ProcessingMode.Local;  
  27.             reportViewer.SizeToReportContent = true;  
  28.             reportViewer.Width = Unit.Percentage(900);  
  29.             reportViewer.Height = Unit.Percentage(900);  
  30.   
  31.             var connectionString = ConfigurationManager.ConnectionStrings["DbEmployeeConnectionString"].ConnectionString;  
  32.   
  33.   
  34.             SqlConnection conx = new SqlConnection(connectionString);            SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM Employee_tbt", conx);  
  35.   
  36.             adp.Fill(ds, ds.Employee_tbt.TableName);  
  37.   
  38.             reportViewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\MyReport.rdlc";  
  39.             reportViewer.LocalReport.DataSources.Add(new ReportDataSource("MyDataSet", ds.Tables[0]));  
  40.   
  41.   
  42.             ViewBag.ReportViewer = reportViewer;  
  43.   
  44.             return View();  
  45.         }  
  46.     }  
  47. }  
Here, I’m creating ReportEmployee() action which will select all data from Employee_tbt table.

Explanation

As data provider, I’m using ADO.NET Framework.

 

  1. Connect to database by using the following line.
    1. var connectionString = ConfigurationManager.ConnectionStrings["DbEmployeeConnectionString"].ConnectionString;    
    2. SqlConnection conx = new SqlConnection(connectionString);  
  2. Using SqlDataAdapter object which takes two parameters: query and connection object, Fill() method is used for loading data to dataset object.
    1. SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM Employee_tbt", conx);  
    2. adp.Fill(ds, ds.Employee_tbt.TableName);  
  3. We need to specify report path by using the following line.
    1. reportViewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\MyReport.rdlc";  
  4. To refresh our report datasource with new data selected from database table, we need to proceed as follow.
    1. reportViewer.LocalReport.DataSources.Add(new ReportDataSource("MyDataSet", ds.Tables[0]));  

Adding View

In Employee Controller, right click on ReportEmployee() action. Select Add View and a dialog will pop up. Write a name for your View and click Add.



ReportEmployee.cshtml

  1. @using ReportViewerForMvc;  
  2. @{  
  3.     ViewBag.Title = "ReportEmployee";  
  4. }  
  5.   
  6.   
  7. @Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)  
Output



That’s all. Please send your feedback and queries in comments box.

 

Up Next
    Ebook Download
    View all
    Learn
    View all