I'm trying to populate my textboxs with data from my database when the user name is selected but I'm having trouble getting the code right. Help Please!
table
users (there is a textbox for each of these fields)
password
lastname
firstname

Using upcmd As New SqlCommand("SELECT * FROM users WHERE userid = @userid", connection)
upcmd.Parameters.Add(New SqlParameter("@userid", Me.txtuseridprofile.Text))
upcmd.Connection.Open()

Try
upcmd.ExecuteNonQuery()
Catch ex As SqlException
If ex.Number <> 0 Then
ErrorProvider1.SetError(Me.txtuseridprofile, "Login Id: " &
Me.txtuseridprofile.Text & " :Not Found!")
upcmd.Connection.Close()
Exit Sub
End If
End Try
upcmd.Connection.Close()
MessageBox.Show("Login ID Found. Update data and Click Go!")

Thanks in advance!

i think ExecuteNonQuery is not it, because it returns no data rows but instead, ..the number of rows affected !!

here's a code suggestion

Using upcmd As New SqlCommand("SELECT * FROM users WHERE userid = @userid", connection)
            upcmd.Parameters.Add(New SqlParameter("@userid", Me.txtuseridprofile.Text))
            upcmd.Connection.Open()
            Try
                Dim dr As SqlDataReader = upcmd.ExecuteReader()
                If dr.Read Then
                    password.Text = dr.Item(1)
                    lastname.Text = dr.Item(2)
                    firstname.Text = dr.Item(3)
                Else
                    MessageBox.Show("Login ID NOT Found !")
                End If
            Catch ex As SqlException
                If ex.Number <> 0 Then
                    ErrorProvider1.SetError(Me.txtuseridprofile, "Login Id: " &
                    Me.txtuseridprofile.Text & " :Not Found!")
                    upcmd.Connection.Close()
                    Exit Sub
                End If
            End Try
            upcmd.Connection.Close()
            MessageBox.Show("Login ID Found. Update data and Click Go!")
        End Using

Edited 6 Years Ago by ÜnLoCo: n/a

This question has already been answered. Start a new discussion instead.