sql reader error

J

juli jul

Hello,
I am using a reader in order to read query results:

SqlDataReader rdr = null;
SqlDataReader rdr_rows=null;

this.conn.Open();

SqlCommand cmd = new SqlCommand("select name from sysobjects where name
like '%prog%'", this.conn);

rdr = cmd.ExecuteReader();

while (rdr.Read())
{
//some code here
}
if (rdr != null)
{
rdr.Close();
}

foreach (Dbdb data in db_coll)
{
SqlCommand cmd_rows = new SqlCommand("SELECT rows FROM sysindexes WHERE
id = OBJECT_ID('"+db_data.Name+"') AND indid < 2", this.conn);
rdr_rows = cmd_rows.ExecuteReader();
rdr_rows.Read();
System.Console.WriteLine(rdr_rows[0]);
}

I get this error:
There is already an open DataReader associated with this connection
which must be closed first.

Why? I am closing it,how to solve this?
Thanks a lot!
 
L

Landi

SqlDataReader rdr = null;
SqlDataReader rdr_rows=null;
Do you get the error here? -> this.conn.Open();
Even so, you tried to open a connection that is already open. If it's open
then just use it; don't try to reopen it.

--
(e-mail address removed)
http://dowhileloop.com website development
http://publicjoe.dowhileloop.com -- C# Tutorials

juli jul said:
Hello,
I am using a reader in order to read query results:
SqlCommand cmd = new SqlCommand("select name from sysobjects where name
like '%prog%'", this.conn);

rdr = cmd.ExecuteReader();

while (rdr.Read())
{
//some code here
}
if (rdr != null)
{
rdr.Close();
}

foreach (Dbdb data in db_coll)
{
SqlCommand cmd_rows = new SqlCommand("SELECT rows FROM sysindexes WHERE
id = OBJECT_ID('"+db_data.Name+"') AND indid < 2", this.conn);
rdr_rows = cmd_rows.ExecuteReader();
rdr_rows.Read();
System.Console.WriteLine(rdr_rows[0]);
}

I get this error:
There is already an open DataReader associated with this connection
which must be closed first.

Why? I am closing it,how to solve this?
Thanks a lot!
 
O

Otis Mukinfus

Hello,
I am using a reader in order to read query results:

SqlDataReader rdr = null;
SqlDataReader rdr_rows=null;

this.conn.Open();

SqlCommand cmd = new SqlCommand("select name from sysobjects where name
like '%prog%'", this.conn);

rdr = cmd.ExecuteReader();

while (rdr.Read())
{
//some code here
}
if (rdr != null)
{
rdr.Close();
}

foreach (Dbdb data in db_coll)
{
SqlCommand cmd_rows = new SqlCommand("SELECT rows FROM sysindexes WHERE
id = OBJECT_ID('"+db_data.Name+"') AND indid < 2", this.conn);
rdr_rows = cmd_rows.ExecuteReader();
rdr_rows.Read();
System.Console.WriteLine(rdr_rows[0]);
}

I get this error:
There is already an open DataReader associated with this connection
which must be closed first.

Why? I am closing it,how to solve this?
Thanks a lot!

I think it would be better to use this syntax to close the DataReader:

If(rdr != null)
{
if(! rdr.IsClosed)
{
rdr.Close();
}
}

I don't think a closed DataReader is null, unless there was a failure creating
it. If that happened it wouldn't be open anyway.

Otis Mukinfus
http://www.otismukinfus.com
 
J

juli jul

Thanks but it didn't help. Are there some other suggestions why I get
the error?
Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top