-->

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.

public ActionResult ReportsEverest()
{
    List allEverest = new List();
    using (MyDatabaseEntities dc = new MyDatabaseEntities())
    {
        allEverest = dc.Everests.ToList();
    }
    return View(allEverest);
}

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

@model IEnumerable<MVCCrystalReport.Everest>
@{ViewBag.Title = "ReportsEverest";}
<h2>ReportsEverest</h2><p>
    @Html.ActionLink("Create New", "Create")
</p>
<div><a href="@Url.Action("ExportReport")"> Get Report in PDF</a></div>
<table border="0" cellpadding="0" cellspacing="10">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Mountain)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.HeightInMeter)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.HeightInFeet)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ProminenceInMeter)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ParentMount)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Mountain)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HeightInMeter)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HeightInFeet)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ProminenceInMeter)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ParentMount)
        </td>
        <td>
           
        </td>
    </tr>
} 
</table>

Run Application.

Look Result show in your browser.

Here I have added below the line for Getting Exported PDF File.
<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.

public ActionResult ExportReport()
{
    List allEverest = new List();
    using (MyDatabaseEntities dc = new MyDatabaseEntities())
    {
        allEverest = dc.Everests.ToList();
    }

    ReportDocument rd = new ReportDocument();
    rd.Load(Path.Combine(Server.MapPath("~/Reports"), "rpt_EverestList.rpt"));
    rd.SetDataSource(allEverest);

    Response.Buffer = false;
    Response.ClearContent();
    Response.ClearHeaders();
    try
    {
        Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
        stream.Seek(0, SeekOrigin.Begin);
        return File(stream, "application/pdf", "EverestList.pdf");
    }
    catch (Exception ex)
    {
        throw;
    }
}

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.