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

Recommended Answers

All 3 Replies

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?

do not hijack others old thread.

start a new thread of your own.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.