ASP.NET MVC

Use Crystal Report In ASP.NET MVC

In this post, we will learn how to generate a PDF file with the data and HTML and export it using Crystal Reports in MVC.NET.
Prerequisites
  • Visual Studio
  • SQL Server
  • Crystal Report

 

Step 1 – Create Crystal Report

IIn an MVC.NET project, on the root, create a folder for reports. Then, right click on that folder and select a new item. After that, create a Crystal Report file by following the below steps.

 

After clicking on the “Add” button, a new popup will open. In there, apply the below steps.

 

 

The Crystal Report will open like in the below image.

 

 

Then, you need to set the table into Crystal Report for displaying records. For that, follow the below steps.
Step 2 – Set Table
Right-click “Database Fields” from the Field Explorer and select the “Database Expert” option.
A new popup will open; apply the following steps.
On clicking OK, you will see the selected tables into Database Fields.
After that, you need to drag fields to the report which you want to display in the PDF file, as shown below.
Step 3 – Create a method in Controller
Create a method for returning the PDF file from the Crystal Report.
using CrystalDecisions.CrystalReports.Engine;  
using System;  
using System.Collections.Generic;  
using System.Data;  
using System.IO;  
using System.Linq;  
using System.Text.RegularExpressions;  
using System.Web;  
using System.Web.Mvc;  
using System.Xml.Linq;  
using temp.Models;  
  
namespace temp.Controllers  
{  
    public class HomeController : Controller  
    {  
        public ActionResult Index()  
        {  
            return View();  
        }  
  
        public ActionResult Download_PDF()  
        {  
            empEntities context = new empEntities();  
  
            ReportDocument rd = new ReportDocument();  
            rd.Load(Path.Combine(Server.MapPath("~/Report"), "Emp_Data.rpt"));  
            rd.SetDataSource(context.emp_table.Select(c => new  
            {  
                id = c.id,  
                name = c.name  
            }).ToList());  
  
            Response.Buffer = false;  
            Response.ClearContent();  
            Response.ClearHeaders();  
  
  
            rd.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;  
            rd.PrintOptions.ApplyPageMargins(new CrystalDecisions.Shared.PageMargins(5, 5, 5, 5));  
            rd.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA5;  
  
            Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);  
            stream.Seek(0, SeekOrigin.Begin);  
  
            return File(stream, "application/pdf", "CustomerList.pdf");  
        }  
  
    }  
}
Here, I am creating a Download_PDF() method for returning the PDF file using Crystal Report.
ReportDocument rd = new ReportDocument();

 

It will create a Crystal Report’s object “rd” and using this object, load the Crystal Report like below.
rd.Load(Path.Combine(Server.MapPath("~/Report"), "Emp_Data.rpt"));

 

Here, “context” is my entity’s object and I will get the emp_table’s data using this object by just using the below code.
context.emp_table.Select(c => new  
            {  
                id = c.id,  
                name = c.name  
            }).ToList()

 

and finally, this line returns the PDF file.
return File(stream, "application/pdf", "CustomerList.pdf");
Step 5 – Call the method of Download PDF from the view side.
<a href="Home/Download_PDF/" target="_blank" class="btn btn-primary" style="margin-top:20px;">Download_PDF</a>

 

Here, I have set the button “Download PDF” and on clicking it, the PDF file gets downloaded.
Output :
Kaushik Dudhat

Hi All, I am Kaushik Dudhat. I'm Working as a Web Developer. I have strong technical skills as well as excellent interpersonal skills. I am eager to be challenged in order to grow and improve my communication and professional IT skills gained through previous experiences in the IT sector. I have extensive experience with designing and developing enterprise scale applications. I have good skill of ASP.NET MVC, AngularJS, Web API, EPPlus, SQL, Entity Framework, Html, CSS, Bootstrap, JQuery, Rotativa and Windows services.

View Comments

  • Hi:i used your code But the following commands become colored and give an error:{"DataSet does not support System.Nullable."}.
    rd.SetDataSource(context.emp_table.Select(c => new
    {
    id = c.id,
    name = c.name
    }).ToList());

    I changed it as follows, it still gives the same error
    rd.SetDataSource(context.peaces.Select(c => new
    {

    ID = c.ID==null? 0 :c.ID,

    //==null? 0 :c.ID,
    code = c.code == null ? 0 : c.code,
    //== null ? 0 : c.code,
    kala = c.kala == null ? "0" : c.kala
    }).ToList());
    Please help me, my program needs reporting and I have been searching for a few days, I can not solve my problem.Thanks

Share
Published by
Kaushik Dudhat

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago