1

here's my code:

'Validation for admin:
    cmd = New OleDbCommand("SELECT * FROM tblSettings WHERE Admin_username=@Username AND Admin_password=@password;", conn)
    Try
        conn.Open()
        'Add parameters:
        cmd.Parameters.AddWithValue("@username", txtUsername.Text)
        cmd.Parameters.AddWithValue("@password", txtPassword.Text)
        'Reader in action:
        Using rd As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            If rd.Read Then
                If StrComp(txtPassword.Text, rd.GetValue(rd.GetOrdinal("Admin_password")), 0) = 0 Then
                    cmd.ExecuteNonQuery()
                    Me.Hide()
                    frmLoginType.Hide()
                    frmMain.MenuStrip1.Enabled = True
                Else
                    ErrorProvider1.SetError(txtPassword, "Invalid password!")
                End If
            Else
                ErrorProvider1.SetError(txtUsername, "Invalid username!")
                ErrorProvider1.SetError(txtPassword, "Invalid password!")
            End If
            rd.Close()
        End Using
        'Close connection:
        cmd.Dispose()
        conn.Close()
    Catch ex As Exception
        MsgBox(ex.Message, vbCritical, "Error")
    End Try

I have tried but i keep getting error that says 'There is a reader associated with this command that must be closed first'. What am I not doing right? Thanks

Steven
  • 51
  • 1
  • 7
  • `cmd.ExecuteNonQuery()` after `cmd.ExecuteReader()`. This code seem *patched up* (different sources?). `rd.Close()` in a `Using` block? `conn.Close()` but not instantiated here: is it a *shared* connection? If it is, get rid of it and open/dispose in place. `rd.GetValue(rd.GetOrdinal("...")`? The Reader offers a simplified (commonly used) syntax for this. – Jimi Feb 16 '19 at 09:02
  • I realised the cmd.ExecuteNonQuery() was bringing trouble so I removed it. Thanks. Its working now. – Steven Feb 16 '19 at 09:14

0 Answers0