Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / Visual Basic

How to Print Invoice using VB.NET?

Rate me:
Please Sign up or sign in to vote.
4.89/5 (24 votes)
14 May 2011CPOL1 min read 175.3K   22.9K   44   27
This is a trial to print Invoice with VB.NET

PrintInvoice_VBNET/Img027.JPG

Introduction

How to use VB.NET to print Invoice? This is a trial to print Invoice with VB.NET.

You can read another article (How to use VB6 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

We also need three classes for printing:

  • System.Windows.Forms.PrintDialog
  • System.Windows.Forms.PrintPreviewDialog
  • System.Drawing.Printing.PrintDocument

Of course, you can use any database file instead of Northwind.mdb and change my code to connect with your database file. You can also change my SQL string to bind DataGrid with data.

Using the Code

Bind the DataGrid in frmInvoice form with all Orders:

VB.NET
' 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

Bind the DataGrid in frmOrder form with one Order:

VB.NET
'InvoiceOrder is the number of Order which you select:
Dim intOrder As Integer = Int32.Parse(InvoiceOrder)
Dim MyDataFile As String = Application.StartupPath & "\DataFile\Northwind.mdb"
Dim MyPass As String = ""
Dim strCon As String = "provider=microsoft.jet.oledb.4.0;data source=" _
& MyDataFile & ";" & "Jet OLEDB:Database Password=" & MyPass & ";"

Try
   ' Get Invoice Data:
   InvSql = "SELECT [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 " _
   & "FROM Products INNER JOIN [Order Details] " _
   & "ON Products.ProductID=[Order Details].ProductID " _
   & "WHERE [Order Details].OrderID = " & intOrder

   ' 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)

   ' Create a TableStyle to format Datagrid columns.
   ordGrid.TableStyles.Clear()
   Dim tableStyle As DataGridTableStyle = New DataGridTableStyle

   For Each dc As DataColumn In dTable.Columns
      Dim txtColumn As DataGridTextBoxColumn = New DataGridTextBoxColumn
      txtColumn.MappingName = dc.ColumnName
      txtColumn.HeaderText = dc.Caption
      Select Case (dc.ColumnName.ToString())
         Case "ProductID" ' Product ID 
            txtColumn.HeaderText = "Product ID"
            txtColumn.Width = 60
         Case "ProductName" ' Product Name 
            txtColumn.HeaderText = "Product Name"
            txtColumn.Width = 110
         Case "UnitPrice" ' Unit Price 
            txtColumn.HeaderText = "Unit Price"
            txtColumn.Format = "0.00"
            txtColumn.Alignment = HorizontalAlignment.Right
            txtColumn.Width = 60
         Case "Discount" ' Discount 
            txtColumn.HeaderText = "Discount"
            txtColumn.Format = "p" 'percent
            txtColumn.Alignment = HorizontalAlignment.Right
            txtColumn.Width = 60
         Case "Quantity" ' Quantity 
            txtColumn.HeaderText = "Quantity"
            txtColumn.Alignment = HorizontalAlignment.Right
             txtColumn.Width = 50
         Case "ExtendedPrice" ' Extended Price 
            txtColumn.HeaderText = "Extended Price"
            txtColumn.Format = "0.00"
            txtColumn.Alignment = HorizontalAlignment.Right
            txtColumn.Width = 90
      End Select
      tableStyle.GridColumnStyles.Add(txtColumn)
   Next

   tableStyle.MappingName = dTable.TableName
   ordGrid.TableStyles.Add(tableStyle)
   ' set DataSource of DataGrid 
   ordGrid.DataSource = dTable.DefaultView
Catch ex As Exception
   MessageBox.Show(ex.ToString())
End Try

Declare and initialize three instances for printing:

VB.NET
Private prnDialog As System.Windows.Forms.PrintDialog
Private prnPreview As System.Windows.Forms.PrintPreviewDialog
Private prnDocument As System.Drawing.Printing.PrintDocument

Me.prnDialog = New System.Windows.Forms.PrintDialog
Me.prnPreview = New System.Windows.Forms.PrintPreviewDialog
Me.prnDocument = New System.Drawing.Printing.PrintDocument
' the Event of 'PrintPage'
AddHandler prnDocument.PrintPage, AddressOf prnDocument_PrintPage

To draw something on the report (as line or text):

  1. Get Left Margin, Right Margin, Top Margin, Bottom Margin, Report Width and Report Height:
    VB.NET
    ' Result of the Event 'PrintPage'
    Private Sub prnDocument_PrintPage(ByVal sender As System.Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        leftMargin = Convert.ToInt32_
        (e.MarginBounds.Left) ' leftMargin, rightMargin, ... Declared before
        rightMargin = Convert.ToInt32(e.MarginBounds.Right)
        topMargin = Convert.ToInt32(e.MarginBounds.Top)
        bottomMargin = Convert.ToInt32(e.MarginBounds.Bottom)
        InvoiceWidth = Convert.ToInt32(e.MarginBounds.Width)
        InvoiceHeight = Convert.ToInt32(e.MarginBounds.Height)
        ' Draw Invoice Head
        SetInvoiceHead(e.Graphics)
    End Sub
  2. Set Font and Color:
    VB.NET
    Dim InvTitleFont As Font = New Font("Arial", 24, FontStyle.Regular)
    Dim HeadBrush As SolidBrush = New SolidBrush(Color.Blue)
  3. Set Font Height and Font Width and coordinate, then use DrawString method:

    VB.NET
    Private Sub SetInvoiceHead(ByVal g As Graphics)
        'Invoice title:
        Dim InvTitle As String = "International Food Company"
        'Title Font:
        Dim InvTitleFont As Font = New Font("Arial", 24, FontStyle.Regular)
        'Title Color:
        Dim HeadBrush As SolidBrush = New SolidBrush(Color.Blue)
        'Title Height:
        Dim InvTitleHeight As Integer = Convert.ToInt32(InvTitleFont.GetHeight(g))
        'Title Length:
        Dim lenInvTitle As Integer = Convert.ToInt32(g.MeasureString_
    	(InvTitle, InvTitleFont).Width)
        'Coordinate:
        Dim CurrentX As Integer = leftMargin + 
        (InvoiceWidth - lenInvTitle) / 2 'to set the title in center 
        Dim CurrentY As Integer = topMargin + InvTitleHeight 
        'draw the title:
        g.DrawString(InvTitle, InvTitleFont, HeadBrush, CurrentX, CurrentY)
    End Sub

The project has several pieces of code in three forms. Please read the code, then run the program to see the result. You can read about:

  • 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?

If you have any ideas or if you find any problems, please tell me.

You can read my next article to see how to print invoice using C#.

I add another project for VB.NET 2010.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to Print this output directly to Microsoft Print PDF Pin
Sanjay Jarad8-Oct-22 21:22
Sanjay Jarad8-Oct-22 21:22 
QuestionHow to set papper size, and how to bring reminning data in next papper Pin
Member 441333313-Nov-19 23:42
Member 441333313-Nov-19 23:42 
QuestionVery good article Pin
S475322-Dec-18 19:50
S475322-Dec-18 19:50 
QuestionError when follow your source code Pin
Hendrat Moko4-Nov-18 4:33
Hendrat Moko4-Nov-18 4:33 
QuestionNice Article- I have one clarification Pin
VigneshKumar9575-May-17 4:17
VigneshKumar9575-May-17 4:17 
Praisethank you Pin
Member 122376403-Jan-16 20:50
Member 122376403-Jan-16 20:50 
QuestionUsing stored proccedure Pin
Member 1163936724-Apr-15 4:50
Member 1163936724-Apr-15 4:50 
QuestionProblem On Paper Height Pin
vignesh9571-Jul-13 19:32
vignesh9571-Jul-13 19:32 
AnswerRe: Problem On Paper Height Pin
VigneshKumar9575-May-17 4:00
VigneshKumar9575-May-17 4:00 
GeneralMy vote of 5 Pin
MohamedKamalPharm21-Jun-13 11:47
MohamedKamalPharm21-Jun-13 11:47 
GeneralMy vote of 5 Pin
fakatkardeepali15-Mar-13 4:41
fakatkardeepali15-Mar-13 4:41 
QuestionVB.Net Print Invoice Pin
Remoddn5-Mar-13 20:53
Remoddn5-Mar-13 20:53 
Questiongreat job Pin
3bo0od2-Dec-12 3:08
3bo0od2-Dec-12 3:08 
Questionexcel data instead of mdb? Pin
Member 837876923-Nov-12 23:58
Member 837876923-Nov-12 23:58 
QuestionPrinting in different Paper sizes Pin
Praveen2121219-Oct-12 7:02
Praveen2121219-Oct-12 7:02 
AnswerRe: Printing in different Paper sizes Pin
Mostafa Kaisoun20-Oct-12 8:07
Mostafa Kaisoun20-Oct-12 8:07 
BugMy First Browser Tree Program in VB.NET Pin
kimthaohg854-Aug-11 3:16
kimthaohg854-Aug-11 3:16 
GeneralRe: My First Browser Tree Program in VB.NET Pin
Mostafa Kaisoun4-Aug-11 10:34
Mostafa Kaisoun4-Aug-11 10:34 
GeneralMy vote of 5 Pin
Sunasara Imdadhusen17-Jun-11 19:00
professionalSunasara Imdadhusen17-Jun-11 19:00 
GeneralRe: My vote of 5 Pin
Mostafa Kaisoun18-Jun-11 22:34
Mostafa Kaisoun18-Jun-11 22:34 
GeneralExcellent!! I have a question Pin
Sunasara Imdadhusen17-Jun-11 18:58
professionalSunasara Imdadhusen17-Jun-11 18:58 
GeneralRe: Excellent!! I have a question Pin
Mostafa Kaisoun18-Jun-11 22:38
Mostafa Kaisoun18-Jun-11 22:38 
GeneralMy vote of 2 Pin
lobotomy5-Jun-11 21:06
professionallobotomy5-Jun-11 21:06 
GeneralMy vote of 5 Pin
Minhajul Shaoun20-May-11 11:08
Minhajul Shaoun20-May-11 11:08 
GeneralRe: My vote of 5 Pin
Mostafa Kaisoun26-May-11 5:48
Mostafa Kaisoun26-May-11 5:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.