I am getting a null reference error and a warning telling me that a variable 'Query' has been used before it was assigned. The code works but after the messagebox 'Please select college' and clicked ok the error occur. What im trying to do is when the user admin will log in to the system he is exempted from choosing a college but when other user are going to login they need to choose from which college are they from.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, ComboBox1.DropDownStyleChanged
mydbcon = New MySqlConnection
mydbcon.ConnectionString = "server=localhost;userid=jared;password=jared;database=database"
Dim reader As MySqlDataReader
Try
mydbcon.Open()
Dim Query As String
If TextBox1UN.Text = "admin" Then
Query = String.Format("SELECT * FROM logininfo WHERE Username = '{0}' AND Password = '{1}'", Me.TextBox1UN.Text.Trim(), Me.TextBox2Pass.Text.Trim())
ElseIf (String.IsNullOrEmpty(ComboBox1.SelectedItem)) Then
MessageBox.Show("Please Select a college")
ElseIf Query = String.Format("SELECT * FROM logininfo WHERE Username = '{0}' AND Password = '{1}' and College = '{2}'", Me.TextBox1UN.Text.Trim(), Me.TextBox2Pass.Text.Trim, Me.ComboBox1.SelectedItem.Trim()) Then
COMMAND = New MySqlCommand(Query, mydbcon)
End If
reader = COMMAND.ExecuteReader
Dim count As Integer = 0
While reader.Read < --- Null reference occur
count = count + 1
End While
If TextBox1UN.Text = "admin" And count = 1 Then
Form2.Show()
TextBox1UN.Clear()
TextBox2Pass.Clear()
ComboBox1.ResetText()
Me.Hide()
ElseIf count = 1 Then
Form2.Show()
Form2.Button4.Hide()
TextBox1UN.Clear()
TextBox2Pass.Clear()
ComboBox1.ResetText()
Me.Hide()
Else
MessageBox.Show("Incorrect! Please Try Again.")
End If
mydbcon.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
mydbcon.Dispose()
End Try
End Sub
End Class