473,616 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Closing DataReader inside of Try{}Catch{}

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(S qlStatement);
while(LocalRead er.Read())
{
... do some data stuff here...
}
}
catch(Exception er)
{
LocalReader.Clo se(); // unassigned local variable here
.... throw error, or whatever...
}
LocalReader.Clo se(); //this close is OK
return sResultArray; // or whatever...
}

private System.Data.Ole Db.OleDbDataRea der executeReader (string
sqlStmnt)
{
// Uses server connection that is extablished elsewhere
// prior to executing operations on db
System.Data.Ole Db.OleDbCommand cmd = new
OleDbCommand (sqlStmnt, this.get_servco nn());

System.Data.Ole Db.OleDbDataRea der rdr = cmd.ExecuteRead er();
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
Nov 17 '05 #1
4 7672
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.co m

"Jeff User" <je*******@hotm ail.com> wrote in message
news:08******** *************** *********@4ax.c om...
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(S qlStatement);
while(LocalRead er.Read())
{
... do some data stuff here...
}
}
catch(Exception er)
{
LocalReader.Clo se(); // unassigned local variable here
.... throw error, or whatever...
}
LocalReader.Clo se(); //this close is OK
return sResultArray; // or whatever...
}

private System.Data.Ole Db.OleDbDataRea der executeReader (string
sqlStmnt)
{
// Uses server connection that is extablished elsewhere
// prior to executing operations on db
System.Data.Ole Db.OleDbCommand cmd = new
OleDbCommand (sqlStmnt, this.get_servco nn());

System.Data.Ole Db.OleDbDataRea der rdr = cmd.ExecuteRead er();
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

Nov 17 '05 #2
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*******@hotm ail.com> wrote in message
news:08******** *************** *********@4ax.c om...
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(S qlStatement);
while(LocalRead er.Read())
{
... do some data stuff here...
}
}
catch(Exception er)
{
LocalReader.Clo se(); // unassigned local variable here
.... throw error, or whatever...
}
LocalReader.Clo se(); //this close is OK
return sResultArray; // or whatever...
}

private System.Data.Ole Db.OleDbDataRea der executeReader (string
sqlStmnt)
{
// Uses server connection that is extablished elsewhere
// prior to executing operations on db
System.Data.Ole Db.OleDbCommand cmd = new
OleDbCommand (sqlStmnt, this.get_servco nn());

System.Data.Ole Db.OleDbDataRea der rdr = cmd.ExecuteRead er();
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

Nov 17 '05 #3

"Jeff User" <je*******@hotm ail.com> wrote in message
news:08******** *************** *********@4ax.c om...
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(S qlStatement);
while(LocalRead er.Read())
{
... do some data stuff here...
}
}
catch(Exception er)
{
LocalReader.Clo se(); // unassigned local variable here
.... throw error, or whatever...
}
LocalReader.Clo se(); //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.Clo se();
}
Nov 17 '05 #4
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*******@hotm ail.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(S qlStatement);
while(LocalRead er.Read())
{
... do some data stuff here...
}
}
catch(Exception er)
{
LocalReader.Clo se(); // unassigned local variable here
.... throw error, or whatever...
}
LocalReader.Clo se(); //this close is OK
return sResultArray; // or whatever...
}

private System.Data.Ole Db.OleDbDataRea der executeReader (string
sqlStmnt)
{
// Uses server connection that is extablished elsewhere
// prior to executing operations on db
System.Data.Ole Db.OleDbCommand cmd = new
OleDbCommand (sqlStmnt, this.get_servco nn());

System.Data.Ole Db.OleDbDataRea der rdr = cmd.ExecuteRead er();
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


Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
6880
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 non-supporting browsers? TIA -- --
2
501
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?
23
3060
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 block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
4
1798
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 try to connect to Server2. Can I have the secong Try inside the first Catch for this purpose ?. Please take a look on my code and fix it if possible. Actually with this code, the database connection with Server2 is opened after the first...
8
2477
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? I give you the following example to meka it more clear: (I use Enterprise Library, but the same also applies without it) public function f_SomeFunction(parm1,....) as integer dim tr as DbTransaction
0
8199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8145
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8592
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7118
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6097
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4060
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4140
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1439
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.