473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

why this is wrong with this finally


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();
}
Nov 15 '05 #1
3 1128
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();
}

Nov 15 '05 #2
you can also try this. once the object exit the USING block, it will
automatically disposed
the object.

try
{
using (SqlCommand dbCmd = new SqlCommand())
{
//do something
using (SqlDataReader rdReader= dbCmd.ExecuteReader())
{
//do something
}
}
}
catch
{
//do something
}

--
gani


"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();
}

Nov 15 '05 #3
Hi,
Initilize the variable to null when you declare it.

OleDbDataReader rdReader = null;
OleDbCommand dbCmd = null;

Cheers
Benny

Ahmet AKGUN wrote:
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();
}


Nov 15 '05 #4

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

Similar topics

3
by: Roger Redford | last post by:
Dear experts, I'm trying to learn java on my own. I picked up a sample online, but it is not compiling right: ------------------------------------------------ import java.io.*;
5
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
16
by: SLIMSHIM | last post by:
Hi, I"m new to c# and .net. I wrote a small program to add rows to an access table. the program goes thru the motions but the data never gets there. here is my code. I am intentionaly not using...
4
by: =?Utf-8?B?VG9kZCBKYXNwZXJz?= | last post by:
Here is what I have: private int NationalCount() { Int32 numRecords = 0; using (SqlConnection dataConnection = new SqlConnection(GlobalVars.sqlConnString)) { SqlCommand dataCommand = new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.