Filter record using vb net 2003 through textbox on Datagrid
Hi to everyone!
I am new in programming and I'm making an application that can filter records on datagrid from the user's input in textbox..I tried this code that I also got here.. But I'm getting an error..please help me..thanks in advance..
ERROR MESSAGE:
An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll
Additional information: Fill: SelectCommand.Connection property has not been initialized.
_____________________________________________________
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim keywords As String = TextBox1.Text
Dim con As OleDbConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\addressbook1.mdb")
' Use wildcard
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM tblcontacts WHERE firstname Like '% " & keywords & " %', con ")
' or Where Filed1='" & keywords & "'
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet
Ok first, you dont need to explictily call open and close to your database connection, your dataadapter will open & close the connection as needed.
Your error is being caused by your concatenated query string. Your connection object is actually within your query double quotes becoming part of the select statement rather then being passed as the second parameter for the connection.