The rdReader is null because the ExecuteReader method has thrown an
exception and the rdReader is never assigned.
Just change the finally bit to:
finally
{
if( rdReader != null ) {
rdReader.Close();
}
}
so the Close is only called if the rdReader is valid.
regards
John Farrow
"Ahmet AKGUN" <ak*********@hotmail.com> wrote in message
news:OO****************@TK2MSFTNGP12.phx.gbl...
Hi
Compiler gives error to code shown below.
It says "refering to unassigned object "rdReader" in finally block".
Is the code below a bad usage of try-finally ? How can I write this code
better without error ?
if I declare rdReader in try block, then finally block cant see this
definition.
I must be sure that Reader cursor closed, so I put it in finally.
any help ?
OleDbDataReader rdReader;
OleDbCommand dbCmd;
try
{
rdReader= dbCmd.ExecuteReader();
}
catch(..)
{
}
finally
{
rdReader.Close();
}