Crystal Reports in ASP.NET MVC and ADO.NET Entity Model

In this tutorial, we are going to look at how to create Crystal Reports in ASP.NET MVC using ADO.NET Entity model.  ADO.NET entity model is an ORM  framework. In our last tutorials, we explained how to create the Crystal Report using Windows desktop application and ASP.NET Webforms.  Please read these tutorials from the following links.

  1. Create Crystal Report using Windows Desktop Application
  2. Create Crystal Report using ASP.NET Webforms

In this tutorials, we are going to

  1. Create an empty MVC Project.
  2. Create a data connection.
  3. Create an entity Data Object.
  4. Create a report.
  5. Bind the report to an entity model.
  6. Run the report.

Create an Empty ASP.NET MVC Application

The First step is to create a new MVC Project.

Create a Empty MVC Application
Create a MVC Application (Empty)
  1. Open Visual Studio.
  2. Click on File->New->Project.
  3. Select Visual C# -> Web -> ASP.NET Application.
  4. Name the Project as crMVCApp.
  5. Click on OK.

This will bring up the New ASP.NET Project template.

Create Emplty MVC Project
Select Empty and then MVC
  1. Select Empty.
  2. Select MVC.
  3. Click on OK.

The MVC Project is created.  Run and verify whether the application is working correctly.

Create a Data Connection

Next step is to Create a data Connection to our Project. To Do this follow, these steps.

Add Data Connection
Add Data Connection
  1. Select Server Explorer.
    If Server Explorer is not visible go to Menu->View -> Server Explorer.
  2. Select Data Connection.
  3. Right, Click on add connection.
    This will open Choose Data source dialog box.
Choose Data Source
Choose Data Source

Under Choose Data Source dialog box

  1. Select Microsoft SQL Server.
  2. Under Data Provider Choose .NET Framework Data Provider for SQL Server.
  3. Click on Continue.
  4. This will open the Add Connection Dialog Box.
Save Connection
Save Connection
  1. Enter Server Name.
  2. Select Windows Authentication.
  3. From the Database Dropdown Select NORTHWIND.
  4. Click on OK.

With this, our project has a data connection

Create an ADO.NET Entity Data Model

In this tutorial, we are going to create ADO.NET Entity Data Model and try to connect it to the Crystal Report. To do this follow these steps

  1. Select the models folder under Project folder.
  2. Right, click and select Add-> Add New.
    This will bring up the Add New Item dialog box.
Create ADO.NET Entity Model
Create ADO.NET Entity Model
  1. Select data.
  2. Select ADO.NET Entity Data Model.
  3. Enter the name of the model as Customer.
  4. Click on Add.
  5. This will take you to Entity Data model wizard.
Entity Data Model Wizard
Entity Data Model Wizard
  1. Select EF Designer from the database.
  2. Click on next.
  3. This will bring up the choose your Data Connection.
Entity Data Model Choose Your Data Connection
Entity Data Model Choose Your Data Connection
  1. The Data connection, which we have created in the previous step appears in the connection drop down button. Select the connection.
  2. Select Save connection settings in Web.config as.
  3. Name the connection as NorthwindEntities.
  4. Click on Next.
  5. It may ask for Version of the framework you want to choose. Choose the latest and Click on Next. This will take you to Choose your database Objects and Settings.
Entity Data Model Choose Your Database objects and Settings
Entity Data Model Choose Your Database objects and Settings
  1. Select Customer Table from the list.
  2. Enter the Model Namespace as NORTHWINDModel.
  3. Click on Finish.

These steps will create NORTHWINDModel ADO.NET entity model.

Create Crystal Report

The Next step is to Create a Report.

  1. Select Project.
  2. Right Click and build your project.
    This will update the reference to ADO.NET entity model in Database expert in crystal reports.
  3. Right Click on the project folder and click on Add->Add New.
  4. Select Reporting.
  5. Select Crystal Reports.
  6. Enter the name of the report as CustomerList.rpt.
  7. Click on Add.
    This will bring up the Crystal Report Gallery.
  8. Select As a Blank Report.
  9. Click on OK.
    These steps are explained in the tutorial Create Report Using ASP.NET Webforms.
    This will create a blank report.
  10. Select Field Explorer.
  11. Right Click and select Database Expert.
Database Export Add Datasource to the Report
Database Export Add Datasource to the Report
  1. Select Project Data.
  2. Select .NET objects.
  3. You will see the entity model Customer which we had created appears on the list.
  4. Select and click right arrow to select Customer.
  5. Click on OK.
Add Fields to Report
Add Fields to Report

Now from the Fields explorer drag the fields to the report. and you are ready.

Bind the Report to Entity Model

We have successfully created the report. The Next step is to show the report. Unlike Windows Forms application or ASP.NET Webform application, in MVC we cannot use crystal Report Viewer. The Only option is to download the report. Follow these steps to generate the report.

Go to Controller folder. Right Click Add Controller.

Select MVC 5 Controller-Empty.Click on Add. Enter the Controller Name as HomeController. Click on ADD. This will create HomeController with Index Action Method.

Open HomeController and keep the cursor on Index and right click and Select Add View. This will open Add View dialog box.

Keep the template as Empty(without model). Click on Add to create the view.This will create the view named “View” under HomeController. Open the View and and insert the following code.

Now go to HomeController. and add the Import the following namespaces.

Create a new Action method ShowCustomerList with the following code.

Run the Report

Now we are ready to test our report.

  1. Run the Report.
  2. Click on Show Customer List Link
  3. If every step is done properly and correctly, then you will definitely see the list of the customer in PDF form.

That will end our tutorial. Please comment on the section below if you any queries.

10 thoughts on “Crystal Reports in ASP.NET MVC and ADO.NET Entity Model”

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top