Public Class frmteacher
    Dim SelectedUserId = 0
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        myInsert("INSERT INTO `tblteachers`(`IMPLOYEEID`, `TEACHERSNAME`) VALUES ( '" & TextBox1.Text & "', '" & TextBox2.Text & "')")
        mySelectQuery("SELECT `TEACHERSID`, `IMPLOYEEID`, `TEACHERSNAME` FROM `tblteachers`")
        SelectedUserId = dgv.CurrentRow.Cells(0).Value
        Checkresult("add")
        LoadData(dgv, "addteacher")
    End Sub

    Private Sub frmteacher_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        mySelectQuery("SELECT `TEACHERSID`, `IMPLOYEEID`, `TEACHERSNAME` FROM `tblteachers`")
        LoadData(dgv, "addteacher")
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        myDelete("DELETE FROM `tblteachers` WHERE `TEACHERSID`=" & SelectedUserId)
        TextBox1.Text = ""
        TextBox2.Text = ""
       
        mySelectQuery("SELECT `TEACHERSID`, `IMPLOYEEID`, `TEACHERSNAME` FROM `tblteachers`")
        LoadData(dgv, "addteacher")
    End Sub

    Private Sub dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellClick
        SelectedUserId = dgv.CurrentRow.Cells(0).Value
        TextBox1.Text = dgv.CurrentRow.Cells(1).Value
        TextBox2.Text = dgv.CurrentRow.Cells(2).Value
      
    End Sub

   
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        myUpdate("UPDATE `tblteachers` SET `TEACHERSID`='" & SelectedUserId & "',`IMPLOYEEID`='" & TextBox1.Text & "',`TEACHERSNAME`='" & TextBox2.Text & "' WHERE `TEACHERSID`=" & SelectedUserId)
        Checkresult("Update")
        mySelectQuery("SELECT `TEACHERSID`, `IMPLOYEEID`, `TEACHERSNAME` FROM `tblteachers`")
        LoadData(dgv, "addteacher")
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
    End Sub
End Class