Introduction
In this post, we will learn how we can use Crystal Report Control in an MVC application.
Today, my requirement is to export data from database table in PDF format by using crystal report and Entity Framework.
Prerequisites
For this, you must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.
SQL Database part
Here, you can find the scripts to create database and table.
Create Database
Create Table
- USE [CustomerDB]
- GO
-
- /****** Object: Table [dbo].[Customers] Script Date: 9/25/2016 3:14:46 AM ******/
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[Customers](
- [CustomerID] [int] NOT NULL,
- [CustomerName] [varchar](50) NULL,
- [CustomerEmail] [varchar](50) NULL,
- [CustomerZipCode] [int] NULL,
- [CustomerCountry] [varchar](50) NULL,
- [CustomerCity] [varchar](50) NULL,
- CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
- (
- [CustomerID] 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 purposes.
Create your MVC application
Open Visual Studio and select File. Click 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 ADO.NET Entity Data Model.
Adding ADO.NET Entity Data Model
For this, right click on the project name, click Add > Add New Item.
Dialog box will pop up, inside Visual C# select Data then ADO.NET Entity Data Model, and enter name for your Dbcontext model as DbContextCustomer, finally click Add.
At this stage, we are going to choose EF Designer from database as given below.
In this snapshot given below, we need to select your server name, then via dropdown list in connection to a database section, you should choose your database name, finally click OK button.
After clicking on Next button, the dialog Entity Data Model Wizard will pop up for choosing the object which we want to use. In this example we are going to choose Customers table and click Finish button.
Finally, we see that EDMX model generates Customer class.
Create Crystal Report
For doing this, right click on CrystalReports folder > Add > New Item > Select Reporting > CrystalReports.
Name your CrystalReport and click Add button.
Note - If Crystal Report Control does not exist in your VS 2015, you must install it from here CRforVS_13_0_16.exe.
Next, window will pop up as given below, in this example we are going to choose "As a Blank Report" option, and click OK.
After clicking OK, our Crystal Report has been created with success.
The next step is to right click on Database Fields > Database Expert…
Now, a new window will pop up as shown below. We need to select model which will be using to display data (in this case our model is Customer).
After clicking OK, we proceed to drag all fields to the report as shown below.
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 (‘CustomerController’).
CustomerController.cs
- using CrystalDecisions.CrystalReports.Engine;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace CrystalReportMVC.Controllers
- {
- public class CustomerController : Controller
- {
-
- private CustomerDBEntities context = new CustomerDBEntities();
-
- public ActionResult Index()
- {
- var customerList = context.Customers.ToList();
- return View(customerList);
- }
-
-
- public ActionResult ExportCustomers()
- {
- List<Customer> allCustomer = new List<Customer>();
- allCustomer = context.Customers.ToList();
-
-
- ReportDocument rd = new ReportDocument();
- rd.Load(Path.Combine(Server.MapPath("~/CrystalReports"), "ReportCustomer.rpt"));
-
- rd.SetDataSource(allCustomer);
-
- Response.Buffer = false;
- Response.ClearContent();
- Response.ClearHeaders();
-
-
- Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
- stream.Seek(0, SeekOrigin.Begin);
- return File(stream, "application/pdf", "CustomerList.pdf");
- }
-
- }
- }
Here, I’m creating Index() action which will select all data from Customers table and ExportCustomers() action that allow us exporting data to pdf format by using crystal report.
Adding View
In CustomerController. Just right click on Index() action, select Add View and dialog will pop up, write a name for your view, then via dropdown list select List as template and Customer as model class. Finally click Add.
Index cshtml
- @model IEnumerable<CrystalReportMVC.Customer>
-
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>Customers List</h2>
-
- <p>
- @Html.ActionLink("Create New", "Create")
-
- <div><a href="@Url.Action("ExportCustomers")"> Report PDF </a></div>
-
- </p>
- <table class="table">
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.CustomerName)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.CustomerEmail)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.CustomerZipCode)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.CustomerCountry)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.CustomerCity)
- </th>
- <th></th>
- </tr>
-
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.CustomerName)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.CustomerEmail)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.CustomerZipCode)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.CustomerCountry)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.CustomerCity)
- </td>
- <td>
- @Html.ActionLink("Edit", "Edit", new { id=item.CustomerID }) |
- @Html.ActionLink("Details", "Details", new { id=item.CustomerID }) |
- @Html.ActionLink("Delete", "Delete", new { id=item.CustomerID })
- </td>
- </tr>
- }
-
- </table>
Output