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 understnad 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 will be very gratefull for your response.

regards,
Abhishek.

Re: Strange behavior of my program by bB

bB
Sat Jul 26 13:56:09 CDT 2003

Have you got any refernces to the connection higher up the stack e.g. a
datareader? If you have, close the datareader first. Deal with all of the
dependent referneces cleanly before closing the connection or the GC will
freak when it is processing your 'destructor' - remember desctructors in a
managed (GC'd)language are not the same as somethig like C++, as they are
not called immeadiately only when GC runs.

I maybe wrong though...

Tim.

"Abhishek Srivastava" <abhishek@nospam.net> wrote in message
news:ukKvpZ2UDHA.1928@TK2MSFTNGP12.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(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 understnad 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 will be very gratefull for your response.
>
> regards,
> Abhishek.
>



Re: Strange behavior of my program by Abhishek

Abhishek
Mon Jul 28 01:47:40 CDT 2003

Hello DB,

I don't have a data reader in my program. My program uses
ExecuteNonQuery. (To insert rows in the table).

I searched google and I found people who faced this error when they were
prematurely closing the datareader.

But that is not the case with my application.

Thanks for your help. Any ideas why I am getting this error even when I
don't use DataReader in my application.

regards,
Abhishek.
bB wrote:
> Have you got any refernces to the connection higher up the stack e.g. a
> datareader? If you have, close the datareader first. Deal with all of the
> dependent referneces cleanly before closing the connection or the GC will
> freak when it is processing your 'destructor' - remember desctructors in a
> managed (GC'd)language are not the same as somethig like C++, as they are
> not called immeadiately only when GC runs.
>
> I maybe wrong though...
>
> Tim.
>
> "Abhishek Srivastava" <abhishek@nospam.net> wrote in message
> news:ukKvpZ2UDHA.1928@TK2MSFTNGP12.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(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 understnad 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 will be very gratefull for your response.
>>
>>regards,
>>Abhishek.
>>
>
>
>