VBdotnet2005
asked on
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.IndexOutOfRangeExc eption' 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.SelectComm and.Parame ters("@nam eID").Valu e _
= Me.TextBox1.Text
DataSet11.Clear()
iRecords = SqlDataAdapter1.Fill(DataS et11)
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
.................
An unhandled exception of type 'System.IndexOutOfRangeExc
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.SelectComm
= Me.TextBox1.Text
DataSet11.Clear()
iRecords = SqlDataAdapter1.Fill(DataS
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SqlDataAdapter1.SelectComm
After that you can change it with
SqlDataAdapter1.SelectComm
= 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.