Windows Forms: Create Orders or Receipts in Crystal Report

Create Crystal Report, Passing Parameters, Print Orders, Invoice, Receipt in C# Windows Forms Application.

Creating orders or receipts in Crystal Reports using C# Windows Forms involves several steps. I'll outline a basic approach to guide you through the process:

Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "PrintOrders" and then click OK

Create Orders and OrderDetail class to map data return from the sql database.

Crystal Reports allows you to connect to various data sources. In this example, we will connect the crystal report to sql database.

public class Orders
{
    public int OrderID { get; set; }
    public string CustomerID { get; set; }
    public string ContactName { get; set; }
    public string Address { get; set; }
    public string PostalCode { get; set; }
    public string City { get; set; }
    public string Phone { get; set; }
    public DateTime OrderDate { get; set; }
}
public class OrderDetail
{
    public int OrderID { get; set; }
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public decimal Discount { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal Total
    {
        get
        {
            return Quantity * UnitPrice - Quantity * UnitPrice * Discount;
        }
    }
}

First, make sure you have Crystal Reports installed in your development environment.

Open Crystal Reports and create a new report.

Name your report: rptOrders

c# print orders crystal report

Design the layout of your order or receipt form by adding fields, text objects, and any other elements you need. then bind the fields on your report to the corresponding fields in your data source. This ensures that the data is displayed correctly on the report.

Save the Crystal Report file (.rpt) in your project directory.

Create a main form named Form1, then drag and drop a CrystalReportViewer control to your Windows Form.

crystal report print orders c#

Next, Set the report source of the CrystalReportViewer control to the Crystal Report file you created.

Adding a connection string to the App.config file

<connectionStrings>
  <add name="cn" connectionString="Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=123@qaz;" providerName="System.Data.SqlClient"/>
</connectionStrings>

We use the Northwind database to play demo. If you haven't got Northwind database, you can view How to download and restore Northwind database in SQL Server. We will use Dapper to retrieve data from sql database, so you need to install Dapper from nuget,

Creating a new form with name frmPrint, then add code to handle your forms as shown below.

In your C# code, populate the object datasource with the necessary data for your order or receipt. Then, pass this object to the Crystal Report.

frmPrint

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrintOrders
{
    public partial class frmPrint : Form
    {
        List<OrderDetail> _list;
        Orders _orders;
        public frmPrint(Orders orders, List<OrderDetail> list)
        {
            InitializeComponent();
            _orders = orders;
            _list = list;
        }

        private void frmPrint_Load(object sender, EventArgs e)
        {
            //Init crystal report
            rptOrders1.SetDataSource(_list);
            rptOrders1.SetParameterValue("pOrderID", _orders.OrderID);
            rptOrders1.SetParameterValue("pDate", _orders.OrderDate.ToString("MM/dd/yyyy"));
            rptOrders1.SetParameterValue("pContactName", _orders.ContactName);
            rptOrders1.SetParameterValue("pPostalCode", _orders.PostalCode);
            rptOrders1.SetParameterValue("pAddress", _orders.Address);
            rptOrders1.SetParameterValue("pCity", _orders.City);
            rptOrders1.SetParameterValue("pPhone", _orders.Phone);
            // Set the CrystalReportViewer report source
            crystalReportViewer.ReportSource = rptOrders1;
            crystalReportViewer.Refresh();
        }
    }
}

Adding code to handle your Form1 as shown below.

Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dapper;
using System.Configuration;
using System.Data.SqlClient;

namespace PrintOrders
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            using(IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
            {
                if (db.State == ConnectionState.Closed)
                    db.Open();
                //Execute query to get customer data
                string query = "select o.OrderID, c.CustomerID, c.ContactName, c.Address, c.PostalCode, c.City, c.Phone, o.OrderDate" +
                                 " from Orders o inner join Customers c on o.CustomerID = c.CustomerID" +
                                $" where o.OrderDate between '{dtFromDate.Value}' and '{dtToDate.Value}'";
                ordersBindingSource.DataSource = db.Query<Orders>(query, commandType: CommandType.Text);          
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Orders obj = ordersBindingSource.Current as Orders;
            if (obj != null)
            {
                using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
                {
                    if (db.State == ConnectionState.Closed)
                        db.Open();
                    // c# execute query to get orders
                    string query = "select d.OrderID, p.ProductName, d.Quantity, d.Discount, d.UnitPrice from [Order Details] d inner join Products p on d.ProductID = p.ProductID" +
                                    $" where d.OrderID = '{obj.OrderID}'";
                    List<OrderDetail> list = db.Query<OrderDetail>(query, commandType: CommandType.Text).ToList();
                    using(frmPrint frm = new frmPrint(obj, list))
                    {
                        frm.ShowDialog();
                    }
                }
            }
        }
    }
}

Build and run your C# Windows Forms application. You should now see your Crystal Report displayed in the CrystalReportViewer control, populated with the data you provided.

VIDEO TUTORIAL