Strange behavior of my program

  • Thread starter Thread starter Abhishek Srivastava
  • Start date Start date
A

Abhishek Srivastava

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(Boolean 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.
 
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
 
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 );
 
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.
 
Abhishek Srivastava said:
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...
 
Back
Top