Closing DataReader inside of Try{}Catch{}

  • Thread starter Thread starter Jeff User
  • Start date Start date
J

Jeff User

Hi all
I am using an OleDbDataReader.
I need to establish and then keep the connection that I use, but I do
not need to keep the data reader, after this operation is over.

Therefore, regardless of failure or not, I need to close the data
reader. So, in trying to practice good habits (and if I dont it causes
an error the next time around) I close the reader before exiting this
procedure.
This is fine until I need to handle errors. So, I implement
try{}catch{} to handle if an error occurs. If an error, I also need to
close reader before exiting.

Although I am setting the data reader in a Try statement, I can not
close it in the Catch statement because of compile error:
use of unassigned local variable 'LocalReader'

How can I handle this?
Here is the basic idea:

public string[] select(string SqlStatement)
{
OleDbDataReader LocalReader;
try
{
LocalReader = executeReader(SqlStatement);
while(LocalReader.Read())
{
... do some data stuff here...
}
}
catch(Exception er)
{
LocalReader.Close(); // unassigned local variable here
.... throw error, or whatever...
}
LocalReader.Close(); //this close is OK
return sResultArray; // or whatever...
}

private System.Data.OleDb.OleDbDataReader executeReader (string
sqlStmnt)
{
// Uses server connection that is extablished elsewhere
// prior to executing operations on db
System.Data.OleDb.OleDbCommand cmd = new
OleDbCommand (sqlStmnt, this.get_servconn());

System.Data.OleDb.OleDbDataReader rdr = cmd.ExecuteReader();
return rdr;
}

This code works without the try-Catch statements but when there are
errors such as table not found, etc.., I need to close the reader
before trying again.

I have read several basic data access articles but none show how to
handle closing in case of error.

Thanks
Jeff
 
Jeff,

Just assign the LocalReader variable to null in the declaration, and the
code should compile fine.

Hope this helps.
 
you should put it in a "finally" block after the catch.
Or use it in a Using block, which is what I usually prefer, since it is a
little nicer to read.
 
Jeff User said:
Hi all
I am using an OleDbDataReader.
I need to establish and then keep the connection that I use, but I do
not need to keep the data reader, after this operation is over.

Therefore, regardless of failure or not, I need to close the data
reader. So, in trying to practice good habits (and if I dont it causes
an error the next time around) I close the reader before exiting this
procedure.
This is fine until I need to handle errors. So, I implement
try{}catch{} to handle if an error occurs. If an error, I also need to
close reader before exiting.

Although I am setting the data reader in a Try statement, I can not
close it in the Catch statement because of compile error:
use of unassigned local variable 'LocalReader'

How can I handle this?
Here is the basic idea:

public string[] select(string SqlStatement)
{
OleDbDataReader LocalReader;
try
{
LocalReader = executeReader(SqlStatement);
while(LocalReader.Read())
{
... do some data stuff here...
}
}
catch(Exception er)
{
LocalReader.Close(); // unassigned local variable here
.... throw error, or whatever...
}
LocalReader.Close(); //this close is OK
return sResultArray; // or whatever...
}
Try setting LocalReader to null at initalization time and then use an if
statement in your catch:

OleDbDataReader LocalReader = null;
try
{

}
catch
{
if (LocalReader != null)
LocalReader.Close();
}
 
Back
Top