Invalid attempt to Read when reader is closed - Help needed

50 views
Skip to first unread message

Karthikeyan R

unread,
Jan 2, 2010, 7:31:02 AM1/2/10
to dotnetde...@googlegroups.com
Hi,
 
I am getting "Invalid attempt to Read when reader is closed" error in one of my pages.I have used the following method in my class file,

public

class ExecuteQuery

{

OleDbConnection conn;

OleDbCommand cmd;

DataTable dt;

OleDbDataAdapter da;

OleDbDataReader dr;

 

public ExecuteQuery()

{

conn =

new OleDbConnection();

conn.ConnectionString =

DBConnection.GetConnectionString();

}

public

OleDbDataReader ExecuteReader(string ReaderQuery)

{

cmd =

new OleDbCommand(ReaderQuery, conn);

conn.Open();

dr = cmd.ExecuteReader(

CommandBehavior.CloseConnection);

conn.Close();

return dr;

}

}
 
In the aspx.cs file I have used the following code,
 

private void ddlfill()

{

try

{

ddlquestion.Items.Add(

"");

ddlquestion.SelectedIndex = 0;

string query = "select Hint_Question from Tbl_Hintquestion order by Hint_No ASC";

bool abc = EQuery.ExecuteReader(query).Read();

while (abc == true)

{

ddlquestion.Items.Add(dr[

"Hint_Question"].ToString());

abc = EQuery.ExecuteReader(query).Read();

}

}

catch (Exception EXE)

{

Alert.Show(EXE.Message);

}

}

Can some one help me out to solve this problem?
--
Thanks & Regards,
Karthikeyan

Benj Nunez

unread,
Jan 3, 2010, 7:41:50 PM1/3/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I noticed in your code you have these:

*conn.Close();*

*return** dr;*


You've closed the connection then returned the "dr" (your datareader
object)
to the caller. Closing the connection killed the datareader as well.


Using a DataSet would help you close the connection object but allows
you to keep
the records that you queried to the DataSet. So instead of using
OleDbDataReader,
why not use DataSet? I suggest you re-write the ExecuteReader()
routine to use it.


Cheers!

Benj

On Jan 2, 8:31 pm, Karthikeyan R <karthi...@gmail.com> wrote:
> Hi,
>
> I am getting *"Invalid attempt to Read when reader is closed"* error in one


> of my pages.I have used the following method in my class file,
>
> public class ExecuteQuery
>
> {
>
> OleDbConnection conn;
>
> OleDbCommand cmd;
>
> DataTable dt;
>
> OleDbDataAdapter da;
>
> OleDbDataReader dr;
>
> public ExecuteQuery()
>
> {
>
> conn = new OleDbConnection();
>
> conn.ConnectionString = DBConnection.GetConnectionString();
>
> }
>

> *public** OleDbDataReader ExecuteReader(string** ReaderQuery)*
>
> *{ *
>
> *cmd = **new OleDbCommand**(ReaderQuery, conn); *
>
> *conn.Open();*
>
> *dr = cmd.ExecuteReader(**CommandBehavior**.CloseConnection); *
>
> *conn.Close();*
>
> *return** dr;*
>
> *}*

sushma sushma

unread,
Jan 3, 2010, 7:45:45 PM1/3/10
to dotnetde...@googlegroups.com
Check the red marked code karthikeya.

OleDbDataReader
 ExecuteReader(string
 ReaderQuery)

{

cmd =

new OleDbCommand(ReaderQuery, conn);

conn.Open();

dr = cmd.ExecuteReader(

CommandBehavior.CloseConnection);

// Try this one Karthik

return dr;
then close connection;

conn.Close();


return dr;
--
Thanks,

Sushma

Email: sush...@gmail.com

Benj Nunez

unread,
Jan 3, 2010, 7:59:02 PM1/3/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
"*// Try this one Karthik*
*
*
*return dr;*
*then close connection;
*
** "


Hi.

I think if you do this, you'll get a compiler error/
warning:"Unreachable code detected.".
As far as I can tell, any statement below the return statement
produces this
error/warning except when you program in Borland Delphi. ;)

Cheers!

Benj

On Jan 4, 8:45 am, sushma sushma <sushma...@gmail.com> wrote:
> *


> Check the red marked code karthikeya.
>

> OleDbDataReader ExecuteReader(string** ReaderQuery)*
>
> *{*
>
> *cmd =*
> *new OleDbCommand**(ReaderQuery, conn);*
>
> *conn.Open();*
>
> *dr = cmd.ExecuteReader(*
> *CommandBehavior**.CloseConnection);*
> *

> *
> *// Try this one Karthik*
> *
> *
> *return dr;*
> *then close connection;
> *
> **


>
> *conn.Close();*
>
> *return** dr;*
>
>
>

> On Sat, Jan 2, 2010 at 7:31 AM, Karthikeyan R <karthi...@gmail.com> wrote:
> > Hi,
>

> > I am getting *"Invalid attempt to Read when reader is closed"* error in


> > one of my pages.I have used the following method in my class file,
>
> > public
> > class ExecuteQuery
>
> > {
>
> > OleDbConnection conn;
>
> > OleDbCommand cmd;
>
> > DataTable dt;
>
> > OleDbDataAdapter da;
>
> > OleDbDataReader dr;
>
> > public ExecuteQuery()
>
> > {
>
> > conn =
> > new OleDbConnection();
>
> > conn.ConnectionString =
> > DBConnection.GetConnectionString();
>
> > }
>

> > *public*


> > * OleDbDataReader ExecuteReader(string** ReaderQuery)*
>
> > *{ *
>
> > *cmd = *
> > *new OleDbCommand**(ReaderQuery, conn); *
>
> > *conn.Open();*
>
> > *dr = cmd.ExecuteReader(*
> > *CommandBehavior**.CloseConnection); *
>
> > *conn.Close();*
>
> > *return** dr;*
>
> > *}*
> > }
>

> > In the aspx.cs file I have used the following code,
>
> > private void ddlfill()
>
> > {
>
> > try
>
> > {
>
> > ddlquestion.Items.Add(
> > "");
>
> > ddlquestion.SelectedIndex = 0;
>
> > string query = "select Hint_Question from Tbl_Hintquestion order by
> > Hint_No ASC";
>
> > bool abc = EQuery.ExecuteReader(query).Read();
>
> > while (abc == true)
>
> > {
>
> > ddlquestion.Items.Add(dr[
> > "Hint_Question"].ToString());
>
> > abc = EQuery.ExecuteReader(query).Read();
>
> > }
>
> > }
>
> > catch (Exception EXE)
>
> > {
>
> > Alert.Show(EXE.Message);
>
> > }
>
> > }
> > Can some one help me out to solve this problem?
> > --
> > Thanks & Regards,
> > Karthikeyan
>
> --
> Thanks,
>
> Sushma
>

> Email: sushma...@gmail.com

Karthikeyan R

unread,
Jan 4, 2010, 7:47:47 AM1/4/10
to dotnetde...@googlegroups.com
Thanks for your replies. 
 
Sushma,
 
As Benj suggested it produced "Unreachable code detected." since return should be last statement in a method (as per my understanding). 
 
I have changed the logic and used a datatable to fill the dropdownlist as I found that would be an efficient way rather than using datareader.
 
I have changed the code in the Execute reader method like this,

public OleDbDataReader ExecuteReader1(string ReaderQuery)

{

OleDbDataReader reader = null;

try

{

conn =

new OleDbConnection();

conn.ConnectionString =

DBConnection
.GetConnectionString();

cmd =

new OleDbCommand(ReaderQuery, conn);

this.conn.Open();

reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

}

catch (Exception ex)

{

if (handleErrors)

strLastError = ex.Message;

else

throw;

}

return reader;

}

It works good in few scenarios, whereas if I call a method to EXECUTESCALAR after the EXECUTEREADER method it gives the folllowing error, "The connection was not closed. The connection's current state is open."

Karthikeyan R

unread,
Jan 4, 2010, 12:21:14 PM1/4/10
to dotnetde...@googlegroups.com
Just fixed the problem by closing the reader in the aspx.cs page.

 private void gethintno()
    {
        try
        {            
            string squery = "select Hint_No from Tbl_Hintquestion where Hint_Question = \'" + (Fixquotes(lblhintquestion.Text.Trim())) + "\'";
            IDataReader dr;
            dr = EQuery.ExecuteReader(squery);                 
            if (dr.Read() == true)
            {
                ihintno = int.Parse(dr["Hint_No"].ToString());
            }
            dr.Close();   
        }
        catch (Exception EXE)
        {
            Alert.Show(EXE.Message);

vinay kumar

unread,
Jan 5, 2010, 2:04:52 AM1/5/10
to dotnetde...@googlegroups.com
add finally block and then close the reader

Karthikeyan R

unread,
Jan 7, 2010, 4:57:44 AM1/7/10
to dotnetde...@googlegroups.com

Oh ok.  I will make that change Vinay.  Thanks for your comments.

Greg Hile

unread,
Jan 8, 2010, 7:58:29 AM1/8/10
to dotnetde...@googlegroups.com

Hi all!

 

When programming I have the program performing some tasks that take a while.

 

How can I make my program more responsive?

 

Would learning threading help?

 

 

 

Greg

Benj Nunez

unread,
Jan 9, 2010, 8:55:49 AM1/9/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Yes. The easiest is to use the BackgroundWorker component. Give it a
try.


Cheers!

Benj

Reply all
Reply to author
Forward
0 new messages