We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

parameterized query with SQL

VBdotnet2005
VBdotnet2005 asked
on
Medium Priority
184 Views
Last Modified: 2010-04-23
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 system.data.dll

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

            DataSet11.Clear()
            iRecords = SqlDataAdapter1.Fill(DataSet11)

            If  iRecords = 0 Then
                Me.SetEntryControls(False)
                MessageBox.Show("Error", "Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                Me.SetEntryControls(True)
                Me.TextBox1.Focus()



            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
Comment
Watch Question

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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.