Here's the full code:
Imports System.Data.SqlClient
Public Class Form1
Dim connectionString As String = "Server=(local);Database=TechzoneHardwareShop;Integrated
Security=True;"
Dim connection As New SqlConnection(connectionString)
Dim adapter As SqlDataAdapter
Dim table As New DataTable()
Dim currentIndex As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
connection.Open()
adapter = New SqlDataAdapter("SELECT * FROM Products", connection)
Dim builder As New SqlCommandBuilder(adapter)
adapter.Fill(table)
DataGridView1.DataSource = table
connection.Close()
If table.Rows.Count > 0 Then
txtProductID.Text = table.Rows(currentIndex)("ProductID").ToString()
txtProductName.Text = table.Rows(currentIndex)("ProductName").ToString()
txtQuantity.Text = table.Rows(currentIndex)("Quantity").ToString()
txtPrice.Text = table.Rows(currentIndex)("Price").ToString()
End If
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
table.Rows.Add(txtProductID.Text, txtProductName.Text, txtQuantity.Text, txtPrice.Text)
DataGridView1.DataSource = table
currentIndex = table.Rows.Count - 1
txtProductID.Text = table.Rows(currentIndex)("ProductID").ToString()
txtProductName.Text = table.Rows(currentIndex)("ProductName").ToString()
txtQuantity.Text = table.Rows(currentIndex)("Quantity").ToString()
txtPrice.Text = table.Rows(currentIndex)("Price").ToString()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
adapter.Update(table)
MessageBox.Show("Record saved successfully")
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Try
If currentIndex >= 0 AndAlso currentIndex < table.Rows.Count Then
table.Rows(currentIndex).Delete()
adapter.Update(table)
If table.Rows.Count > 0 Then
currentIndex = 0
txtProductID.Text = table.Rows(currentIndex)("ProductID").ToString()
txtProductName.Text = table.Rows(currentIndex)("ProductName").ToString()
txtQuantity.Text = table.Rows(currentIndex)("Quantity").ToString()
txtPrice.Text = table.Rows(currentIndex)("Price").ToString()
Else
txtProductID.Clear()
txtProductName.Clear()
txtQuantity.Clear()
txtPrice.Clear()
End If
MessageBox.Show("Record deleted successfully")
Else
MessageBox.Show("No record to delete")
End If
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If currentIndex < table.Rows.Count - 1 Then
currentIndex += 1
txtProductID.Text = table.Rows(currentIndex)("ProductID").ToString()
txtProductName.Text = table.Rows(currentIndex)("ProductName").ToString()
txtQuantity.Text = table.Rows(currentIndex)("Quantity").ToString()
txtPrice.Text = table.Rows(currentIndex)("Price").ToString()
End If
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
If currentIndex > 0 Then
currentIndex -= 1
txtProductID.Text = table.Rows(currentIndex)("ProductID").ToString()
txtProductName.Text = table.Rows(currentIndex)("ProductName").ToString()
txtQuantity.Text = table.Rows(currentIndex)("Quantity").ToString()
txtPrice.Text = table.Rows(currentIndex)("Price").ToString()
End If
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles
DataGridView1.CellClick
If e.RowIndex >= 0 Then
currentIndex = e.RowIndex
txtProductID.Text = table.Rows(currentIndex)("ProductID").ToString()
txtProductName.Text = table.Rows(currentIndex)("ProductName").ToString()
txtQuantity.Text = table.Rows(currentIndex)("Quantity").ToString()
txtPrice.Text = table.Rows(currentIndex)("Price").ToString()
End If
End Sub
End Class