Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello All,
I'm writing a program about leitnerbox. In my program I want to search words in my table in data base. I have a text box and my codes are in text_changed event. Here is my code amd then the error.

VB
Private Sub TextBoxX3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxX3.TextChanged
       Dim objcmd As SqlCommand
       Dim sql As String
       If CheckBoxX2.Checked = True Then
           sql = "select * from leitnertable where word like '%" & TextBoxX3.Text & "%'"
       End If
       If CheckBoxX1.Checked = True Then
           sql = "select * from leitnertable where firstmeaning or secondmeaning like '%" & TextBoxX3.Text & "%'"
       End If
       objcmd = New SqlCommand(sql, con)
       If objcmd.Connection.State = ConnectionState.Closed Then objcmd.Connection.Open()
       Dim dr As SqlDataReader
       dr = objcmd.ExecuteReader
       While dr.Read
           Dim dt As New DataTable
           dt.Load(dr)
           DataGridView2.DataSource = dt
       End While
   End Sub


Check box1 is searching in meaninngs and check box2 is searching in words.
When I debug the program yhe error is:

Invalid attempt to call Read when reader is closed.


I don't know what to do.

Please help me.

Thanks in Advance.
Posted
Updated 9-Jun-11 21:04pm
v4
Comments
Firo Atrum Ventus 10-Jun-11 2:46am    
are you sure your SqlConnection is open?
nafiseh.s 10-Jun-11 3:09am    
yes.i define the conection and connection string in page-looad.but when i debug the program and write s.th in text box this error occure that the reader is not opened.i think i should bind the grid view.but it has not Datagridview.databind()!
should i import s.th special to program?

You instantiate your SqlCommand with objcmd = New SqlCommand(sql,con) but nowhere do you declare con as a SqlConnection. Is it a Global or Form Level variable, and is it declared properly, with the correct ConnectionString ?
 
Share this answer
 
Comments
nafiseh.s 10-Jun-11 3:17am    
yes.i define the conection and connection string in page-looad.but when i debug the program and write s.th in text box this error occure that the reader is not opened.
You shouldn't call the DataReader.Read method: after Command.ExecuteReader you have just to call once DataTable.Load and then close the DataReader.
 
Share this answer
 
Comments
nafiseh.s 10-Jun-11 3:16am    
you mean i should write this:
Dim dr As SqlDataReader
dr = objcmd.ExecuteReader
Dim dt As New DataTable
dt.Load(dr)
DataGridView2.DataSource = dt
CPallini 10-Jun-11 3:24am    
Yes.
nafiseh.s 10-Jun-11 3:18am    
dont you think that my problem is in binding gridview?i dont bind gridview.because it does not have databind() method.why???
nafiseh.s 10-Jun-11 3:21am    
thanks a lot.your solution is working.thank a lot
CPallini 10-Jun-11 3:42am    
You are welcome.
Check your SQLCONNECTION. There is the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900