Hi!
I am having trouble with my code...can anyone help me look at it where in my code is giving the problem.
I am trying to assign subjects to students, I do it by first
1. I select from radiobutton if Grade School or High School
2. then all names of HS (if I select HS) will display on my combobox and select from there the student I want to assign with subject
3. In another combobox, I select the year level of the student.
4. then I click a button to display the list of subjects for that certain year level in datagridview
5. and lastly, I will click another button to register/save/assign said subject to the student.
6. I repeat, steps 1 to 5 for another student...
The first time I do the step it is successful but for the second time around it just save/assign the last subject listed in the datagridview. Sometimes also, it is successful for quite a number of students and back to the problem again.
I would appreciate it very much for any help you can extend to me...thank you in advance!
Here's my code:
I am having trouble with my code...can anyone help me look at it where in my code is giving the problem.
I am trying to assign subjects to students, I do it by first
1. I select from radiobutton if Grade School or High School
2. then all names of HS (if I select HS) will display on my combobox and select from there the student I want to assign with subject
3. In another combobox, I select the year level of the student.
4. then I click a button to display the list of subjects for that certain year level in datagridview
5. and lastly, I will click another button to register/save/assign said subject to the student.
6. I repeat, steps 1 to 5 for another student...
The first time I do the step it is successful but for the second time around it just save/assign the last subject listed in the datagridview. Sometimes also, it is successful for quite a number of students and back to the problem again.
I would appreciate it very much for any help you can extend to me...thank you in advance!
Here's my code:
Code:
Imports System.Data.SqlClient
Public Class frmAssignSubjects
Private cnn As New SqlConnection(My.Settings.MyConnectionString)
Private DeptStr As String = ""
Private IDStr As String = ""
Private Curr As String = ""
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub frmAssignSubjects_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
If cnn.State = ConnectionState.Open Then cnn.Close()
End Sub
Private Sub frmAssignSubjects_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = (MessageBox.Show("Are you sure you want to quit?", "QUIT", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) <> Windows.Forms.DialogResult.Yes)
End Sub
Private Sub frmAssignSubjects_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If cnn.State = ConnectionState.Closed Then cnn.Open()
If rdoHS.Checked = True Then
DisplayRecordsToCombo()
ElseIf rdoElem.Checked Then
DisplayRecordsToCombo()
End If
txtSubjectName.Hide()
txtSubjectID.Hide()
End Sub
Private Function GetStudList() As DataTable
Using cmdStudList As New SqlCommand("SELECT IDNo, " & _
" LastName + ', ' + FirstName + ' ' + IsNull(MI,'') AS StudentName, " & _
" LastName, FirstName, MI, CompleteAddress, Gender, Citizenship, " & _
" ContactPerson, ContactRelation, ContactTelNo " & _
"FROM tblMaster " & _
"WHERE IDNo LIKE @IDStr " & _
"ORDER BY LastName ASC", cnn)
With cmdStudList.Parameters
.AddWithValue("@IDStr", IDStr)
End With
Using StudListReader As SqlDataReader = cmdStudList.ExecuteReader()
Dim StudListTable As New DataTable
StudListTable.Clear()
StudListTable.Load(StudListReader)
StudListReader.Close()
Return StudListTable
End Using
End Using
End Function
Private Function GetYearLevel() As DataTable
Using cmdYearLevel As New SqlCommand("SELECT * FROM tblYearLevel " & _
"WHERE Dept LIKE @DeptStr ", cnn)
With cmdYearLevel.Parameters
.AddWithValue("@DeptStr", DeptStr)
End With
Using YearLevelReader As SqlDataReader = cmdYearLevel.ExecuteReader()
Dim YearLevelTable As New DataTable
YearLevelTable.Clear()
YearLevelTable.Load(YearLevelReader)
YearLevelReader.Close()
Return YearLevelTable
End Using
End Using
End Function
Private Function GetSubjects() As DataTable
Using cmdSubjects As New SqlCommand("SELECT SubjectID,SubjectName, " & _
"YearLevel,Dept " & _
"FROM tblSubjects WHERE YearLevel = @YearLevel " & _
"AND Dept = @Curr ", cnn)
With cmdSubjects.Parameters
.AddWithValue("@YearLevel", Me.cboYearLevel.Text)
.AddWithValue("@Curr", Curr)
End With
Using SubjectsReader As SqlDataReader = cmdSubjects.ExecuteReader()
Dim SubjectsTable As New DataTable
SubjectsTable.Clear()
SubjectsTable.Load(SubjectsReader)
SubjectsReader.Close()
Return SubjectsTable
End Using
End Using
End Function
Private Sub CheckRadioButtonValue()
If cnn.State = ConnectionState.Closed Then cnn.Open()
If rdoHS.Checked = True Then
IDStr = "HL%"
DeptStr = "HS%"
ElseIf rdoElem.Checked = True Then
IDStr = "EL%"
DeptStr = "EL%"
End If
End Sub
Private Sub rdo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoHS.CheckedChanged, rdoElem.CheckedChanged
If rdoElem.Checked = True Then
RdoHYes.Checked = False
RdoHNo.Checked = False
RdoHYes.Enabled = False
RdoHNo.Enabled = False
Else
RdoHNo.Checked = True
RdoHYes.Enabled = True
RdoHNo.Enabled = True
End If
Me.dtgsubjects.Columns.Clear()
DisplayRecordsToCombo()
End Sub
Private Sub DisplayRecordsToCombo()
CheckRadioButtonValue()
Me.StudListBindingSource.DataSource = Me.GetStudList()
With cboFullName
.DisplayMember = "StudentName"
.ValueMember = "IDNO"
.DataSource = Me.StudListBindingSource
End With
Me.YearLevelBindingSource.DataSource = Me.GetYearLevel()
With cboYearLevel
.DisplayMember = "Yearlevel"
.ValueMember = "YearLevelID"
.DataSource = Me.YearLevelBindingSource
End With
End Sub
Private Sub cboFullName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyData = Keys.Enter Then
''btnRegister.Focus()
End If
End Sub
Private Sub cboFullName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'UpperCase
e.KeyChar = Char.ToUpper(e.KeyChar)
End Sub
'TODO: This will display the the records in datagridview as well as bind the to txtSubjectID and txtSubjectName to
' SubjectID and SubjectName respectively
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
If cnn.State = ConnectionState.Closed Then cnn.Open()
Me.SubjListBindingSource.Position = 0
Me.SubjListBindingSource.DataSource = GetSubjects()
Me.dtgsubjects.DataSource = Me.SubjListBindingSource
txtSubjectID.DataBindings.Clear()
txtSubjectName.DataBindings.Clear()
txtSubjectID.DataBindings.Add("Text", Me.SubjListBindingSource, "SubjectID")
txtSubjectName.DataBindings.Add("Text", Me.SubjListBindingSource, "SubjectName")
'format Datagridview header display
With dtgsubjects
.Columns("SubjectID").HeaderText = "Subject ID"
.Columns("SubjectName").HeaderText = "Subject Name"
.Columns("Yearlevel").HeaderText = "Year Level"
dtgsubjects.Columns("Dept").HeaderText = "Dept"
End With
End Sub
Private Sub rdo_YesNoChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RdoHYes.CheckedChanged, RdoHNo.CheckedChanged
If RdoHYes.Checked = True Then
Curr = "HS SCICUR"
ElseIf RdoHNo.Checked = True Then
Curr = "HS REG"
Else
Curr = "ELEM"
End If
Me.dtgsubjects.Columns.Clear()
End Sub
Private Sub cboYearLevel_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboYearLevel.SelectedIndexChanged
Me.dtgsubjects.Columns.Clear()
End Sub
Private Sub cboFullName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFullName.SelectedIndexChanged
'Clear datagridview
Me.dtgsubjects.Columns.Clear()
'Clear and Display record details in textboxes
ClearTextBoxes()
txtStudID.DataBindings.Add("Text", Me.StudListBindingSource, "IDNO")
txtLastName.DataBindings.Add("Text", Me.StudListBindingSource, "LastName")
txtFirstName.DataBindings.Add("Text", Me.StudListBindingSource, "FirstName")
txtMI.DataBindings.Add("Text", Me.StudListBindingSource, "MI")
txtCompleteAddress.DataBindings.Add("Text", Me.StudListBindingSource, "CompleteAddress")
txtGender.DataBindings.Add("Text", Me.StudListBindingSource, "Gender")
txtCitizenship.DataBindings.Add("Text", Me.StudListBindingSource, "Citizenship")
txtContactPerson.DataBindings.Add("Text", Me.StudListBindingSource, "ContactPerson")
txtContactRelation.DataBindings.Add("Text", Me.StudListBindingSource, "ContactRelation")
txtContactTelNo.DataBindings.Add("Text", Me.StudListBindingSource, "ContactTelNo")
End Sub
Private Sub ClearTextBoxes()
'to clear multiple Databind textBoxes
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do Until ctrl Is Nothing
If TypeOf ctrl Is TextBox Then
ctrl.DataBindings.Clear()
End If
ctrl = Me.GetNextControl(ctrl, True)
Loop
End Sub
Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
If cnn.State = ConnectionState.Closed Then cnn.Open()
Try
Dim x As Integer
Dim y As Integer
'Check tblEnrol IDNO is already registered
If CheckID() = True Then
MessageBox.Show("Student is already registered.", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)
cboFullName.Focus()
Exit Sub
End If
'check if datagrid has values here
'If dtgsubjects.ColumnCount = 0 Then
'End If
'insert all selected subjects
y = Me.SubjListBindingSource.Count - 1
Me.SubjListBindingSource.Position = 0
For x = 0 To y
Using cmdRegister As New SqlCommand("INSERT INTO tblEnrol(IDNo,SubjectName, " & _
"FirstName,MI,LastName,YearLevel, Address,Gender) " & _
"VALUES (@StudID,@SubjName,@FName,@MI,@LName,@YearLevel,@Address,@Gender)", cnn)
With cmdRegister.Parameters
.AddWithValue("@StudID", txtStudID.Text.Trim)
.AddWithValue("@SubjName", txtSubjectName.Text.Trim)
.AddWithValue("@FName", txtFirstName.Text.Trim)
.AddWithValue("@MI", txtMI.Text.Trim)
.AddWithValue("@LName", txtLastName.Text.Trim)
.AddWithValue("@YearLevel", cboYearLevel.Text.Trim)
.AddWithValue("@Address", txtCompleteAddress.Text.Trim)
.AddWithValue("@Gender", txtGender.Text.Trim)
End With
cmdRegister.ExecuteNonQuery()
Me.SubjListBindingSource.Position += 1
End Using
Next
MessageBox.Show("Successfully Registered.", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)
cboFullName.Focus()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
cnn.Close()
End Try
End Sub
'TODO : return true if ID already exist
Private Function CheckID() As Boolean
'CheckID = False
Using cmdCheckID As New SqlCommand("SELECT COUNT(*)" & _
"FROM tblEnrol " & _
"WHERE IDNO =@StudID ", cnn)
With cmdCheckID.Parameters
.AddWithValue("@StudID", Me.txtStudID.Text)
End With
'ExecuteScalar executes a query that only return a single value
Dim Count As Integer = CInt(cmdCheckID.ExecuteScalar())
If Count = 0 Then
CheckID = False
Else
CheckID = True
End If
End Using
End Function
End Class