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

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

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.

Edited 2 Years Ago by deceptikon

This article has been dead for over six months. Start a new discussion instead.