Beginner : closing DataReader

N

NotYetaNurd

try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of scope
in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens
.......
 
S

Saurabh

Can you not do something like :

SqlDataReader dread = null;
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
if (dread != null)
{ //or some sensible checking, i can't say what it should be... ;)
dread.Close();
}
sqlcom.Connection.Close();
}
 
M

Miha Markic

Hi Not

SqlDataReader dread=sqlcom.ExecuteReader();
try
{
}
finally
{
dread.Close();
sqlcom.Connection.Close();
}

Your code should looks like the code above.
 
S

Shiva

Since dread variable is local only to the try block it will not be available
in the finally block. Only option is to move the data reader declaration
outside try block or have the Close() method inside try.

try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
dread.Close();
sqlcom.Connection.Close();
}

here is my problem..
the above code does not compile becoz dread (SqlDataReader) is out of scope
in finally block
i cant declare dread (SqlDataReader) outside try block .....
how do i close data reader dread (SqlDataReader) if any exception happens
.......
 
N

NotYetaNurd

Thanks saurabh ..

Saurabh said:
Can you not do something like :

SqlDataReader dread = null;
try
{
SqlDataReader dread=sqlcom.ExecuteReader();
}

finally
{
if (dread != null)
{ //or some sensible checking, i can't say what it should be... ;)
dread.Close();
}
sqlcom.Connection.Close();
}
 
S

Saurabh

Actually it should be as below.... in the previous post i had the
declaration outside as well as inside the try block. My mistake in copy +
paste ;)

SqlDataReader dread = null;
try
{
dread=sqlcom.ExecuteReader();
}
 
S

Sami Vaaraniemi

Or even better:

try
{
using (SqlDataReader dread = sqlcom.ExecuteReader())
{
// use reader here - no need to call Close in the finally block
}
}
finally
{
sqlcom.Connection.Close();
}

Further, if you put the SqlConnection into a using block too you can get rid
of the try/finally completely.

Regards,
Sami
 
D

Daniel Pratt

Or you could even do:

using (SqlDataReader dread =
sqlcom.ExecuteReader(CommandBehavior.CloseConnection))
{
// use reader here - no need to call Close in the finally block
...
}

Regards,
Dan
 
N

NotYetaNurd

hi miha but i wanted to capture any exception that happens in
SqlDataReader dread=sqlcom.ExecuteReader();
so i have to put it inside try block....
 
N

NotYetaNurd

okey :)


Saurabh said:
Actually it should be as below.... in the previous post i had the
declaration outside as well as inside the try block. My mistake in copy +
paste ;)

SqlDataReader dread = null;
try
{
dread=sqlcom.ExecuteReader();
}
 
M

Miha Markic

Hi,

Yes, you might put it inside another try/catch block or do like this:
SqlDataReader dread;
try
{
dread = =sqlcom.ExecuteReader();
}
finally
{
if (dread != null)
dread.Close();
sqlcom.Connection.Close();
}
 

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