Hi There. I have the following code for a login system withing VB 2010. I am getting a Warning: "Variable 'reader' is used before it has been assigned a value. A null reference exception could result at runtime." Where did I go wrong?

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Dim cmd As New MySqlCommand

        Dim User As String = txtUsername.Text
        Dim Pass As String = txtPassword.Text
        Dim reader As DataTableReader

        connection.Open()
        cmd.CommandText = "SELECT * FROM `users` WHERE Username= '" & txtUsername.Text & "' and Password= '" & txtPassword.Text & "', conn|"
        cmd.Connection = connection

        cmd.Parameters.Add(New MySqlParameter("?UserName", txtUsername.Text))
        cmd.Parameters.Add(New MySqlParameter("?Password", txtPassword.Text))

        cmd.Connection = connection
        'cmd.EndExecuteReader = reader

        If reader.HasRows() Then
            MessageBox.Show("Login Success!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Form2.Show()
            Me.Close()
        Else
            MsgBox("Invalid Login Information!", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        connection.Close()
    End Sub
End Class

Recommended Answers

All 3 Replies

reader is an uninitialized local variable. You need to create an object of DataTableReader and assign the reference to your variable.

Okay, so how would I go about doing that?

I'm not hip on the MySQL classes, but you probably want a MySqlDataReader instead, and that object can be initialized with cmd.ExecuteReader(), provided it follows the same design as the standard .NET classes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.