How to Print Orders/Receipt using Report Viewer in C#
By FoxLearn 12/10/2024 2:24:51 PM 21.51K
How to generate and print orders/receipts using Report Viewer?
First, Open 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
Next, Install the Microsoft.ReportViewer.WinForms NuGet package in your project
Install-Package Microsoft.Reporting.WinForms
Design your form as shown below.
Name your main form: Form1
Name your print form: frmPrint
Add an .rdlc report file to your project.
Name your report: rptOrders
Design the receipt template using the report designer by dragging and dropping fields onto the design surface.
Add the following configuration to your App.config file:
<connectionStrings> <add name="cn" connectionString="Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=123@qaz;" providerName="System.Data.SqlClient"/> </connectionStrings>
The connection string allows your application to connect to the Northwind database.
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
Install Dapper from nuget, then define the data models to map the orders and order details from the 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; } } }
frmPrint
for Displaying and Printing the Report.
This form initializes the report data source and sets parameters for the Report Viewer.
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 { Orders _orders; List<OrderDetail> _list; public frmPrint(Orders orders, List<OrderDetail> list) { InitializeComponent(); _orders = orders; _list = list; } private void frmPrint_Load(object sender, EventArgs e) { // Initialize data source OrderDetailBindingSource.DataSource = _list; // Set parameters for the report Microsoft.Reporting.WinForms.ReportParameter[] p = new Microsoft.Reporting.WinForms.ReportParameter[] { new Microsoft.Reporting.WinForms.ReportParameter("pOrderID",_orders.OrderID.ToString()), new Microsoft.Reporting.WinForms.ReportParameter("pOrderDate",_orders.OrderDate.ToString("MM/dd/yyyy")), new Microsoft.Reporting.WinForms.ReportParameter("pContactName",_orders.ContactName), new Microsoft.Reporting.WinForms.ReportParameter("pPostalCode",_orders.PostalCode), new Microsoft.Reporting.WinForms.ReportParameter("pAddress",_orders.Address), new Microsoft.Reporting.WinForms.ReportParameter("pCity",_orders.City), new Microsoft.Reporting.WinForms.ReportParameter("pPhone",_orders.Phone) }; this.reportViewer.LocalReport.SetParameters(p); this.reportViewer.RefreshReport(); } } }
Form1
for Loading and Printing Orders
This form retrieves order data and opens the print form.
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(); // Query to fetch order and customer details 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(); // Query to fetch order details 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(); // Open print form using(frmPrint frm = new frmPrint(obj, list)) { frm.ShowDialog(); } } } } } }
By following these steps, you can efficiently print orders or receipts using the Report Viewer in C#.
VIDEO TUTORIAL
- How to Fix the Missing ReportViewer Control in the Visual Studio Toolbox
- How to Print Receipt using Report Viewer in C#
- How to create a Chart / Graph using RDLC Report in C#
- How to Create Report Viewer using Stored Procedure in C#
- How to Create QR Code in RDLC Report in C#
- How to Print RDLC Report without Report Viewer in C#
- How to Display an Image in Report Viewer using C#