Imports System.Data.SqlClientPublic Class frmInvoice Private dtDetails As New DataTable()
Private Sub frmInvoice_Load(sender As Object, e As EventArgs) Handles MyBase.Load txtInvoiceDate.Text = DateTime.Now.ToString("yyyy-MM-dd") LoadInvoiceNumber() LoadCustomers() InitializeDetailsGrid() End Sub Private Sub LoadInvoiceNumber() Dim query As String = "SELECT ISNULL(MAX(CAST(SUBSTRING(InvoiceNo, 3, LEN(InvoiceNo)) AS INT)), 0) + 1 FROM tbl_Invoice_Master WHERE InvoiceNo LIKE 'IN%'" Dim nextNum As Integer = Convert.ToInt32(ExecuteScalar(query)) txtInvoiceNo.Text = "IN" & nextNum.ToString("D6") End Sub Private Sub LoadCustomers() Dim dt As DataTable = GetDataTable("SELECT CustomerID, CustomerName FROM tbl_Customers") cmbCustomer.DisplayMember = "CustomerName" cmbCustomer.ValueMember = "CustomerID" cmbCustomer.DataSource = dt End Sub Private Sub InitializeDetailsGrid() dtDetails.Columns.Add("ProductID", GetType(Integer)) dtDetails.Columns.Add("ProductName", GetType(String)) dtDetails.Columns.Add("Quantity", GetType(Decimal)) dtDetails.Columns.Add("Rate", GetType(Decimal)) dtDetails.Columns.Add("TaxableValue", GetType(Decimal)) dtDetails.Columns.Add("CGST", GetType(Decimal)) dtDetails.Columns.Add("SGST", GetType(Decimal)) dgvDetails.DataSource = dtDetails End Sub Private Sub btnAddProduct_Click(sender As Object, e As EventArgs) Handles btnAddProduct.Click ' Assume a popup product search form returns selected product Dim frm As New frmProductSearch() If frm.ShowDialog() = DialogResult.OK Then Dim newRow As DataRow = dtDetails.NewRow() newRow("ProductID") = frm.SelectedProductID newRow("ProductName") = frm.SelectedProductName newRow("Quantity") = 1 newRow("Rate") = frm.Rate ' Tax calculation will be done row-by-row based on GST% Dim gstPercent As Decimal = frm.GSTPercent Dim taxable As Decimal = newRow("Quantity") * newRow("Rate") newRow("TaxableValue") = taxable newRow("CGST") = Math.Round(taxable * (gstPercent / 100) / 2, 2) newRow("SGST") = Math.Round(taxable * (gstPercent / 100) / 2, 2) dtDetails.Rows.Add(newRow) CalculateTotals() End If End Sub Private Sub CalculateTotals() Dim subTotal As Decimal = 0 Dim totalCGST As Decimal = 0 Dim totalSGST As Decimal = 0 For Each row As DataRow In dtDetails.Rows subTotal += CDec(row("TaxableValue")) totalCGST += CDec(row("CGST")) totalSGST += CDec(row("SGST")) Next lblSubtotal.Text = subTotal.ToString("N2") lblTotalCGST.Text = totalCGST.ToString("N2") lblTotalSGST.Text = totalSGST.ToString("N2") lblGrandTotal.Text = (subTotal + totalCGST + totalSGST).ToString("N2") End Sub Private Sub btnSaveInvoice_Click(sender As Object, e As EventArgs) Handles btnSaveInvoice.Click If dtDetails.Rows.Count = 0 Then MessageBox.Show("Add at least one product") Return End If Using conn As New SqlConnection(ConnectionString) conn.Open() Dim transaction = 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() ' Insert Details For Each row As DataRow In dtDetails.Rows Dim detailCmd As New SqlCommand("INSERT INTO tbl_Invoice_Details (InvoiceNo, ProductID, Quantity, Rate, TaxableValue, CGST_Amount, SGST_Amount) VALUES (@invNo, @pid, @qty, @rate, @taxable, @cgst, @sgst)", conn, transaction) detailCmd.Parameters.AddWithValue("@invNo", txtInvoiceNo.Text) detailCmd.Parameters.AddWithValue("@pid", row("ProductID")) detailCmd.Parameters.AddWithValue("@qty", row("Quantity")) detailCmd.Parameters.AddWithValue("@rate", row("Rate")) detailCmd.Parameters.AddWithValue("@taxable", row("TaxableValue")) detailCmd.Parameters.AddWithValue("@cgst", row("CGST")) detailCmd.Parameters.AddWithValue("@sgst", row("SGST")) detailCmd.ExecuteNonQuery() Next transaction.Commit() MessageBox.Show("Invoice Saved Successfully!") Me.Close() Catch ex As Exception transaction.Rollback() MessageBox.Show("Error: " & ex.Message) End Try End Using End Sub
End Class
Create a database named BillingDB and run the following scripts:
CREATE TABLE tbl_InvoiceDetails (
ID INT PRIMARY KEY IDENTITY,
InvoiceNo NVARCHAR(20) FOREIGN KEY REFERENCES tbl_Invoices(InvoiceNo),
ProductID INT FOREIGN KEY REFERENCES tbl_Products(ProductID),
Quantity INT,
Price DECIMAL(18,2),
GST_Amount DECIMAL(18,2),
Total DECIMAL(18,2)
);
Add a PrintDocument control and its PrintPage event. vbnet+billing+software+source+code
Private WithEvents pd As New Printing.PrintDocument Private invoiceContent As StringPrivate Sub PrintInvoice(ByVal invNo As String) ' Build invoice text Dim sb As New System.Text.StringBuilder() sb.AppendLine(" MY BILLING SOFTWARE ") sb.AppendLine("--------------------------") sb.AppendLine($"Invoice No: invNo") sb.AppendLine($"Date: DateTime.Now") sb.AppendLine("--------------------------") sb.AppendLine("Item Qty Price Total") For Each row As DataRow In cartTable.Rows sb.AppendLine($"row("ProductName") row("Quantity") row("Price") row("Total")") Next sb.AppendLine("--------------------------") sb.AppendLine($"Grand Total: lblGrandTotal.Text") sb.AppendLine("--------------------------") sb.AppendLine(" Thank you! ")
invoiceContent = sb.ToString() pd.Print()End Sub
Private Sub pd_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles pd.PrintPage Dim font As New Font("Courier New", 10) Dim yPos As Single = 10 Dim leftMargin As Single = e.MarginBounds.Left Dim lines As String() = invoiceContent.Split(Environment.NewLine)
For Each line As String In lines e.Graphics.DrawString(line, font, Brushes.Black, leftMargin, yPos) yPos += font.GetHeight(e.Graphics) Next
End Sub
If you are searching for "VB.NET billing software source code," be careful of "spaghetti code" repositories. Here are reliable places to look: Imports System