473,659 Members | 2,602 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.InvalidO perationExcepti on: Handle is not
initialized
..
at System.WeakRefe rence.get_Targe t()
at System.Data.Com mon.WeakReferen ceCollection.Cl ose(Boolean flag)
at System.Data.Ole Db.OleDbConnect ion.CloseRefere nces(Boolean canceling)
at System.Data.Ole Db.OleDbConnect ion.DisposeMana ged()
at System.Data.Ole Db.OleDbConnect ion.Close()
at LoadCkpt.Finali ze()
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 (ExecuteNonQuer y).

I will be very gratefull for your response.

regards,
Abhishek.

Nov 15 '05 #1
4 5016
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.Dispos e in the event. Override Dispose in your DB connection class,
and close your connection there.

Chris

"Abhishek Srivastava" <ab******@nospa m.net> wrote in message
news:uG******** ******@tk2msftn gp13.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.InvalidO perationExcepti on: Handle is not
initialized
.
at System.WeakRefe rence.get_Targe t()
at System.Data.Com mon.WeakReferen ceCollection.Cl ose(Boolean flag)
at System.Data.Ole Db.OleDbConnect ion.CloseRefere nces(Boolean canceling) at System.Data.Ole Db.OleDbConnect ion.DisposeMana ged()
at System.Data.Ole Db.OleDbConnect ion.Close()
at LoadCkpt.Finali ze()
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 (ExecuteNonQuer y).

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.SuppressFina lyze( this );
"Abhishek Srivastava" <ab******@nospa m.net> ha scritto nel messaggio
news:uG******** ******@tk2msftn gp13.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.InvalidO perationExcepti on: Handle is not
initialized
.
at System.WeakRefe rence.get_Targe t()
at System.Data.Com mon.WeakReferen ceCollection.Cl ose(Boolean flag)
at System.Data.Ole Db.OleDbConnect ion.CloseRefere nces(Boolean canceling) at System.Data.Ole Db.OleDbConnect ion.DisposeMana ged()
at System.Data.Ole Db.OleDbConnect ion.Close()
at LoadCkpt.Finali ze()
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 (ExecuteNonQuer y).

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.SuppressFina lyze( this );
"Abhishek Srivastava" <ab******@nospa m.net> ha scritto nel messaggio
news:uG******** ******@tk2msftn gp13.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.InvalidO perationExcepti on: Handle is not
initialized
.
at System.WeakRefe rence.get_Targe t()
at System.Data.Com mon.WeakReferen ceCollection.Cl ose(Boolean flag)
at System.Data.Ole Db.OleDbConnect ion.CloseRefere nces(Boolean


canceling)
at System.Data.Ole Db.OleDbConnect ion.DisposeMana ged()
at System.Data.Ole Db.OleDbConnect ion.Close()
at LoadCkpt.Finali ze()
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 (ExecuteNonQuer y).

I will be very gratefull for your response.

regards,
Abhishek.



Nov 15 '05 #4
Abhishek Srivastava <ab******@nospa m.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.co m>
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
1392
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. #!/usr/bin/python import string
36
3426
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 print 2.
7
1705
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> void main(){ exit(EXIT_SUCCESS); }
6
8522
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 of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
31
6618
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 variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
10
1755
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: // BEGIN #include<iostream> int main(void) {
1
2293
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 value of s is "test2.icon1.ico". This program will compile and run just fine from visual studio .net 2003 using .net 1.1 and also will compile and run just fine using csc form1.cs /resource:icon1.ico. Now, I created a new string called s2 and...
8
3207
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: Whenever I declare the sockaddr_in structure inside the main, the debugger crashes at line X*, not being able to access argv parameters (see code below). It is very strange.. by only being there, sockaddr_in does not allow me to question argc...
19
1742
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 that I remember that). Now I am getting this error: $ make g++ -Wall -ansi -pedantic -c pirma_lib.cpp g++ -Wall -ansi -pedantic -c pirma.cpp In file included from pirma.cpp:2:
20
2223
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 ----------------------------------- #include <stdio.h> int main (void) { char xx="abcd"; char * p1 = xx;
0
8428
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
8341
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
8851
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...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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
6181
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...
1
2759
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 we have to send another system
2
1739
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.