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!

Recommended Answers

All 3 Replies

What kind of error do you get? What happens when you run the code?

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

Thank you very much!

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.