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
"Saurabh" <sa*****@nagpurcity.net> wrote in message
news:eh**************@tk2msftngp13.phx.gbl...
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();
}
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:Of****************@TK2MSFTNGP11.phx.gbl... 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 ......