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 4 7629
Jeff,
Just assign the LocalReader variable to null in the declaration, and the
code should compile fine.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jeff User" <je*******@hotmail.com> wrote in message
news:08********************************@4ax.com... 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
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" <je*******@hotmail.com> wrote in message
news:08********************************@4ax.com... 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 User" <je*******@hotmail.com> wrote in message
news:08********************************@4ax.com... 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();
}
Thanks guys
Setting it to null when declaring it solved the problem!
Jeff
On Mon, 14 Nov 2005 20:53:43 GMT, Jeff User <je*******@hotmail.com>
wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: kaeli |
last post by:
Hey all,
I'd like to start using the try/catch construct in some scripts. Older
browsers don't support this. What's the best way to test for support for this
construct so it doesn't kill...
|
by: Craig |
last post by:
Can you put a Response.Redirect inside a Try Catch block?
I think you will get a thread Abored Error, true?
|
by: VB Programmer |
last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally.
Example: In order to close/dispose a db connection you have to dim the
connection outside of the Try Catch Finally...
|
by: bienwell |
last post by:
Hi all,
I'd like to use TRY, CATCH to open my database connection. I have 2 servers
that have the same table name "myTable". If the first connection to Server1
is failed, then the program will...
|
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= |
last post by:
Is it right when placing the RETURN statement inside the TRY or inside the
CATCH statement, when there is a FINALLY clause? Especially when there is a
transaction going on, in the try/catch block?...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |