Here's the updated code that uses MySQL instead of SQL Server:
Imports MySql.Data.MySqlClient
Public Class Form1
Dim connectionString As String = "Server=localhost;Port=3306;Database=Hardware;User
Id=root;Password=mditbaihm2006;"
Dim connection As New MySqlConnection(connectionString)
Dim adapter As MySqlDataAdapter
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 MySqlDataAdapter("SELECT * FROM Products", connection)
Dim builder As New MySqlCommandBuilder(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
connection.Open()
adapter.Update(table)
connection.Close()
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()
connection.Open()
adapter.Update(table)
connection.Close()
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