How to Search a datagrid in SQL, VB.net
-
hi All the Following code reside on my Search Button.i want to search the results in datagrid(dataset),using the input from a textbox. and return the results if found. am usng SQl. here is the code i tried write , please help me fix this .
'Open Database Connection
Dim strconnection As String = "workstation id=5875-049;packet size=4096;user id=***; Password=******;data source=S*******;persist security info=False;initial catalog=Tshwane_Valuations"
Dim cn As SqlConnection = New SqlConnection(strconnection)
cn.Open()'set up data adapter Dim strSql As String = \_ "select Actual\_Extent,Attrib\_code,Lis\_key,Func\_key,Density," \_ & "New\_Net\_Usable\_Extent,New\_Total\_Land\_Factor,New\_AdJ\_Extent\_1,New\_Points\_1," \_ & "New\_ADJ\_Extent\_2, New\_Points\_2, LS\_Prediction, LS\_Confidence,LS\_TYPE from Property\_Mass " Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, cn) 'load Dataset Dim ds As DataSet = New DataSet da.Fill(ds, "Property\_Summary") 'Set up a new Data adapter Object strSql = "select Property\_Mass.Actual\_Extent,Property\_Mass.Attrib\_code,Property\_Mass.Lis\_key,Property\_Mass.Func\_key,Property\_Mass.Density," \_ & " Property\_Mass.New\_Net\_Usable\_Extent,Property\_Mass.New\_Total\_Land\_Factor,Property\_Mass.New\_AdJ\_Extent\_1, Property\_Mass.New\_Points\_1," \_ & " Property\_Mass.New\_ADJ\_Extent\_2, Property\_Mass.New\_Points\_2, Property\_Mass.LS\_Prediction, Property\_Mass.LS\_Confidence, Property\_Mass.LS\_TYPE," \_ & " Property\_Summary.Purchase\_Date, Property\_Summary.Purchase\_Price" \_ & " from Property\_Mass, Property\_Summary" \_ & "where Substring(Lis\_key,1,4)= ?" da.SelectCommand.Parameters("Lis\_key").Value = txtliskeysearch.Text da.Fill(ds) If ds.Tables("Property\_Mass").Rows.Count = 0 Then MessageBox.Show("Township not Found") End If End Sub
When i run this code it gives me an Error
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in system.data.dll
Additional information: An SqlParameter with ParameterName 'Lis_key' is not contained by this SqlParameterCollection.
And points to the Following line
da.SelectCommand.Parameters("Lis_key").Value = txtliskeysearch.Text
Help Thanks Thanks
Vuyiswa Maseko
-
hi All the Following code reside on my Search Button.i want to search the results in datagrid(dataset),using the input from a textbox. and return the results if found. am usng SQl. here is the code i tried write , please help me fix this .
'Open Database Connection
Dim strconnection As String = "workstation id=5875-049;packet size=4096;user id=***; Password=******;data source=S*******;persist security info=False;initial catalog=Tshwane_Valuations"
Dim cn As SqlConnection = New SqlConnection(strconnection)
cn.Open()'set up data adapter Dim strSql As String = \_ "select Actual\_Extent,Attrib\_code,Lis\_key,Func\_key,Density," \_ & "New\_Net\_Usable\_Extent,New\_Total\_Land\_Factor,New\_AdJ\_Extent\_1,New\_Points\_1," \_ & "New\_ADJ\_Extent\_2, New\_Points\_2, LS\_Prediction, LS\_Confidence,LS\_TYPE from Property\_Mass " Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, cn) 'load Dataset Dim ds As DataSet = New DataSet da.Fill(ds, "Property\_Summary") 'Set up a new Data adapter Object strSql = "select Property\_Mass.Actual\_Extent,Property\_Mass.Attrib\_code,Property\_Mass.Lis\_key,Property\_Mass.Func\_key,Property\_Mass.Density," \_ & " Property\_Mass.New\_Net\_Usable\_Extent,Property\_Mass.New\_Total\_Land\_Factor,Property\_Mass.New\_AdJ\_Extent\_1, Property\_Mass.New\_Points\_1," \_ & " Property\_Mass.New\_ADJ\_Extent\_2, Property\_Mass.New\_Points\_2, Property\_Mass.LS\_Prediction, Property\_Mass.LS\_Confidence, Property\_Mass.LS\_TYPE," \_ & " Property\_Summary.Purchase\_Date, Property\_Summary.Purchase\_Price" \_ & " from Property\_Mass, Property\_Summary" \_ & "where Substring(Lis\_key,1,4)= ?" da.SelectCommand.Parameters("Lis\_key").Value = txtliskeysearch.Text da.Fill(ds) If ds.Tables("Property\_Mass").Rows.Count = 0 Then MessageBox.Show("Township not Found") End If End Sub
When i run this code it gives me an Error
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in system.data.dll
Additional information: An SqlParameter with ParameterName 'Lis_key' is not contained by this SqlParameterCollection.
And points to the Following line
da.SelectCommand.Parameters("Lis_key").Value = txtliskeysearch.Text
Help Thanks Thanks
Vuyiswa Maseko
This might help .... http://www.vbforums.com/showthread.php?t=479407[^]