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.
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.
This will open the Add Connection Dialog Box.
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.
This will take you to Entity Data model wizard.
This will bring up the choose your Data Connection.
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.
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.
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.
<a href="@Url.Action("ShowCustomerList");">Show Customer List</a>
Now go to
HomeController. and add the Import the following namespaces.
Create a new Action method
ShowCustomerList with the following code.
public ActionResult ShowCustomerList()
CrMVCApp.Models.NORTHWINDEntities db = new crMVCApp.Models.NORTHWINDEntities();
var c = (from b in db.Customers select b).ToList();
CustomerList rpt = new CustomerList();
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.