Public Class frmeval
    Dim quest = Nothing
    Private Sub frmeval_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        mySelectQuery("SELECT `TEACHERID`, `TEACHERSNAME`,`GRADE`, `SECTION`,`SUBJECT` FROM `tblschedule` GROUP BY `SECTION` ")

        ' mySelectQuery("SELECT t.`TEACHERSID`, t.TEACHERSNAME FROM `tblschedule` s LEFT JOIN tblteachers t on s.`TEACHERID` = t.TEACHERSID WHERE s.GRADE = " & studgrade & " GROUP BY t.TEACHERSID")
        fillcombo(cbteachersname, "TEACHERSNAME", "TEACHERSID")


        mySelectQuery(" SELECT `QUESTID`, `QUESTION` FROM `tblquestion`")
        LoadData(dgvquest, "quest")
    End Sub

    Private Sub Label5_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim TeachersID = cbteachersname.SelectedValue
        Dim SubjectID = ComboBox2.SelectedValue

        mySelectQuery("SELECT MAX(`ISEVALDONE`) AS `ISEVALDONE` FROM `tblevalstatus` WHERE `IDNO`=" & studid & " AND TEACHERID=" & TeachersID & " AND SUBJECTID=" & SubjectID)
        Dim IsEvalDone = getsvalue()
        Debug.WriteLine("Button2_Click === Q ======>" & "SELECT `ISEVALDONE` FROM `tblevalstatus` WHERE `IDNO`=" & studid)
        Debug.WriteLine("Button2_Click === zR ======>" & IsEvalDone)
        Debug.WriteLine("Button2_Click frmeval =============>" & "INSERT INTO `tblevalstatus`(`IDNO`, `TEACHERID`, `SUBJECTID`, `ISEVALDONE`) VALUES (" & studid & "," & TeachersID & "," & SubjectID & ", true)")

        If Not IsDBNull(IsEvalDone) Then
            MsgBox("Sorry. You can evaluate a teacher for 1 time only.")
        Else
            Try
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If
                con.Open()

                For i As Integer = 0 To dgvquest.Rows.Count - 1 Step +1

                    cmd = New MySql.Data.MySqlClient.MySqlCommand("INSERT INTO `tblanswer`(`TEACHERID`, `STUDENTID`,`QUESTIONID`, `ANSWER`) VALUES (" & TeachersID & ", " & studid & ", @q, @a)")
                    cmd.Parameters.Add("@q", MySql.Data.MySqlClient.MySqlDbType.VarChar).Value = dgvquest.Rows(i).Cells(1).Value()
                    cmd.Parameters.Add("@a", MySql.Data.MySqlClient.MySqlDbType.Int64).Value = dgvquest.Rows(i).Cells(2).Value()

                    cmd.Connection = con
                    cmd.ExecuteNonQuery()

                Next
                myInsert("INSERT INTO `tblcoments`(`TEACHERID`, `COMMENTS`, `DATE`) VALUES ( '" & Val(TeachersID) & "','" & RTBOXCOMMENTS.Text & "', NOW())")
                myInsert("INSERT INTO `tblevalstatus`(`IDNO`, `TEACHERID`, `SUBJECTID`, `ISEVALDONE`) VALUES (" & studid & "," & TeachersID & "," & SubjectID & ", true)")
                MsgBox("Success")
                con.Close()
            Catch ex As Exception
                MessageBox.Show("Please Answer all the Question ")
            End Try
        End If

        
    End Sub

   

    Private Sub cbteachersname_TextChanged(sender As Object, e As EventArgs) Handles cbteachersname.TextChanged
        Dim TeachersID = cbteachersname.SelectedValue
    
        mySelectQuery("SELECT `ID`, `SUBJECT` FROM `tblschedule`  WHERE `TEACHERID`= " & TeachersID.ToString() & " GROUP BY `SUBJECT`")
        fillcombo(ComboBox2, "SUBJECT", "ID")
    End Sub
End Class