[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


parameterized query with SQL

Posted on 2006-03-30
Medium Priority
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

            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
Question by:VBdotnet2005
  • 2
LVL 11

Expert Comment

ID: 16340574
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.
LVL 11

Accepted Solution

LordWabbit earned 1000 total points
ID: 16340586
oh yeah forgot to say that if you want only one record in the table you can add TOP 1 to your select statement.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month19 days, 19 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question