parameterized query with SQL

What I want is to retrieve only record at a time.  To retrieve a data., enter a nameID and click on the button1. Then if a row is found with that nameID, the row is retreived and diplay data on the form(txtCity, txtState and txtZip)

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in

Additional information: An SqlParameter with ParameterName '@nameID' is not contained by this SqlParameterCollection.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim iRecords As Integer

        If Me.TextBox1.Text <> "" Then

            SqlDataAdapter1.SelectCommand.Parameters("@nameID").Value _
            = Me.TextBox1.Text

            iRecords = SqlDataAdapter1.Fill(DataSet11)

            If  iRecords = 0 Then
                MessageBox.Show("Error", "Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If
    End Sub

    Private Sub SetEntryControls(ByVal bEditmode As Boolean)

        Me.txtCity.Enabled = bEditmode
        Me.txtState.Enabled = bEditmode
        Me.txtZip.Enabled = bEditmode
    End Sub
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You need to add the @nameID parameter, you are trying to reference it in your parameters collection before it's been added.  
SqlDataAdapter1.SelectCommand.Parameters.Add("@nameID", Me.TextBox1.Text)

After that you can change it with

SqlDataAdapter1.SelectCommand.Parameters("@nameID").Value _
            = Me.TextBox1.Text

if need be and continue using the same data adapter.  Just remember that if you try to add @nameID again it will fail because it's already in the collection, so either modify it or alternatively you can clear the parameters collection and add again.
oh yeah forgot to say that if you want only one record in the table you can add TOP 1 to your select statement.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.