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.
In this tutorials, we are going to
- Create an empty MVC Project.
- Create a data connection.
- Create an entity Data Object.
- Create a report.
- Bind the report to an entity model.
- Run the report.
Table of Contents
Create an Empty ASP.NET MVC Application
The First step is to create a new MVC Project.
- Open Visual Studio.
- Click on File->New->Project.
- Select Visual C# -> Web -> ASP.NET Application.
- Name the Project as crMVCApp.
- Click on OK.
This will bring up the New ASP.NET Project template.
- Select Empty.
- Select MVC.
- 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.
- Select Server Explorer.
If Server Explorer is not visible go to Menu->View -> Server Explorer. - Select Data Connection.
- Right, Click on add connection.
This will open Choose Data source dialog box.
Under Choose Data Source dialog box
- Select Microsoft SQL Server.
- Under Data Provider Choose .NET Framework Data Provider for SQL Server.
- Click on Continue.
- This will open the Add Connection Dialog Box.
- Enter Server Name.
- Select Windows Authentication.
- From the Database Dropdown Select NORTHWIND.
- 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
- Select the models folder under Project folder.
- Right, click and select Add-> Add New.
This will bring up the Add New Item dialog box.
- Select data.
- Select ADO.NET Entity Data Model.
- Enter the name of the model as Customer.
- Click on Add.
- This will take you to Entity Data model wizard.
- Select EF Designer from the database.
- Click on next.
- This will bring up the choose your Data Connection.
- The Data connection, which we have created in the previous step appears in the connection drop down button. Select the connection.
- Select Save connection settings in Web.config as.
- Name the connection as NorthwindEntities.
- Click on Next.
- 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.
- Select Customer Table from the list.
- Enter the Model Namespace as NORTHWINDModel.
- Click on Finish.
These steps will create NORTHWINDModel ADO.NET entity model.
Create Crystal Report
The Next step is to Create a Report.
- Select Project.
- Right Click and build your project.
This will update the reference to ADO.NET entity model in Database expert in crystal reports. - Right Click on the project folder and click on Add->Add New.
- Select Reporting.
- Select Crystal Reports.
- Enter the name of the report as CustomerList.rpt.
- Click on Add.
This will bring up the Crystal Report Gallery. - Select As a Blank Report.
- Click on OK.
These steps are explained in the tutorial Create Report Using ASP.NET Webforms.
This will create a blank report. - Select Field Explorer.
- Right Click and select Database Expert.
- Select Project Data.
- Select .NET objects.
- You will see the entity model Customer which we had created appears on the list.
- Select and click right arrow to select Customer.
- Click on OK.
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.
1 2 3 | <a href="@Url.Action("ShowCustomerList");">Show Customer List</a> |
Now go to HomeController
. and add the Import the following namespaces.
1 2 3 4 | using System.IO; using CrMVCApp.Models; |
Create a new Action method ShowCustomerList
with the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public ActionResult ShowCustomerList() { CrMVCApp.Models.NORTHWINDEntities db = new crMVCApp.Models.NORTHWINDEntities(); //CrMVCApp.Models.Customer c; var c = (from b in db.Customers select b).ToList(); CustomerList rpt = new CustomerList(); rpt.Load(); rpt.SetDataSource(c); Stream s = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); return File( s,"application/pdf"); } |
Run the Report
Now we are ready to test our report.
- Run the Report.
- Click on Show Customer List Link
- 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.
this is your good guidance for old version users of VS 2015,2019. but not for VS 2022. so pls post updated tutorial which are according to new version.
Hi asif ,
can you guide me ,how to pass parameter using store procedure in MVC Crystal Report
please post tutorial for vs2022 these are for old version which are according to New version. thanks
Hi:I did the above but result is this error:Requested URL: /Home/ShowCustomerList;
when binding the report with entity model , it gives the error
“Failed to load database information
The Database connector crdb_adoplus.dll could not be loaded. The correct version of the database client for this database type might not be installed
“.
I was facing the same problem and I have updated the Crystal reports and my issue has been resolved.