Public Sub CloseConnection() If conn.State = ConnectionState.Open Then conn.Close() End If End Sub
While C# gets a lot of the spotlight, VB.NET is still a powerhouse for business logic.
Private Sub UpdateTotal(amount As Double) totalBill += amount lblGrandTotal.Text = "Total: $" & totalBill.ToString("N2") End Sub
' Reduce stock Dim updateStock As String = $"UPDATE tbl_Products SET StockQuantity = StockQuantity - qty WHERE ProductID = productID" ExecuteNonQuery(updateStock) Next vbnet+billing+software+source+code
Private Sub CalculateTotals() Subtotal = 0 For Each item In billItems Subtotal += item.Total Next
Developing desktop-based retail and enterprise solutions remains highly efficient using VB.NET and Windows Forms. This article provides a comprehensive guide to building a robust billing system from scratch, complete with database architecture, user interface design, and core source code components. Architecture of a VB.NET Billing System
: Writing VB.NET code to perform arithmetic calculations, handle events like button clicks, and manage data navigation. Resources for Source Code Public Sub CloseConnection() If conn
Here are some final recommendations to guide your success with billing software:
A robust billing system is defined by its core functionalities, which you can assemble from available source code:
Private Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click Dim qty As Integer = Integer.Parse(txtQuantity.Text) Dim price As Decimal = Decimal.Parse(txtPrice.Text) Dim product As String = txtProduct.Text Architecture of a VB
Downloading a project and trying to make it work is just the first step. To truly learn and adapt it, follow this systematic approach:
Imports System.Data.SqlClient Public Class frmBilling Private TaxRate As Decimal = 0.18D ' 18% Tax Rate Private Sub frmBilling_Load(sender As Object, e As EventArgs) Handles MyBase.Load GenerateInvoiceNumber() InitializeCartGrid() End Sub Private Sub InitializeCartGrid() With dgvCart .Columns.Clear() .Columns.Add("ProdID", "ID") .Columns.Add("ProdCode", "Code") .Columns.Add("ProdName", "Item Name") .Columns.Add("Qty", "Qty") .Columns.Add("Rate", "Price") .Columns.Add("Total", "Total") .Columns("ProdID").Visible = False End With End Sub Private Sub GenerateInvoiceNumber() Try OpenConnection() Dim cmd As New SqlCommand("SELECT TOP 1 InvoiceNo FROM Invoices ORDER BY InvoiceID DESC", conn) Dim lastNo As Object = cmd.ExecuteScalar() If lastNo IsNot Nothing Then Dim num As Integer = Integer.Parse(lastNo.ToString().Replace("INV-", "")) + 1 txtInvoiceNo.Text = "INV-" & num.ToString("D5") Else txtInvoiceNo.Text = "INV-00001" End If Catch ex As Exception MessageBox.Show("Error generating invoice number: " & ex.Message) Finally CloseConnection() End Try End Sub Private Sub txtProductCode_KeyDown(sender As Object, e As KeyEventArgs) Handles txtProductCode.KeyDown If e.KeyCode = Keys.Enter AndAlso Not String.IsNullOrWhiteSpace(txtProductCode.Text) Then FetchProductDetails(txtProductCode.Text.Trim()) End If End Sub Private Sub FetchProductDetails(code As String) Try OpenConnection() Dim cmd As New SqlCommand("SELECT ProductID, ProductName, UnitPrice, StockQty FROM Products WHERE ProductCode = @Code", conn) cmd.Parameters.AddWithValue("@Code", code) Dim reader As SqlDataReader = cmd.ExecuteReader() If reader.Read() Then Dim stock As Integer = Convert.ToInt32(reader("StockQty")) If stock <= 0 Then MessageBox.Show("Item out of stock!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub End If lblProdID.Text = reader("ProductID").ToString() txtProductName.Text = reader("ProductName").ToString() txtRate.Text = reader("UnitPrice").ToString() txtQty.Text = "1" txtQty.Focus() Else MessageBox.Show("Product not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If reader.Close() Catch ex As Exception MessageBox.Show(ex.Message) Finally CloseConnection() End Try End Sub Private Sub btnAddToCart_Click(sender As Object, e As EventArgs) Handles btnAddToCart.Click If String.IsNullOrEmpty(lblProdID.Text) Then Return Dim qty As Integer = Integer.Parse(txtQty.Text) Dim rate As Decimal = Decimal.Parse(txtRate.Text) Dim total As Decimal = qty * rate ' Add row to DataGridView dgvCart.Rows.Add(lblProdID.Text, txtProductCode.Text, txtProductName.Text, qty, rate, total) CalculateBillTotals() ClearItemInputs() End Sub Private Sub CalculateBillTotals() Dim subTotal As Decimal = 0 For Each row As DataGridViewRow In dgvCart.Rows If Not row.IsNewRow Then subTotal += Convert.ToDecimal(row.Cells("Total").Value) End If Next Dim tax As Decimal = subTotal * TaxRate Dim grandTotal As Decimal = subTotal + tax txtSubTotal.Text = subTotal.ToString("F2") txtTaxAmount.Text = tax.ToString("F2") txtGrandTotal.Text = grandTotal.ToString("F2") End Sub Private Sub ClearItemInputs() lblProdID.Text = "" txtProductCode.Clear() txtProductName.Clear() txtRate.Clear() txtQty.Clear() txtProductCode.Focus() End Sub Private Sub btnSavePrint_Click(sender As Object, e As EventArgs) Handles btnSavePrint.Click if dgvCart.Rows.Count = 0 Then MessageBox.Show("Cart is empty.") Return End If Dim transaction As SqlTransaction = Nothing Try OpenConnection() transaction = conn.BeginTransaction() ' 1. Insert into Invoices Table Dim cmdInvoice As New SqlCommand("INSERT INTO Invoices (InvoiceNo, SubTotal, TaxAmount, GrandTotal, CashierName) VALUES (@InvNo, @Sub, @Tax, @Grand, @Cashier); SELECT SCOPE_IDENTITY();", conn, transaction) cmdInvoice.Parameters.AddWithValue("@InvNo", txtInvoiceNo.Text) cmdInvoice.Parameters.AddWithValue("@Sub", Decimal.Parse(txtSubTotal.Text)) cmdInvoice.Parameters.AddWithValue("@Tax", Decimal.Parse(txtTaxAmount.Text)) cmdInvoice.Parameters.AddWithValue("@Grand", Decimal.Parse(txtGrandTotal.Text)) cmdInvoice.Parameters.AddWithValue("@Cashier", "Admin") Dim newInvoiceId As Integer = Convert.ToInt32(cmdInvoice.ExecuteScalar()) ' 2. Insert Details & Update Stock For Each row As DataGridViewRow In dgvCart.Rows If Not row.IsNewRow Then Dim prodId As Integer = Convert.ToInt32(row.Cells("ProdID").Value) Dim qty As Integer = Convert.ToInt32(row.Cells("Qty").Value) Dim rate As Decimal = Convert.ToDecimal(row.Cells("Rate").Value) Dim total As Decimal = Convert.ToDecimal(row.Cells("Total").Value) ' Insert detail record Dim cmdDetail As New SqlCommand("INSERT INTO InvoiceDetails (InvoiceID, ProductID, Quantity, Rate, TotalAmount) VALUES (@InvID, @ProdID, @Qty, @Rate, @Tot)", conn, transaction) cmdDetail.Parameters.AddWithValue("@InvID", newInvoiceId) cmdDetail.Parameters.AddWithValue("@ProdID", prodId) cmdDetail.Parameters.AddWithValue("@Qty", qty) cmdDetail.Parameters.AddWithValue("@Rate", rate) cmdDetail.Parameters.AddWithValue("@Tot", total) cmdDetail.ExecuteNonQuery() ' Deduct Inventory Stock Dim cmdStock As New SqlCommand("UPDATE Products SET StockQty = StockQty - @Qty WHERE ProductID = @ProdID", conn, transaction) cmdStock.Parameters.AddWithValue("@Qty", qty) cmdStock.Parameters.AddWithValue("@ProdID", prodId) cmdStock.ExecuteNonQuery() End If Next transaction.Commit() MessageBox.Show("Invoice Saved Successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information) ' Reset UI for next sale dgvCart.Rows.Clear() CalculateBillTotals() GenerateInvoiceNumber() Catch ex As Exception If transaction IsNot Nothing Then transaction.Rollback() MessageBox.Show("Transaction Failed: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Finally CloseConnection() End Try End Sub End Class Use code with caution. UI Layout Configurations