banner



How To Create Crystal Report In C# Windows Application

Introduction

In this post, we will learn how we can use Crystal Report Control in an MVC application.

Today, my requirement is to export data from database table in PDF format by using crystal report and Entity Framework.

PrerequisitesFor this, you must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.

SQL Database partHere, you can find the scripts to create database and table.

Create Database

  1. USE [master]
  2. GO
  3. /****** Object:Database  [CustomerDB] Script Date : 9/25/2016 3:14:16 AM ******/
  4. CREATE DATABASE  [CustomerDB]
  5. CONTAINMENT = NONE
  6. ON PRIMARY
  7. (NAME  = N 'CustomerDB' , FILENAME = N 'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CustomerDB.mdf'  , SIZE  = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
  8. LOGON
  9. (NAME  = N 'CustomerDB_log' , FILENAME = N 'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CustomerDB_log.ldf'  , SIZE  = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
  10. GO
  11. ALTER DATABASE  [CustomerDB] SET  COMPATIBILITY_LEVEL = 110
  12. GO
  13. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled' ))
  14. begin
  15. EXEC  [CustomerDB].[dbo].[sp_fulltext_database] @ action  = 'enable'
  16. end
  17. GO
  18. ALTER DATABASE  [CustomerDB] SET  ANSI_NULL_DEFAULT OFF
  19. GO
  20. ALTER DATABASE  [CustomerDB] SET  ANSI_NULLS OFF
  21. GO
  22. ALTER DATABASE  [CustomerDB] SET  ANSI_PADDING OFF
  23. GO
  24. ALTER DATABASE  [CustomerDB] SET  ANSI_WARNINGS OFF
  25. GO
  26. ALTER DATABASE  [CustomerDB] SET  ARITHABORT OFF
  27. GO
  28. ALTER DATABASE  [CustomerDB] SET  AUTO_CLOSE OFF
  29. GO
  30. ALTER DATABASE  [CustomerDB] SET  AUTO_CREATE_STATISTICS ON
  31. GO
  32. ALTER DATABASE  [CustomerDB] SET  AUTO_SHRINK OFF
  33. GO
  34. ALTER DATABASE  [CustomerDB] SET  AUTO_UPDATE_STATISTICS ON
  35. GO
  36. ALTER DATABASE  [CustomerDB] SET  CURSOR_CLOSE_ON_COMMIT OFF
  37. GO
  38. ALTER DATABASE  [CustomerDB] SET  CURSOR_DEFAULT GLOBAL
  39. GO
  40. ALTER DATABASE  [CustomerDB] SET  CONCAT_NULL_YIELDS_NULL OFF
  41. GO
  42. ALTER DATABASE  [CustomerDB] SET  NUMERIC_ROUNDABORT OFF
  43. GO
  44. ALTER DATABASE  [CustomerDB] SET  QUOTED_IDENTIFIER OFF
  45. GO
  46. ALTER DATABASE  [CustomerDB] SET  RECURSIVE_TRIGGERS OFF
  47. GO
  48. ALTER DATABASE  [CustomerDB] SET  DISABLE_BROKER
  49. GO
  50. ALTER DATABASE  [CustomerDB] SET  AUTO_UPDATE_STATISTICS_ASYNC OFF
  51. GO
  52. ALTER DATABASE  [CustomerDB] SET  DATE_CORRELATION_OPTIMIZATION OFF
  53. GO
  54. ALTER DATABASE  [CustomerDB] SET  TRUSTWORTHY OFF
  55. GO
  56. ALTER DATABASE  [CustomerDB] SET  ALLOW_SNAPSHOT_ISOLATION OFF
  57. GO
  58. ALTER DATABASE  [CustomerDB] SET  PARAMETERIZATION SIMPLE
  59. GO
  60. ALTER DATABASE  [CustomerDB] SET  READ_COMMITTED_SNAPSHOT OFF
  61. GO
  62. ALTER DATABASE  [CustomerDB] SET  HONOR_BROKER_PRIORITY OFF
  63. GO
  64. ALTER DATABASE  [CustomerDB] SET  RECOVERY SIMPLE
  65. GO
  66. ALTER DATABASE  [CustomerDB] SET  MULTI_USER
  67. GO
  68. ALTER DATABASE  [CustomerDB] SET  PAGE_VERIFY CHECKSUM
  69. GO
  70. ALTER DATABASE  [CustomerDB] SET  DB_CHAINING OFF
  71. GO
  72. ALTER DATABASE  [CustomerDB] SET  FILESTREAM( NON_TRANSACTED_ACCESS = OFF  )
  73. GO
  74. ALTER DATABASE  [CustomerDB] SET  TARGET_RECOVERY_TIME = 0 SECONDS
  75. GO
  76. ALTER DATABASE  [CustomerDB] SET  READ_WRITE
  77. GO

Create Table

  1. USE [CustomerDB]
  2. GO
  3. /****** Object:Table  [dbo].[Customers] Script Date : 9/25/2016 3:14:46 AM ******/
  4. SET  ANSI_NULLS ON
  5. GO
  6. SET  QUOTED_IDENTIFIER ON
  7. GO
  8. SET  ANSI_PADDING ON
  9. GO
  10. CREATE TABLE  [dbo].[Customers](
  11. [CustomerID] [int ] NOT NULL ,
  12. [CustomerName] [varchar ](50) NULL ,
  13. [CustomerEmail] [varchar ](50) NULL ,
  14. [CustomerZipCode] [int ] NULL ,
  15. [CustomerCountry] [varchar ](50) NULL ,
  16. [CustomerCity] [varchar ](50) NULL ,
  17. CONSTRAINT  [PK_Customers] PRIMARY KEY  CLUSTERED
  18. (
  19. [CustomerID]ASC
  20. )WITH  (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON  [ PRIMARY ]
  21. )ON  [ PRIMARY ]
  22. GO
  23. SET  ANSI_PADDING OFF
  24. GO

After creating the table, you can add some records, as shown below for demo purposes.

Create your MVC application

Open Visual Studio and select File. Click New Project.

The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.

Now, new dialog will pop up for selecting the template. We are going choose MVC template and click OK button.

After creating our project, we are going to add ADO.NET Entity Data Model.

Adding ADO.NET Entity Data Model

For this, right click on the project name, click Add > Add New Item.

Dialog box will pop up, inside Visual C# select Data then ADO.NET Entity Data Model, and enter name for your Dbcontext model as DbContextCustomer, finally click Add.

At this stage, we are going to choose EF Designer from database as given below.

In this snapshot given below, we need to select your server name, then via dropdown list in connection to a database section, you should choose your database name, finally click OK button.

After clicking on Next button, the dialog Entity Data Model Wizard will pop up for choosing the object which we want to use. In this example we are going to choose Customers table and click Finish button.

Finally, we see that EDMX model generates Customer class.

Create Crystal ReportFor doing this, right click on CrystalReports folder > Add > New Item > Select Reporting > CrystalReports.
Name your CrystalReport and click Add button.

Note - If Crystal Report Control does not exist in your VS 2015, you must install it from here CRforVS_13_0_16.exe.

Next, window will pop up as given below, in this example we are going to choose "As a Blank Report" option, and click OK.

After clicking OK, our Crystal Report has been created with success.
The next step is to right click on Database Fields > Database Expert…

Now, a new window will pop up as shown below. We need to select model which will be using to display data (in this case our model is Customer).

After clicking OK, we proceed to drag all fields to the report as shown below.

Create a controller

Now, we are going to create a controller. Right click on the controllers folder > Add > Controller> selecting MVC 5 Controller – Empty > click Add.

Enter Controller name ('CustomerController').

CustomerController.cs

  1. using CrystalDecisions.CrystalReports.Engine;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. namespace CrystalReportMVC.Controllers
  9. {
  10. public class  CustomerController : Controller
  11.     {
  12. private  CustomerDBEntities context = new  CustomerDBEntities();
  13. public  ActionResult Index()
  14.         {
  15. var  customerList = context.Customers.ToList();
  16. return  View(customerList);
  17.         }
  18. public  ActionResult ExportCustomers()
  19.         {
  20.             List<Customer> allCustomer =new  List<Customer>();
  21.             allCustomer = context.Customers.ToList();
  22.             ReportDocument rd =new  ReportDocument();
  23.             rd.Load(Path.Combine(Server.MapPath("~/CrystalReports" ), "ReportCustomer.rpt" ));
  24.             rd.SetDataSource(allCustomer);
  25.             Response.Buffer =false ;
  26.             Response.ClearContent();
  27.             Response.ClearHeaders();
  28.                 Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
  29.                 stream.Seek(0, SeekOrigin.Begin);
  30. return  File(stream, "application/pdf" , "CustomerList.pdf" );
  31.         }
  32.     }
  33. }

Here, I'm creating Index() action which will select all data from Customers table and ExportCustomers() action that allow us exporting data to pdf format by using crystal report.

Adding ViewIn CustomerController. Just right click on Index() action, select Add View and dialog will pop up, write a name for your view, then via dropdown list select List as template and Customer as model class. Finally click Add.

Index cshtml

  1. @model IEnumerable<CrystalReportMVC.Customer>
  2. @{
  3.     ViewBag.Title ="Index" ;
  4. }
  5. <h2>Customers List</h2>
  6. <p>
  7.     @Html.ActionLink("Create New" , "Create" )
  8.     <div><a href="@Url.Action(" ExportCustomers ")" > Report PDF </a></div>
  9. </p>
  10. <tableclass = "table" >
  11.     <tr>
  12.         <th>
  13.             @Html.DisplayNameFor(model => model.CustomerName)
  14.         </th>
  15.         <th>
  16.             @Html.DisplayNameFor(model => model.CustomerEmail)
  17.         </th>
  18.         <th>
  19.             @Html.DisplayNameFor(model => model.CustomerZipCode)
  20.         </th>
  21.         <th>
  22.             @Html.DisplayNameFor(model => model.CustomerCountry)
  23.         </th>
  24.         <th>
  25.             @Html.DisplayNameFor(model => model.CustomerCity)
  26.         </th>
  27.         <th></th>
  28.     </tr>
  29. @foreach (var  item in  Model) {
  30.     <tr>
  31.         <td>
  32.             @Html.DisplayFor(modelItem => item.CustomerName)
  33.         </td>
  34.         <td>
  35.             @Html.DisplayFor(modelItem => item.CustomerEmail)
  36.         </td>
  37.         <td>
  38.             @Html.DisplayFor(modelItem => item.CustomerZipCode)
  39.         </td>
  40.         <td>
  41.             @Html.DisplayFor(modelItem => item.CustomerCountry)
  42.         </td>
  43.         <td>
  44.             @Html.DisplayFor(modelItem => item.CustomerCity)
  45.         </td>
  46.         <td>
  47.             @Html.ActionLink("Edit" , "Edit" , new  { id=item.CustomerID }) |
  48.             @Html.ActionLink("Details" , "Details" , new  { id=item.CustomerID }) |
  49.             @Html.ActionLink("Delete" , "Delete" , new  { id=item.CustomerID })
  50.         </td>
  51.     </tr>
  52. }
  53. </table>

Output


How To Create Crystal Report In C# Windows Application

Source: https://www.c-sharpcorner.com/article/using-crystal-report-with-asp-net-mvc-5/

Posted by: pullenmrseach.blogspot.com

0 Response to "How To Create Crystal Report In C# Windows Application"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel