Vb.net Billing Software Source Code

Utilizing is an excellent way to develop reliable, customized, and efficient invoicing solutions. By understanding the core components—from database design to user interface implementation—developers can create powerful applications that streamline financial operations.

A DataGridView control named dgvInvoiceItems containing columns: Product ID , Product Name , Unit Price , Quantity , and Total Price .

Dim command As New SqlCommand("INSERT INTO Invoices (CustomerID, InvoiceDate, TotalAmount) VALUES (@customerID, @invoiceDate, @totalAmount)", connection) command.Parameters.AddWithValue("@customerID", customerID) command.Parameters.AddWithValue("@invoiceDate", invoiceDate) command.Parameters.AddWithValue("@totalAmount", totalAmount)

Private Sub PrintInvoice() Dim printDoc As New Printing.PrintDocument() AddHandler printDoc.PrintPage, AddressOf PrintPageHandler Dim printDialog1 As New PrintDialog() printDialog1.Document = printDoc If printDialog1.ShowDialog() = DialogResult.OK Then printDoc.Print() End If End Sub vb.net billing software source code

Replace the code in your main form with the fully integrated backend implementation below. Make sure to adjust the connString variable to match your database server configuration.

To implement and run the source code provided below, ensure your development environment includes:

To enhance this foundational engine into a fully customized commercial application, prioritize implementing these extensions: Utilizing is an excellent way to develop reliable,

Imports System.Data.SqlClient Public Class FormBilling Private SelectedProductID As Integer = 0 Private Sub FormBilling_Load(sender As Object, e As EventArgs) Handles MyBase.Load GenerateInvoiceNumber() ConfigureDataGridView() ClearItemInputs() End Sub Private Sub ConfigureDataGridView() dgvItems.Columns.Clear() dgvItems.Columns.Add("Code", "Product Code") dgvItems.Columns.Add("Name", "Product Name") dgvItems.Columns.Add("Price", "Unit Price") dgvItems.Columns.Add("Qty", "Quantity") dgvItems.Columns.Add("Total", "Line Total") dgvItems.Columns.Add("ID", "ProductID") dgvItems.Columns("ID").Visible = False ' Hidden system column End Sub Private Sub GenerateInvoiceNumber() txtInvoiceNo.Text = "INV-" & DateTime.Now.ToString("yyyyMMddHHmmss") End Sub ' Fetches product details when an operator types or scans a Product Code 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 Using conn As SqlConnection = GetConnection() Dim query As String = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE ProductCode = @Code" Using cmd As New SqlCommand(query, conn) cmd.Parameters.AddWithValue("@Code", txtProductCode.Text.Trim()) Using reader As SqlDataReader = cmd.ExecuteReader() If reader.Read() Then SelectedProductID = Convert.ToInt32(reader("ProductID")) txtProductName.Text = reader("ProductName").ToString() txtUnitPrice.Text = Convert.ToDecimal(reader("UnitPrice")).ToString("F2") txtQuantity.Focus() Else MsgBox("Product not found!", MsgBoxStyle.Exclamation, "Invalid Code") ClearItemInputs() End If End Using End Using End Using e.SuppressKeyPress = True End If End Sub ' Adds the configured line item to the visual DataGridView matrix Private Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click Dim qty As Integer = 0 Dim price As Decimal = 0 If SelectedProductID = 0 OrElse Not Integer.TryParse(txtQuantity.Text, qty) OrElse qty <= 0 Then MsgBox("Please enter a valid product and quantity.", MsgBoxStyle.Warning, "Input Validation") Exit Sub End If Decimal.TryParse(txtUnitPrice.Text, price) Dim lineTotal As Decimal = qty * price ' Append row to grid dgvItems.Rows.Add(txtProductCode.Text, txtProductName.Text, price, qty, lineTotal, SelectedProductID) CalculateInvoiceTotals() ClearItemInputs() txtProductCode.Focus() End Sub Private Sub CalculateInvoiceTotals() Dim subTotal As Decimal = 0 For Each row As DataGridViewRow In dgvItems.Rows If Not row.IsNewRow Then subTotal += Convert.ToDecimal(row.Cells("Total").Value) End If Next Dim taxRate As Decimal = 0 Dim discount As Decimal = 0 Decimal.TryParse(txtTaxRate.Text, taxRate) Decimal.TryParse(txtDiscount.Text, discount) Dim taxAmount As Decimal = subTotal * (taxRate / 100) Dim grandTotal As Decimal = (subTotal + taxAmount) - discount lblSubTotal.Text = subTotal.ToString("F2") lblGrandTotal.Text = grandTotal.ToString("F2") End Sub Private Sub ClearItemInputs() SelectedProductID = 0 txtProductCode.Clear() txtProductName.Clear() txtUnitPrice.Clear() txtQuantity.Clear() End Sub ' Saves the invoice using a secure SQL transaction block Private Sub btnSaveInvoice_Click(sender As Object, e As EventArgs) Handles btnSaveInvoice.Click If dgvItems.Rows.Count = 0 Then MsgBox("Cannot save an empty invoice.", MsgBoxStyle.Warning, "Validation Error") Exit Sub End If Using conn As SqlConnection = GetConnection() Dim tx As SqlTransaction = conn.BeginTransaction() Try Dim masterQuery As String = "INSERT INTO Invoices (InvoiceNumber, CustomerName, SubTotal, TaxAmount, Discount, GrandTotal) " & "VALUES (@InvNo, @Customer, @Sub, @Tax, @Disc, @Grand); SELECT SCOPE_IDENTITY();" Dim newInvoiceID As Integer = 0 Dim subTotal As Decimal = Convert.ToDecimal(lblSubTotal.Text) Dim taxRate As Decimal = 0 Decimal.TryParse(txtTaxRate.Text, taxRate) Dim taxAmt As Decimal = subTotal * (taxRate / 100) Dim discount As Decimal = 0 Decimal.TryParse(txtDiscount.Text, discount) Using cmdMaster As New SqlCommand(masterQuery, conn, tx) cmdMaster.Parameters.AddWithValue("@InvNo", txtInvoiceNo.Text.Trim()) cmdMaster.Parameters.AddWithValue("@Customer", If(String.IsNullOrEmpty(txtCustomer.Text), "Walk-In Customer", txtCustomer.Text.Trim())) cmdMaster.Parameters.AddWithValue("@Sub", subTotal) cmdMaster.Parameters.AddWithValue("@Tax", taxAmt) cmdMaster.Parameters.AddWithValue("@Disc", discount) cmdMaster.Parameters.AddWithValue("@Grand", Convert.ToDecimal(lblGrandTotal.Text)) newInvoiceID = Convert.ToInt32(cmdMaster.ExecuteScalar()) End Using ' Insert details and process real-time stock deductions For Each row As DataGridViewRow In dgvItems.Rows If Not row.IsNewRow Then Dim pID As Integer = Convert.ToInt32(row.Cells("ID").Value) Dim qty As Integer = Convert.ToInt32(row.Cells("Qty").Value) Dim price As Decimal = Convert.ToDecimal(row.Cells("Price").Value) Dim total As Decimal = Convert.ToDecimal(row.Cells("Total").Value) ' Insert item detail record Dim itemQuery As String = "INSERT INTO InvoiceItems (InvoiceID, ProductID, Quantity, UnitPrice, LineTotal) " & "VALUES (@InvID, @ProdID, @Qty, @Price, @Total)" Using cmdItem As New SqlCommand(itemQuery, conn, tx) cmdItem.Parameters.AddWithValue("@InvID", newInvoiceID) cmdItem.Parameters.AddWithValue("@ProdID", pID) cmdItem.Parameters.AddWithValue("@Qty", qty) cmdItem.Parameters.AddWithValue("@Price", price) cmdItem.Parameters.AddWithValue("@Total", total) cmdItem.ExecuteNonQuery() End Using ' Deduct inventory levels safely inside the current transaction block Dim stockQuery As String = "UPDATE Products SET StockQuantity = StockQuantity - @Qty WHERE ProductID = @ProdID" Using cmdStock As New SqlCommand(stockQuery, conn, tx) cmdStock.Parameters.AddWithValue("@Qty", qty) cmdStock.Parameters.AddWithValue("@ProdID", pID) cmdStock.ExecuteNonQuery() End Using End If Next tx.Commit() MsgBox("Invoice saved successfully!", MsgBoxStyle.Information, "Success") ' Reset for next transaction dgvItems.Rows.Clear() GenerateInvoiceNumber() txtCustomer.Clear() CalculateInvoiceTotals() Catch ex As Exception tx.Rollback() MsgBox("Transaction failed: " & ex.Message, MsgBoxStyle.Critical, "Database Write Failure") End Try End Using End Sub Private Sub txtTaxRate_TextChanged(sender As Object, e As EventArgs) Handles txtTaxRate.TextChanged, txtDiscount.TextChanged CalculateInvoiceTotals() End Sub End Class Use code with caution. 5. Security and Performance Optimization Tips

Real-time stock deductions upon invoice generation.

A production-grade billing application requires a structured, multi-tier data flow to ensure transaction data remains accurate and secure. This implementation covers: TextBox for Customer Name

Generating daily sales reports, tax reports, and customer statement summaries.

Place this code directly behind your primary user interface form.

TextBox for Customer Name, DateTimePicker for Invoice Date, and a read-only TextBox for Invoice ID.

Shopping Basket