473,508 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange behavior of my program

Hello All,

I wrote a program in which I have a OleDbConnection which I open in the
contructor and close in the Destructor. When I run the program I get the
following error

Unhandled Exception: System.InvalidOperationException: Handle is not
initialized
..
at System.WeakReference.get_Target()
at System.Data.Common.WeakReferenceCollection.Close(B oolean flag)
at System.Data.OleDb.OleDbConnection.CloseReferences( Boolean canceling)
at System.Data.OleDb.OleDbConnection.DisposeManaged()
at System.Data.OleDb.OleDbConnection.Close()
at LoadCkpt.Finalize()
If I remove the contructor and destructor and open and close the
connection in my method only, then the program runs successfully.

I don't understand why is this error coming? Why is it that I can't
open/close my connection in my contructor descructor?

Has anyone else seen this kind of error? I searched google and found
that some people got this error when the closed the DataReader
prematurely. But in my case I don't have any DataReader since my query
is just to insert a record (ExecuteNonQuery).

I will be very gratefull for your response.

regards,
Abhishek.

Nov 15 '05 #1
4 5007
I think the problem is putting something into the destructor. In general,
you really don't do that. Is this class instantiated from a main form, or is
it the main form? If it's possible to destroy it on a Form_Close event, call
myObject.Dispose in the event. Override Dispose in your DB connection class,
and close your connection there.

Chris

"Abhishek Srivastava" <ab******@nospam.net> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
Hello All,

I wrote a program in which I have a OleDbConnection which I open in the
contructor and close in the Destructor. When I run the program I get the
following error

Unhandled Exception: System.InvalidOperationException: Handle is not
initialized
.
at System.WeakReference.get_Target()
at System.Data.Common.WeakReferenceCollection.Close(B oolean flag)
at System.Data.OleDb.OleDbConnection.CloseReferences( Boolean canceling) at System.Data.OleDb.OleDbConnection.DisposeManaged()
at System.Data.OleDb.OleDbConnection.Close()
at LoadCkpt.Finalize()
If I remove the contructor and destructor and open and close the
connection in my method only, then the program runs successfully.

I don't understand why is this error coming? Why is it that I can't
open/close my connection in my contructor descructor?

Has anyone else seen this kind of error? I searched google and found
that some people got this error when the closed the DataReader
prematurely. But in my case I don't have any DataReader since my query
is just to insert a record (ExecuteNonQuery).

I will be very gratefull for your response.

regards,
Abhishek.

Nov 15 '05 #2
Agreeing with Chris...also
-) Good practice closing connection as soon as possible, not in destructor
-) Good practice not to have a destructor, better implementing or calling
Dispose()..
-) If you have a Destructor somebody says that you should call
GC.SuppressFinalyze( this );
"Abhishek Srivastava" <ab******@nospam.net> ha scritto nel messaggio
news:uG**************@tk2msftngp13.phx.gbl...
Hello All,

I wrote a program in which I have a OleDbConnection which I open in the
contructor and close in the Destructor. When I run the program I get the
following error

Unhandled Exception: System.InvalidOperationException: Handle is not
initialized
.
at System.WeakReference.get_Target()
at System.Data.Common.WeakReferenceCollection.Close(B oolean flag)
at System.Data.OleDb.OleDbConnection.CloseReferences( Boolean canceling) at System.Data.OleDb.OleDbConnection.DisposeManaged()
at System.Data.OleDb.OleDbConnection.Close()
at LoadCkpt.Finalize()
If I remove the contructor and destructor and open and close the
connection in my method only, then the program runs successfully.

I don't understand why is this error coming? Why is it that I can't
open/close my connection in my contructor descructor?

Has anyone else seen this kind of error? I searched google and found
that some people got this error when the closed the DataReader
prematurely. But in my case I don't have any DataReader since my query
is just to insert a record (ExecuteNonQuery).

I will be very gratefull for your response.

regards,
Abhishek.

Nov 15 '05 #3
Thanks to both of you for replying to my question.
I agree with the points raised, that I should not close my connection in
the destructor but as soon as possible.

It may be a poor practice... but why does it throw and error? and if I
just move the code of connection opening and closing in my method rather
than constructor and destructor then everything works fine.

I agree that the practise is not good... but logically it should throw
me a handle invalid error.

My program works when I follow your suggestions and eliminate the error.
However I want to understand 'why' (the causality) behind this error
message ;-)

regards,
Abhishek.

Christian wrote:
Agreeing with Chris...also
-) Good practice closing connection as soon as possible, not in destructor
-) Good practice not to have a destructor, better implementing or calling
Dispose()..
-) If you have a Destructor somebody says that you should call
GC.SuppressFinalyze( this );
"Abhishek Srivastava" <ab******@nospam.net> ha scritto nel messaggio
news:uG**************@tk2msftngp13.phx.gbl...
Hello All,

I wrote a program in which I have a OleDbConnection which I open in the
contructor and close in the Destructor. When I run the program I get the
following error

Unhandled Exception: System.InvalidOperationException: Handle is not
initialized
.
at System.WeakReference.get_Target()
at System.Data.Common.WeakReferenceCollection.Close(B oolean flag)
at System.Data.OleDb.OleDbConnection.CloseReferences( Boolean


canceling)
at System.Data.OleDb.OleDbConnection.DisposeManaged()
at System.Data.OleDb.OleDbConnection.Close()
at LoadCkpt.Finalize()
If I remove the contructor and destructor and open and close the
connection in my method only, then the program runs successfully.

I don't understand why is this error coming? Why is it that I can't
open/close my connection in my contructor descructor?

Has anyone else seen this kind of error? I searched google and found
that some people got this error when the closed the DataReader
prematurely. But in my case I don't have any DataReader since my query
is just to insert a record (ExecuteNonQuery).

I will be very gratefull for your response.

regards,
Abhishek.



Nov 15 '05 #4
Abhishek Srivastava <ab******@nospam.net> wrote:
Here is my application. it throws the exception which was specified in
my post. If you remove the constructor and destructor and move those
lines of code in the method itself, then the program works fine.

Though the practice of opening/closing connection in
constructor/destructor is not the best one, it should not throw an error.

Though I am able to solve the problem by your suggestions, I still need
to know why the error occurred.


Bear in mind that when your object is finalized, the connection may
already have been finalized. That may be the actual problem, but I
don't know for sure...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

1
1389
by: Dan Stromberg | last post by:
The below small program is giving strange behavior. At the bottom of the code, please find "this works" and "this doesn't work" comments. Why does one work and the other not? TIA. ...
36
3383
by: Dmitriy Iassenev | last post by:
hi, I found an interesting thing in operator behaviour in C++ : int i=1; printf("%d",i++ + i++); I think the value of the expression "i++ + i++" _must_ be 3, but all the compilers I tested...
7
1696
by: Ziggy | last post by:
Just for curiosity. I know that main function is returning an int, but I came cross with the following code. (Unnessecary code is snipped) Is there any problem with it? #include <stdlib.h>...
6
8503
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
31
6575
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
10
1743
by: weichaoliu | last post by:
I know that this problem is concerned to GCC. But I hope somebody here can tell me some detailed things about why. The version of g++ I'm using is: g++ (GCC) 3.4.4 (mingw special). The code is:...
1
2276
by: hansolox1 | last post by:
The following program simply sets the icon of a form called form1. When I get the name of the embedded icon using GetManifestResourceNames(), I store the name in a string variable called s. The...
8
3187
by: FBM | last post by:
Hi there, I am puzzled with the behavior of my code.. I am working on a networking stuff, and debugging with eclipse (GNU gdb 6.6-debian).. The problem I am experiencing is the following: ...
19
1728
by: david | last post by:
I took old code and decided to modify it a bit, and I just noticed that it does not compile at all and before server one of severs (main) crashed in the system it was working fine (I am really sure...
20
2204
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
0
7225
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
7326
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,...
0
7383
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
5627
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,...
1
5053
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...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
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...
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
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...

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.