DbConnection Close Throwing Exception

  • Thread starter Thread starter rbDeveloper
  • Start date Start date
R

rbDeveloper

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();
 
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
 
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().
 
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.
 
Thanks!

Jeff Winn said:
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.
 
Back
Top