Introduction
This is a trial to print Invoice with VB.Net, this project lets you know:
- How to create a report using PrintPreviewDialog control and PrintDocument control?
- How to draw Invoice head?
- How to draw the table of products and its price?
- How to compute and draw Invoice total?
You can read my article about
How to use C# to print Invoice.

My Project has three Forms:
- frmInvoice: to bind DataGrid with all Orders from Northwind database file.
- frmInput: to choose one Order which you want to print its Invoice.
- frmOrder: to display Invoice on DataGrid, then you can Print Preview or Print the Invoice as Report.
Also, we need three classes for printing:
System.Windows.Forms.PrintDialog, System.Windows.Forms.PrintPreviewDialog and
System.Drawing.Printing.PrintDocument.
Of course you can use any database file instead Northwind.mdb and change my code
to connect with your database file, also you can change my SQL string to bind
DataGrid with data.
About the Code
Bind the DataGrid in frmInvoice form with all Orders:
' following lines to connect with
access database file 'Northwind.mdb'
Dim
MyPass As String
= ""
Dim
MyDataFile As
String =
Application.StartupPath & "\DataFile\Northwind.mdb"
Dim
strCon
As String
=
"provider=microsoft.jet.oledb.4.0;data source=" _
& MyDataFile & ";" & "Jet OLEDB:Database Password=" & MyPass & ";"
' If you are using SQL Server, please replace previous lines
with following:
Dim
strCon As String
= "provider=sqloledb;Data Source=PC;Initial Catalog=" _
& "Northwind;Integrated Security=SSPI" & ";"
' and replace 'Data Source=PC' with the name of your
system
Try
' Get data from tables: Orders, Customers, Employees,
Products, Order Details:
Dim
InvSql As String
= "SELECT Customers.CompanyName, Customers.City, " _
& "Employees.FirstName
+
Space(1) +
Employees.LastName AS Salesperson, " _
& "Orders.OrderID,
Orders.OrderDate, " _
& "[Order
Details].ProductID, Products.ProductName, [Order Details].UnitPrice, " _
& "[Order
Details].Quantity, [Order Details].Discount, " _
& "CCur([Order
Details].UnitPrice*[Quantity]*(1-[Discount])/100)*100 AS ExtendedPrice, " _
& "Orders.Freight " _
& "FROM Products INNER
JOIN ((Employees INNER JOIN " _
& "(Customers INNER
JOIN Orders ON Customers.CustomerID = Orders.CustomerID) " _
& "ON
Employees.EmployeeID = Orders.EmployeeID) " _
& "INNER JOIN [Order
Details] ON Orders.OrderID = [Order Details].OrderID) " _
& "ON
Products.ProductID = [Order Details].ProductID;"
' create an OleDbDataAdapter
Dim
datAdp As
OleDbDataAdapter =
New
OleDbDataAdapter(InvSql, strCon)
' create a command builder
Dim
cBuilder As
OleDbCommandBuilder =
New
OleDbCommandBuilder(datAdp)
' create a DataTable to hold the query results
Dim
dTable As
DataTable = New
DataTable
' fill the DataTable
datAdp.Fill(dTable)
' set DataSource of DataGrid
datGrid.DataSource =
dTable
Catch
ex As
Exception
MessageBox.Show(ex.ToString())
End Try

Join the conversation! Your thoughts help the community grow.