Vbnet+billing+software+source+code | [extra Quality]
Public Sub CalculateTotal() Dim sum As Double = 0 For i As Integer = 0 To DataGridView1.Rows.Count - 1 sum += Convert.ToDouble(DataGridView1.Rows(i).Cells("Amount").Value) Next txtGrandTotal.Text = sum.ToString("N2") End Sub Use code with caution. Copied to clipboard Database Schema Design
For your source code to work efficiently, your database should have at least three tables: Products Table: ProductName StockQuantity Sales Table: CustomerName TotalAmount SalesDetails Table: Why Use VB.NET for Billing?
Private Sub btnGenerateInvoice_Click(sender As Object, e As EventArgs) Handles btnGenerateInvoice.Click If cartTable.Rows.Count = 0 Then MessageBox.Show("Cart is empty!") Return End If
For Each row As DataRow In dtDetails.Rows subTotal += CDec(row("TaxableValue")) totalCGST += CDec(row("CGST")) totalSGST += CDec(row("SGST")) Next
Download a sample project such as this Store-Billing-System or Billing-System . vbnet+billing+software+source+code
Imports System.Data.OleDb Public Class Form1 ' Define database connection string (Place BillingDB.accdb in your bin/Debug folder) Private connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BillingDB.accdb;" Private conn As New OleDbConnection(connString) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ClearProductInputs() lblGrandTotal.Text = "₹ 0.00" End Sub ' Event: Fetch product details automatically when ProductID is entered Private Sub txtProductID_TextChanged(sender As Object, e As EventArgs) Handles txtProductID.TextChanged If txtProductID.Text.Trim().Length > 0 Then Try If conn.State = ConnectionState.Closed Then conn.Open() Dim query As String = "SELECT ProductName, Price FROM Products WHERE ProductID = @ID" Using cmd As New OleDbCommand(query, conn) cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(txtProductID.Text)) Using reader As OleDbDataReader = cmd.ExecuteReader() If reader.Read() Then txtProductName.Text = reader("ProductName").ToString() txtPrice.Text = reader("Price").ToString() txtQuantity.Text = "1" ' Default quantity Else txtProductName.Clear() txtPrice.Clear() txtQuantity.Clear() End If End Using End Using Catch ex As Exception ' Fail silently during typing to avoid intrusive popups Finally conn.Close() End Try End If End Sub ' Event: Add validated item to DataGridView Private Sub btnAddToGrid_Click(sender As Object, e As EventArgs) Handles btnAddToGrid.Click If txtProductName.Text = "" Or txtPrice.Text = "" Or txtQuantity.Text = "" Then MessageBox.Show("Please select a valid product and enter quantity.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub End If Dim price As Decimal = Convert.ToDecimal(txtPrice.Text) Dim qty As Integer = Convert.ToInt32(txtQuantity.Text) Dim total As Decimal = price * qty ' Append row directly to DataGridView dgvBill.Rows.Add(txtProductID.Text, txtProductName.Text, price, qty, total) CalculateGrandTotal() ClearProductInputs() End Sub ' Helper: Calculate running invoice total Private Sub CalculateGrandTotal() Dim grandTotal As Decimal = 0 For Each row As DataGridViewRow In dgvBill.Rows If Not row.IsNewRow Then grandTotal += Convert.ToDecimal(row.Cells(4).Value) End If Next lblGrandTotal.Text = "₹ " & grandTotal.ToString("F2") End Sub ' Helper: Clear fields for next entry Private Sub ClearProductInputs() txtProductID.Clear() txtProductName.Clear() txtPrice.Clear() txtQuantity.Clear() txtProductID.Focus() End Sub ' Event: Save checkout data and process standard inventory deduction Private Sub btnPrintBill_Click(sender As Object, e As EventArgs) Handles btnPrintBill.Click If dgvBill.Rows.Count = 0 Or (dgvBill.Rows.Count = 1 And dgvBill.Rows(0).IsNewRow) Then MessageBox.Show("The cart is empty.", "Checkout Error", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub End If Try If conn.State = ConnectionState.Closed Then conn.Open() ' Generate a pseudo-random invoice number for tracking Dim rand As New Random() Dim invoiceNo As Integer = rand.Next(100000, 999999) For Each row As DataGridViewRow In dgvBill.Rows If Not row.IsNewRow Then Dim prodID As Integer = Convert.ToInt32(row.Cells(0).Value) Dim prodName As String = row.Cells(1).Value.ToString() Dim price As Decimal = Convert.ToDecimal(row.Cells(2).Value) Dim qty As Integer = Convert.ToInt32(row.Cells(3).Value) Dim total As Decimal = Convert.ToDecimal(row.Cells(4).Value) ' 1. Insert transaction details log Dim insertQuery As String = "INSERT INTO InvoiceDetails (InvoiceNo, ProductName, Price, Quantity, [Total]) VALUES (@Inv, @Name, @Price, @Qty, @Tot)" Using cmdInsert As New OleDbCommand(insertQuery, conn) cmdInsert.Parameters.AddWithValue("@Inv", invoiceNo) cmdInsert.Parameters.AddWithValue("@Name", prodName) cmdInsert.Parameters.AddWithValue("@Price", price) cmdInsert.Parameters.AddWithValue("@Qty", qty) cmdInsert.Parameters.AddWithValue("@Tot", total) cmdInsert.ExecuteNonQuery() End Using ' 2. Deduct inventory stock balances Dim updateStockQuery As String = "UPDATE Products SET Stock = Stock - @Qty WHERE ProductID = @ID" Using cmdUpdate As New OleDbCommand(updateStockQuery, conn) cmdUpdate.Parameters.AddWithValue("@Qty", qty) cmdUpdate.Parameters.AddWithValue("@ID", prodID) cmdUpdate.ExecuteNonQuery() End Using End If Next MessageBox.Show("Transaction saved successfully! Invoice No: " & invoiceNo, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information) dgvBill.Rows.Clear() lblGrandTotal.Text = "₹ 0.00" Catch ex As Exception MessageBox.Show("Database Error: " & ex.Message, "Execution Failed", MessageBoxButtons.OK, MessageBoxIcon.Error) Finally conn.Close() End Try End Sub End Class Use code with caution. 🛠️ Optimisations for Enterprise Scaling
Check if RequestedQty <= StockQty before adding to the grid.
This form allows the cashier to add products, calculate GST, and generate the invoice.
Always use SqlCommand with parameters rather than concatenating strings to prevent database attacks. Public Sub CalculateTotal() Dim sum As Double =
This example demonstrates the logic for adding items to a "Cart" (DataGridView) and calculating the total live. 1. The Variable Setup
Public Function CreateInvoice(customerId As Integer, items As List(Of InvoiceItem)) As Boolean Dim queryInvoice As String = "INSERT INTO tbl_Invoices (Date, CustomerID, TotalAmount) VALUES (@Date, @CID, @Total); SELECT SCOPE_IDENTITY();" Dim queryItem As String = "INSERT INTO tbl_InvoiceItems (InvoiceID, ProductID, Quantity, UnitPrice) VALUES (@IID, @PID, @Qty, @Price);" Dim queryUpdateStock As String = "UPDATE tbl_Products SET StockQty = StockQty - @Qty WHERE ProductID = @PID;"
While this article provides the complete logic, I recommend you type every line yourself to understand the flow. Only by debugging will you master billing software architecture.
Creating billing software in VB.NET is an excellent way to understand transaction handling, foreign keys, and real-time calculations. The source code provided above gives you a professional starting point. You can extend it to support multi-currency, discounts, or even cloud sync via Web API. Imports System
Using conn As SqlConnection = dbHelper.GetConnection() conn.Open() Dim transaction As SqlTransaction = conn.BeginTransaction()
Try ' Insert into Invoice Master Dim masterCmd As New SqlCommand("INSERT INTO tbl_Invoice_Master (InvoiceNo, InvoiceDate, CustomerID, SubTotal, TotalCGST, TotalSGST, GrandTotal) VALUES (@invNo, @date, @custId, @sub, @cgst, @sgst, @grand)", conn, transaction) masterCmd.Parameters.AddWithValue("@invNo", txtInvoiceNo.Text) masterCmd.Parameters.AddWithValue("@date", Convert.ToDateTime(txtInvoiceDate.Text)) masterCmd.Parameters.AddWithValue("@custId", cmbCustomer.SelectedValue) masterCmd.Parameters.AddWithValue("@sub", CDec(lblSubtotal.Text)) masterCmd.Parameters.AddWithValue("@cgst", CDec(lblTotalCGST.Text)) masterCmd.Parameters.AddWithValue("@sgst", CDec(lblTotalSGST.Text)) masterCmd.Parameters.AddWithValue("@grand", CDec(lblGrandTotal.Text)) masterCmd.ExecuteNonQuery()
Building a Retail Billing Software in VB.NET: A Complete Guide with Source Code