-2

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
Redd
  • 3
  • 5
  • This is an excellent opportunity to use the debugger. Set a breakpoint, enter "Admin" into the textbox, Stepo thru the code and Learn. – Ňɏssa Pøngjǣrdenlarp Nov 29 '15 at 13:43
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – David Wilson Nov 29 '15 at 23:24

2 Answers2

0

You declare a variable:

Dim Query As String

Then you try to use it in a comparison before any value has been assigned to it:

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

Clearly that variable, which hasn't been assigned, isn't going to equal that value. Maybe you just meant that to be an Else block?:

Else
  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())

Also, since this happens in a conditional block:

COMMAND = New MySqlCommand(Query, mydbcon)

then any time that condition isn't met (which, as shown above, it's never met) then no actual database command is ever executed. So there's no valid data reader to use.


Essentially, it looks like that If/Else structure is a bit messed up. It's not entirely clear how it should be structured in your logic, but you definitely don't want to compare variables which aren't assigned nor do you want to conditionally execute your query and then assume it always executed.

David
  • 176,566
  • 33
  • 178
  • 245
  • So what should i do to remove the error at the reader line? Should i remove it? – Redd Nov 29 '15 at 11:44
  • @Redd: You should fix that If/Else logic as recommended. The error is happening because your code assumes that the data reader is always available, but currently it's *never* available because you only execute the query in an ElseIf block that's never true. – David Nov 29 '15 at 12:00
  • Can you give me some suggestions? Here's what im trying to do. The admin should login the system leaving the course combobox blank. But for the other users it's a must. – Redd Nov 29 '15 at 12:55
  • @Redd: Suggestions for what? I certainly can't write your application for you. But at the very least this answer has identified the causes of the errors you're seeing. That last `ElseIf` block makes no sense and should probably just be an `Else` block. And you need to make sure that you execute your query before trying to read the results of that query. – David Nov 29 '15 at 17:33
0
  1. Make sure your database query works.
  2. When "admin" but "college" is ="" then

MessageBox.Show("Please Select a college")  
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())  
ElseIf

So when "college = "" " then messagebox appear and when press OK make sure that query run well, put "ElseIf" below your "college" query and add another if

  • @Redd you need to learn how IF, ElseIf, structural, you can try with simple ones, if you mastered it, you can do any IF like your code above, with any query or something else. – Bondan's Blackk Diamond Nov 29 '15 at 17:39