Public Class frmaddstudent
    Dim SelectedUserId = 0
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        myInsert("INSERT INTO `tblstudents`( `STUDENT NAME`, `GRADE`, `SECTION`, `IDNO`) VALUES ( '" & TextBox1.Text & "', '" & TextBox2.Text & "','" & TextBox3.Text & "', '" & TextBox4.Text & "')")
        mySelectQuery("SELECT `STUDENTID`,`STUDENT NAME`, `GRADE`, `SECTION`, `IDNO` FROM `tblstudents")
        SelectedUserId = dgv.CurrentRow.Cells(0).Value
        Checkresult("add")
        LoadData(dgv, "addstudent")

    End Sub

    Private Sub frmaddstudent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        mySelectQuery("SELECT `STUDENTID`,`STUDENT NAME`, `GRADE`, `SECTION`, `IDNO` FROM `tblstudents")

        LoadData(dgv, "addstudent")
    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
        TextBox3.Text = dgv.CurrentRow.Cells(3).Value
        TextBox4.Text = dgv.CurrentRow.Cells(4).Value
    End Sub

 

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If SelectedUserId > 0 Then

            myUpdate("UPDATE `tblstudents` SET `STUDENTID`='" & SelectedUserId & "', `STUDENT NAME`='" & TextBox1.Text & "',`GRADE`='" & TextBox2.Text & "',`SECTION`='" & TextBox3.Text & "',`IDNO`='" & TextBox4.Text & "' WHERE `STUDENTID`=" & SelectedUserId)

            Checkresult("Update")
            mySelectQuery("SELECT `STUDENTID`,`STUDENT NAME`, `GRADE`, `SECTION`, `IDNO` FROM `tblstudents")

            LoadData(dgv, "addstudent")
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        myDelete("DELETE FROM `tblstudents` WHERE `STUDENTID`=" & SelectedUserId)
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        mySelectQuery("SELECT `STUDENTID`,`STUDENT NAME`, `GRADE`, `SECTION`, `IDNO` FROM `tblstudents")

        LoadData(dgv, "addstudent")
    End Sub

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


End Class