Step 1: 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
Step 2: Design your form as below
Name your main form: Form1

Name your report: rptOrders

Step 3: Add 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
Step 4: Install Dapper from nuget, then create Orders and OrderDetail class to map data return from the northwind 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;
}
}
}
Step 5: Add code to handle your forms as below
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);
crystalReportViewer.ReportSource = rptOrders1;
crystalReportViewer.Refresh();
}
}
}
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();
//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();
}
}
}
}
}
}
VIDEO TUTORIALS