-->

How to use Crystal Report in MVC4.


Introduction

In this post, I explain how to use Crystal Report in MVC4.

In one of my previous article I have shown you how to use Microsoft report in asp.net MVC, Today I show you a very easy way to use Crystal Report with MVC.
It is very easy to create crystal reports in asp.net web form application, but in MVC it's a little bit difficult since MVC does not have any server side controls and events. Today I show you a very easy way to use Crystal Report with MVC.

Prerequisite

I used followings:
  • .Net framework 4.0
  • Entity Framework
  • SQLServer 2008

Steps :

Just follow the steps and get result easily.

Step - 1 : Create New Project

Go to File > New > Project > Select asp.net mvc4 web application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table and insert data for show in report

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.

My Table Structure in this Example

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add Action for populate data.

Go To Controller > Add your action > write the following code and Rebuild your application to get data from Database.

I have used HomeController Here.

  1. public ActionResult ReportsEverest()
  2. {
  3. List allEverest = new List();
  4. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  5. {
  6. allEverest = dc.Everests.ToList();
  7. }
  8. return View(allEverest);
  9. }

Step-6: Add View for show data on a page.

Right Click on your Action > Add View > Enter View name > Check Create a strongly-type view > Select your model class > Select Scaffold templete > Select list > Add.

View Html Code

  1. @model IEnumerable<MVCCrystalReport.Everest>
  2. @{ViewBag.Title = "ReportsEverest";}
  3. <h2>ReportsEverest</h2><p>
  4. @Html.ActionLink("Create New", "Create")
  5. </p>
  6. <div><a href="@Url.Action("ExportReport")"> Get Report in PDF</a></div>
  7. <table border="0" cellpadding="0" cellspacing="10">
  8. <tr>
  9. <th>
  10. @Html.DisplayNameFor(model => model.Mountain)
  11. </th>
  12. <th>
  13. @Html.DisplayNameFor(model => model.HeightInMeter)
  14. </th>
  15. <th>
  16. @Html.DisplayNameFor(model => model.HeightInFeet)
  17. </th>
  18. <th>
  19. @Html.DisplayNameFor(model => model.ProminenceInMeter)
  20. </th>
  21. <th>
  22. @Html.DisplayNameFor(model => model.ParentMount)
  23. </th>
  24. <th></th>
  25. </tr>
  26.  
  27. @foreach (var item in Model) {
  28. <tr>
  29. <td>
  30. @Html.DisplayFor(modelItem => item.Mountain)
  31. </td>
  32. <td>
  33. @Html.DisplayFor(modelItem => item.HeightInMeter)
  34. </td>
  35. <td>
  36. @Html.DisplayFor(modelItem => item.HeightInFeet)
  37. </td>
  38. <td>
  39. @Html.DisplayFor(modelItem => item.ProminenceInMeter)
  40. </td>
  41. <td>
  42. @Html.DisplayFor(modelItem => item.ParentMount)
  43. </td>
  44. <td>
  45. </td>
  46. </tr>
  47. }
  48. </table>

Run Application.

Look Result show in your browser.

Here I have added below the line for Getting Exported PDF File.
  1. <a href="@Url.Action("ExportReport")"> Get Report in PDF</a>

Step-7: Add Report file(.rpt) and Design your report.

Add "Reports" folder to your project
Right Click on "Reports" folder > Add > New item > Select Report under Reporing (Crystal Report file) > Enter report file name > Add.

Here we also have to add Datasource for our report.
Right Click On "Database Fields" under Fields Explorer > Database Expert > Project Data > .NET Objects > Select your Object > Click on simble ">>" > Ok.
Now Design your Report looks.
Here I have designed my Report Like this....



Step-9: Add Action for generating PDF File for Report Data

Go To Controller > Add your action > write the following code and Rebuild your application to get data from Database.

  1. public ActionResult ExportReport()
  2. {
  3. List allEverest = new List();
  4. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  5. {
  6. allEverest = dc.Everests.ToList();
  7. }
  8.  
  9. ReportDocument rd = new ReportDocument();
  10. rd.Load(Path.Combine(Server.MapPath("~/Reports"), "rpt_EverestList.rpt"));
  11. rd.SetDataSource(allEverest);
  12.  
  13. Response.Buffer = false;
  14. Response.ClearContent();
  15. Response.ClearHeaders();
  16. try
  17. {
  18. Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
  19. stream.Seek(0, SeekOrigin.Begin);
  20. return File(stream, "application/pdf", "EverestList.pdf");
  21. }
  22. catch (Exception ex)
  23. {
  24. throw;
  25. }
  26. }

Step-10: Run Application

Click on links "Get Report in PDF" and get your report in PDF format. Thank you.



Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: