We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,610 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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

End Sub

4
Contributors
3
Replies
1 Year
Discussion Span
2 Years Ago
Last Updated
6
Views
bluerdanblue08
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

TomW
Posting Whiz
343 posts since Sep 2009
Reputation Points: 84
Solved Threads: 48
Skill Endorsements: 1

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


what should i do?

seventeen17
Newbie Poster
1 post since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

do not hijack others old thread.

start a new thread of your own.

debasisdas
Posting Genius
6,968 posts since Feb 2007
Reputation Points: 722
Solved Threads: 457
Skill Endorsements: 20

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.3171 seconds using 2.65MB