Does DbConnection.Dispose() call Close()?

dotnet framework


  • 1. Global Connection object?
    Hi, in the old times (I mean before .NET) we had to use only one connection object in our applicaiton, since the connection eats a lot of resources, and it takes a long time to connect. Unfortunatelly I can not see any sample where are more, than one form, and therefore I don't know how to keep only one connect, what can be used allover the forms. Any idea? Thanks: Peter
  • 2. Adding record to disconnected DataTable slow
    I have an ADO.NET datatable which I created from scratch - just defined its columns and started adding data. I find that when I step through adding a row in the debugger, it's quite slow - up to several seconds - at the point where I actually add the row to the table. Any ideas? TIA - Turtle
  • 3. Insert NULL into SQL Server Image column
    Hi, I have a SQL Server database with a column of type "Image" that I am using to store bitmaps. I am able to store and retrieve bitmaps from C# using well documented methods on various internet sites but since my bitmap is optional I also need the ability to insert a null into this column. Using the following code gives an error for an INSERT command: sqlCmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Picture", System.Data.SqlDbType.VarBinary, 2147483647, "Picture")); sqlCmd.Parameters["@Picture"].Value = DBNull.Value; sqlCmd.ExecuteNonQuery(); Upon the execute, I get the error "An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in Additional information: System error." Can somebody help me please and explain how it is possible to insert a NULL into an Image column since the DBNull.Value does not appear to work. Kind Regards Alasdair
  • 4. Invalid cast exception when assign combobox returned value to the variable
    Hi! I'm from Poland and sorry for my english ;-) I'm also beginner with ADO Net programing. My problem is, when I want to assign returned value from combobox, which is connected to an existing datatable in dataset. The combobox's propertises like datasource, displaymember and valuemember are sets properly. I achieve returned value through the combobox.selectedvalue propertie I guess that The Invalid cast exception occures because I don't exactly know what type of value combobox1.selectedvalue is... I want to assign this returned value to the column of another table (foreign key). I know that is typical operation, and it seems to be simple TO DO, but i can't find any clear tutorial (Even on the csharpcorner website). I will be very thankful for any samples codes (I suppose that it will be a few lines of code).... or explanation where my approach and understanding this trouble starts to be incorrectly.. Please don't call me LAMER for this, because I know it's often using operation. But I really tried to find satisfactory solution by google and another ways and I didn't think that it will be so difficult .....

Does DbConnection.Dispose() call Close()?

Postby RGF2aWQgVGhpZWxlbg » Sat, 18 Mar 2006 05:31:24 GMT


If I do using (DbConnection conn = ...) { ... code ... }

That will call dispose when it exits the using. Does that dispose call Close 
(if the connection is open) and will it do so even on an exception? Reading 
the docs I assume it does, but I can't find anywhere that it says absolutely 
it does.

thanks - dave

Re: Does DbConnection.Dispose() call Close()?

Postby Sericinus hunter » Sat, 18 Mar 2006 05:44:59 GMT

Here is how IDbConnection.Dispose() is implemented (as Reflector utility shows):

protected override void Dispose(bool disposing)
       if (disposing)
             switch (this._objectState)
                   case ConnectionState.Open:
             this._constr = null;

protected override void Dispose(bool disposing)
       if (disposing)
             this._constr = null;
             CNativeBuffer buffer1 = this._buffer;
             if (buffer1 != null)
                   this._buffer = null;

protected override void Dispose(bool disposing)
       if (disposing)
             if (this.objectState != 0)
                   if (base.DesignMode)
                   this.OnStateChange(ConnectionState.Open, ConnectionState.Closed);
             if (this.propertyIDSet != null)
                   this.propertyIDSet = null;
             this._constr = null;

Re: Does DbConnection.Dispose() call Close()?

Postby Cor Ligthert [MVP] » Sat, 18 Mar 2006 19:07:46 GMT


Yes and as extra does it remove the by you added connection.string as showed 
in the documentation.


Similar Threads:

1.Cannot call Dispose() while doing CreateHandle()

2.Can't call Close() while doing CreateHandle().

After some research and experimentation, I have found that the best way
to deal with the problem of closing the form while loading (at least in
my case) is to override the form's CreateHandle method.  This is very
similar to overloading the OnLoad method, but it appears that the
OnLoad method is called as a "piece" of the CreateHandle function.  If
you override CreateHandle (and allow it to call the base class
CreateHandle) then you can close the form *after* the base class'
CreateHandle is done.  After the base class' CreateHandle is done, the
form's Load and Activate methods/events have already completed, and the
form is in a state that will allow you to close it.  If necessary, keep
a member variable to track whether the form should close after
CreateHandle has completed.  But in many cases, you should just be able
to use CreateHandle instead of OnLoad.  In my case I had to keep a form
level variable because I wanted to close from within the Activated
event, even if the form had already been completely initialized.

3.Cannot call Close() while doing CreateHandle()

Is there a way around this error message?

Example (not what I'm actually doing but similar, this is a simple version):

Lets say you are creating some sort of calendar application you create a 
form which is a MDI container, another form which is the 'events' form and a 
dialog form which simply allows you to select a date with a ok and cancel 

Your app loads, the MDI container loads the events form. In the load of the 
events form it realises that it does not have a date, so therefore it loads 
the date dialog box, if you select a date and click on OK the events form 
loads all the events for that particular date. However if I click the cancel 
button I want the events form to close.


4.Cannot call Close() while doing CreateHandle

I get this exception when I call Me.Close on Load event of a Form. The form 
producing the error is an MDI child of another form. Any ideas how I can get 
over this problem please.

I have tried doing Me.Close on HandleCreated and Activated events but get 
the same exception. 

5.Calling Dispose instead of close, clear, ....


I've written the following little helper function which calls Dispose on 
classes which implement the idisposable interface.

     Public Sub DisposeObject(ByVal DisposeAbleObject As IDisposable)
         If Not DisposeAbleObject Is Nothing Then
         End If
     End Sub

The reason for this is to avoid ugly looking finally clauses with lots 
of checking if an object ref is nothing before calling dispose on them. 
For example in a try finally block in which a connection is opnened, a 
transaction is started and an sqldatareader gets created which looks 
like this:

   if sqlconnection is nothing then
   end if

   if Transaction is nothing then
   end if

   if Datareader is nothing then
   end if
end try

i'd write this:

end try

a lot better looking IMO. I dont have to worry about whether the ref is 
nothing or not.

Now my question is this: Is this going to cause any strange behavior? 
I've tested it and it seems to work.. but im wondering if there could be 
any side effects since im not calling the specific Close of the 
datareader or sqlconnection, im calling the Dispose. The documentation 
says there's not much difference in calling Dispose instead of Close on 
a connection, but can i assume the same for any object that implements 


6. DbConnection Close Throwing Exception

7. Form Events - Closing, Disposing - Removing from collections when closed

8. Does DataGridView.Dispose Call DataGridViewRow.Dispose?

Return to dotnet framework


Who is online

Users browsing this forum: No registered users and 77 guest