Beginner : closing DataReader

  • Thread starter Thread starter NotYetaNurd
  • Start date Start date
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
.......
 
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();
}
 
Hi Not

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

Your code should looks like the code above.
 
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
.......
 
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();
}
 
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();
}
 
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
 
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
 
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....
 
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();
}
 
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();
}
 
Back
Top