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
Create Table
- USE [DbEmployee]
- GO
-
- /****** Object: Table [dbo].[Employee_tbt] Script Date: 9/29/2016 2:38:05 AM ******/
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[Employee_tbt](
- [id] [int] IDENTITY(1,1) NOT NULL,
- [Name] [varchar](50) NULL,
- [Designation] [varchar](50) NULL,
- [Gender] [varchar](50) NULL,
- [JoinDate] [date] NULL,
- [Salary] [float] NULL,
- [City] [varchar](50) NULL,
- [State] [varchar](50) NULL,
- [Zip] [int] NULL,
- CONSTRAINT [PK_Employee_tbt] PRIMARY KEY CLUSTERED
- (
- [id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
-
- SET ANSI_PADDING OFF
- 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
- using Microsoft.Reporting.WebForms;
- using ReportViewerMVC5.Reports;
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.UI.WebControls;
-
- namespace ReportViewerMVC5.Controllers
- {
- public class EmployeeController : Controller
- {
-
- public ActionResult Index()
- {
- return View();
- }
-
-
- MyDataSet ds = new MyDataSet();
- public ActionResult ReportEmployee()
- {
- ReportViewer reportViewer = new ReportViewer();
- reportViewer.ProcessingMode = ProcessingMode.Local;
- reportViewer.SizeToReportContent = true;
- reportViewer.Width = Unit.Percentage(900);
- reportViewer.Height = Unit.Percentage(900);
-
- var connectionString = ConfigurationManager.ConnectionStrings["DbEmployeeConnectionString"].ConnectionString;
-
-
- SqlConnection conx = new SqlConnection(connectionString); SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM Employee_tbt", conx);
-
- adp.Fill(ds, ds.Employee_tbt.TableName);
-
- reportViewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\MyReport.rdlc";
- reportViewer.LocalReport.DataSources.Add(new ReportDataSource("MyDataSet", ds.Tables[0]));
-
-
- ViewBag.ReportViewer = reportViewer;
-
- return View();
- }
- }
- }
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.
- Connect to database by using the following line.
- var connectionString = ConfigurationManager.ConnectionStrings["DbEmployeeConnectionString"].ConnectionString;
- SqlConnection conx = new SqlConnection(connectionString);
- Using SqlDataAdapter object which takes two parameters: query and connection object, Fill() method is used for loading data to dataset object.
- SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM Employee_tbt", conx);
- adp.Fill(ds, ds.Employee_tbt.TableName);
- We need to specify report path by using the following line.
- reportViewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\MyReport.rdlc";
- To refresh our report datasource with new data selected from database table, we need to proceed as follow.
- 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
- @using ReportViewerForMvc;
- @{
- ViewBag.Title = "ReportEmployee";
- }
-
-
- @Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)
Output
That’s all. Please send your feedback and queries in comments box.