PC Review


Reply
Thread Tools Rate Thread

DbConnection Close Throwing Exception

 
 
rbDeveloper
Guest
Posts: n/a
 
      24th Jun 2008
In the code below, I'm setting a watch on the variable dbCon. When I expand
the watch on dbCon, one of the visible items under dbCon is "ServerVersion."

When the dbCon.Close() executes, I see the following:

dbCon.ServerVersion threw an expection of type
'System.InvalidOperationException'

....and the details...

Operation is not valid due to the current state of the object.

I'd rather not throw exceptions. Any ideas what I need to do before closing
to avoid all this?

Thanks...


DbConnection dbCon = null;

dbCon = db.CreateConnection();
dbCon.ConnectionString = DBConstants.ConnectionString(dbCon.ConnectionString);
dbCon.Open();

adpt.SelectCommand = dbCmd;
adpt.SelectCommand.Connection = dbCon;
adpt.Fill(result);

dbCon.Close();
 
Reply With Quote
 
 
 
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      24th Jun 2008
On Jun 24, 12:37*pm, rbDeveloper
<rbDevelo...@discussions.microsoft.com> wrote:
> In the code below, I'm setting a watch on the variable dbCon. When I expand
> the watch on dbCon, one of the visible items under dbCon is "ServerVersion."
>
> When the dbCon.Close() executes, I see the following:
>
> dbCon.ServerVersion threw an expection of type
> 'System.InvalidOperationException'
>
> ...and the details...
>
> Operation is not valid due to the current state of the object.
>
> I'd rather not throw exceptions. Any ideas what I need to do before closing
> to avoid all this?


You need to copy the value BEFORE closing the connection, if the
conenction is close you cannot access it
 
Reply With Quote
 
rbDeveloper
Guest
Posts: n/a
 
      24th Jun 2008
In this question, I'm actually all set with the data. The thing I'm trying to
figure out is what I need to do so that I don't get an exception when I call
dbCon.Close().



"Ignacio Machin ( .NET/ C# MVP )" wrote:

> On Jun 24, 12:37 pm, rbDeveloper
> <rbDevelo...@discussions.microsoft.com> wrote:
> > In the code below, I'm setting a watch on the variable dbCon. When I expand
> > the watch on dbCon, one of the visible items under dbCon is "ServerVersion."
> >
> > When the dbCon.Close() executes, I see the following:
> >
> > dbCon.ServerVersion threw an expection of type
> > 'System.InvalidOperationException'
> >
> > ...and the details...
> >
> > Operation is not valid due to the current state of the object.
> >
> > I'd rather not throw exceptions. Any ideas what I need to do before closing
> > to avoid all this?

>
> You need to copy the value BEFORE closing the connection, if the
> conenction is close you cannot access it
>

 
Reply With Quote
 
Jeff Winn
Guest
Posts: n/a
 
      25th Jun 2008
If you're looking at the watch you created earlier and see it doing that -
it's normal. When the properties are being checked by the watch you created,
and an exception gets thrown while it's updating the watch it just shows you
there was a problem. Those exceptions won't get thrown within your
application unless you access the dbCon.ServerVersion property after you've
closed the connection in your own code.

"rbDeveloper" <(E-Mail Removed)> wrote in message
news:3E92AA2E-70A0-4175-9C21-(E-Mail Removed)...
> In this question, I'm actually all set with the data. The thing I'm trying
> to
> figure out is what I need to do so that I don't get an exception when I
> call
> dbCon.Close().
>
>
>
> "Ignacio Machin ( .NET/ C# MVP )" wrote:
>
>> On Jun 24, 12:37 pm, rbDeveloper
>> <rbDevelo...@discussions.microsoft.com> wrote:
>> > In the code below, I'm setting a watch on the variable dbCon. When I
>> > expand
>> > the watch on dbCon, one of the visible items under dbCon is
>> > "ServerVersion."
>> >
>> > When the dbCon.Close() executes, I see the following:
>> >
>> > dbCon.ServerVersion threw an expection of type
>> > 'System.InvalidOperationException'
>> >
>> > ...and the details...
>> >
>> > Operation is not valid due to the current state of the object.
>> >
>> > I'd rather not throw exceptions. Any ideas what I need to do before
>> > closing
>> > to avoid all this?

>>
>> You need to copy the value BEFORE closing the connection, if the
>> conenction is close you cannot access it
>>


 
Reply With Quote
 
rbDeveloper
Guest
Posts: n/a
 
      25th Jun 2008
Thanks!

"Jeff Winn" wrote:

> If you're looking at the watch you created earlier and see it doing that -
> it's normal. When the properties are being checked by the watch you created,
> and an exception gets thrown while it's updating the watch it just shows you
> there was a problem. Those exceptions won't get thrown within your
> application unless you access the dbCon.ServerVersion property after you've
> closed the connection in your own code.
>
> "rbDeveloper" <(E-Mail Removed)> wrote in message
> news:3E92AA2E-70A0-4175-9C21-(E-Mail Removed)...
> > In this question, I'm actually all set with the data. The thing I'm trying
> > to
> > figure out is what I need to do so that I don't get an exception when I
> > call
> > dbCon.Close().
> >
> >
> >
> > "Ignacio Machin ( .NET/ C# MVP )" wrote:
> >
> >> On Jun 24, 12:37 pm, rbDeveloper
> >> <rbDevelo...@discussions.microsoft.com> wrote:
> >> > In the code below, I'm setting a watch on the variable dbCon. When I
> >> > expand
> >> > the watch on dbCon, one of the visible items under dbCon is
> >> > "ServerVersion."
> >> >
> >> > When the dbCon.Close() executes, I see the following:
> >> >
> >> > dbCon.ServerVersion threw an expection of type
> >> > 'System.InvalidOperationException'
> >> >
> >> > ...and the details...
> >> >
> >> > Operation is not valid due to the current state of the object.
> >> >
> >> > I'd rather not throw exceptions. Any ideas what I need to do before
> >> > closing
> >> > to avoid all this?
> >>
> >> You need to copy the value BEFORE closing the connection, if the
> >> conenction is close you cannot access it
> >>

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does DbConnection.Dispose() call Close()? =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= Microsoft ADO .NET 2 17th Mar 2006 10:07 AM
throwing new Exception Daniel Groh Microsoft C# .NET 1 3rd May 2005 02:38 PM
Throwing Exception SK Microsoft Dot NET 2 16th Aug 2004 11:44 AM
RE: Re-throwing an exception =?Utf-8?B?SmFrb2IgQ2hyaXN0ZW5zZW4=?= Microsoft Dot NET Framework 0 28th May 2004 09:06 AM
Re: Re-throwing an exception Jon Skeet [C# MVP] Microsoft Dot NET Framework 0 27th May 2004 06:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:02 AM.