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..

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 & "'

Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet

myDA.Fill(myDataSet, "MyTable")
DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView

End Sub

7 Years
Discussion Span
Last Post by debasisdas

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.


i am having the same error :(
Fill: SelectCommand.Connection property has not been initialized.

what should i do?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.