Use Crystal report in Asp.net mvc

Use Crystal report  in Asp.net mvc

It is very easy to Use Crystal report  in Asp.net mvc but In MVC   it became little bit complex to  install and use crystal report in mvc .

We have to install Crystal report from here to get its features in visual studio .

https://code.msdn.microsoft.com/ASPNET-MVC-SAP-Crystal-e0564cac

First create a project  in visual studio :

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

Now create a database table whose data is to shown in crystal report.

Use Crystal report in Asp.net mvc

 

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.

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….

Use Crystal report in Asp.net mvc

Add Razor view

In your view add a button on  click of that button you want to open the crystal report.

<button class="btn btn-primary" type="submit" id="showKratesReport">Show Report </button>

Add this java script code that will be executed on button click

 

@section Scripts {

<script>

$(function () {

$("#showKratesReport").click(function () {

 

window.location.href = "/DailyCustomerReort/OnDayCustomersReport";

 

});

});

</script>

}

 

Add the  action method in controller that  will bind the data to crystal report and display .

 

public ActionResult OnDayCustomersReport()

{

 

DateTime today = System.DateTime.Now.Date;

var customerInformation = (from q in context.Customers

where q.IsDeleted == false

select new OnlineBillingSystem.Models.CustomerDailyReport

{

CustomerName = q.CustomerName,

TotalBalanceKarat = (from karat in context.Karats

where karat.CustomerId == q.Id && karat.IsDeleted != true

select karat.IssuedKarat).Sum()

-

(from karat in context.Karats

where karat.CustomerId == q.Id

select karat.ReturnedKarat).Sum() ?? 0,

 

TodayKarat = (from karat in context.Karats

where karat.CustomerId == q.Id && karat.Date == today && karat.IsDeleted != true

select karat.IssuedKarat).Sum() ?? 0,

 

TotalBalanceAmount = (from bal in context.Transactions

where bal.CustomerId == q.Id && bal.IsDeleted != true

select bal.TotalAmount).Sum()

-

(from bal in context.Transactions

where bal.CustomerId == q.Id && bal.IsDeleted != true

select bal.AmountPaid).Sum() ?? 0,

BalanceAmount = (from bal in context.Sales

where bal.CustomerId == q.Id && SqlFunctions.DateDiff("DAY", bal.DateCreated, today) == 0 && SqlFunctions.DateDiff("mm", bal.DateCreated, today) == 0 && SqlFunctions.DateDiff("yy", bal.DateCreated, today) == 0 && bal.IsDeleted != true

select bal.TotalAmount).Sum()

-

(from bal in context.Sales

where bal.CustomerId == q.Id && SqlFunctions.DateDiff("DAY", bal.DateCreated, today) == 0 && SqlFunctions.DateDiff("mm", bal.DateCreated, today) == 0 && SqlFunctions.DateDiff("yy", bal.DateCreated, today) == 0 && bal.IsDeleted != true

select bal.AmountPaid).Sum() ?? 0,

TodaySale = (from bal in context.Sales

where bal.CustomerId == q.Id && SqlFunctions.DateDiff("DAY", bal.DateCreated, today) == 0 && SqlFunctions.DateDiff("mm", bal.DateCreated, today) == 0 && SqlFunctions.DateDiff("yy", bal.DateCreated, today) == 0 && bal.IsDeleted != true

select bal.TotalAmount).Sum() ?? 0,

AmountCollected = "",

}).OrderBy(x => x.CustomerName).ToList();

 

ReportClass rptH = new ReportClass();

rptH.FileName = Server.MapPath("~/Reports/DailyCustomersReport.rpt");

rptH.Load();

rptH.SetDataSource(customerInformation);

Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

 

rptH.Close();

rptH.Dispose();

return File(stream, "application/pdf");

}

Now run the project . click the button , pdf will be open like this .

Use Crystal report in Asp.net mvc

Happy Coding – Tutorial on Use Crystal report  in Asp.net mvc!!!!