Vb.net Billing Software Source Code ❲Top 10 Trusted❳

Imports System.Data.SqlClient

Public Class DBConnection Private Shared connectionString As String = "Data Source=localhost;Initial Catalog=BillingSystem;Integrated Security=True" Public Shared conn As SqlConnection = New SqlConnection(connectionString)

Public Shared Function GetConnection() As SqlConnection
    Return conn
End Function
Public Shared Sub OpenConnection()
    If conn.State = ConnectionState.Closed Then
        conn.Open()
    End If
End Sub
Public Shared Sub CloseConnection()
    If conn.State = ConnectionState.Open Then
        conn.Close()
    End If
End Sub

End Class

| Pitfall | Solution | |---------|----------| | Race condition in stock update | Use transactions with SERIALIZABLE isolation level or row-level locks. | | Floating point errors in money | Always use Decimal (not Double) for currency. | | Slow product search | Create non-clustered index on ProductCode and ProductName. | | Bill printing formatting issues | Use PrintPreviewDialog before actual print. | | No offline mode | Store a local SQL Express or SQLite copy with sync logic. | vb.net billing software source code


Create a module modDatabase.vb:

Imports System.Data.SqlClient

Module modDatabase Public conn As SqlConnection Public cmd As SqlCommand Public da As SqlDataAdapter Public dt As DataTable

Public Sub OpenDB()
    conn = New SqlConnection("Data Source=localhost\SQLEXPRESS;Initial Catalog=BillingDB;Integrated Security=True")
    If conn.State = ConnectionState.Closed Then
        conn.Open()
    End If
End Sub
Public Sub CloseDB()
    If conn.State = ConnectionState.Open Then
        conn.Close()
    End If
End Sub

End Module

This is the most critical function. It must ensure that if the invoice master saves, the details save; otherwise, roll back.

Private Sub SaveInvoice()
    Using conn As SqlConnection = getConnection()
        conn.Open()
        Dim transaction As SqlTransaction = conn.BeginTransaction()
        Try
            '1. Insert into tbl_Invoice_Master
            Dim masterQuery As String = "INSERT INTO tbl_Invoice_Master (InvoiceDate, CustomerID, SubTotal, TaxAmount, GrandTotal) " &
                                        "VALUES (@date, @custID, @sub, @tax, @grand); SELECT SCOPE_IDENTITY();"
            Dim newInvoiceNo As Integer = 0
            Using cmdMaster As New SqlCommand(masterQuery, conn, transaction)
                cmdMaster.Parameters.AddWithValue("@date", DateTime.Now)
                cmdMaster.Parameters.AddWithValue("@custID", GetCurrentCustomerID()) 'Function to get selected customer ID
                cmdMaster.Parameters.AddWithValue("@sub", lblSubTotal.Text)
                cmdMaster.Parameters.AddWithValue("@tax", lblTax.Text)
                cmdMaster.Parameters.AddWithValue("@grand", lblGrandTotal.Text)
                newInvoiceNo = Convert.ToInt32(cmdMaster.ExecuteScalar())
            End Using
        '2. Insert into tbl_Invoice_Details for each row in cart
        Dim detailsQuery As String = "INSERT INTO tbl_Invoice_Details (InvoiceNo, ProductID, Quantity, Rate, Total) " &
                                     "VALUES (@invNo, @prodID, @qty, @rate, @total)"
        For Each row As DataGridViewRow In dgvCart.Rows
            Using cmdDetails As New SqlCommand(detailsQuery, conn, transaction)
                cmdDetails.Parameters.AddWithValue("@invNo", newInvoiceNo)
                cmdDetails.Parameters.AddWithValue("@prodID", row.Cells("ProductID").Value)
                cmdDetails.Parameters.AddWithValue("@qty", row.Cells("Quantity").Value)
                cmdDetails.Parameters.AddWithValue("@rate", row.Cells("Rate").Value)
                cmdDetails.Parameters.AddWithValue("@total", row.Cells("Total").Value)
                cmdDetails.ExecuteNonQuery()
'3. Update stock in tbl_Products
                Dim stockQuery As String = "UPDATE tbl_Products SET StockQuantity = StockQuantity - @qty WHERE ProductID = @prodID"
                Using cmdStock As New SqlCommand(stockQuery, conn, transaction)
                    cmdStock.Parameters.AddWithValue("@qty", row.Cells("Quantity").Value)
                    cmdStock.Parameters.AddWithValue("@prodID", row.Cells("ProductID").Value)
                    cmdStock.ExecuteNonQuery()
                End Using
            End Using
        Next
transaction.Commit()
        MessageBox.Show("Invoice saved successfully. Invoice No: " & newInvoiceNo)
        ClearCart()
    Catch ex As Exception
        transaction.Rollback()
        MessageBox.Show("Failed to save invoice: " & ex.Message)
    End Try
End Using

End Sub

The real power of having the source code is the ability to tweak it. No off-the-shelf software understands your business like you do.

This should give you a starting point for creating a simple billing software in VB.NET. Expand on this by adding more features as needed. Imports System


This is a working billing system that you can expand based on your specific requirements!

Creating a comprehensive billing software source code in VB.NET for a full application is quite extensive and complex for a single response. However, I can guide you through a basic example of how to structure a simple billing system. This example will include basic functionalities such as adding items, calculating subtotal, tax, and total.

Scroll to Top