Solved

dispose of a DataTable created in another class

Posted on 2016-10-23
4
49 Views
Last Modified: 2016-10-25
I have a windows form and a class.  One of the methods in the class creates a DataTable and returns it to the calling form method.  Do I need to explicity dispose of the DataTable when I'm done with it?  If so, how?
0
Comment
Question by:dhenderson12
4 Comments
 
LVL 50

Assisted Solution

by:Ryan Chong
Ryan Chong earned 125 total points
ID: 41856547
I just read this and it could be help explained to you.

Should Dispose be called on DataTable and DataSet objects?
http://web.archive.org/web/20081203232123/http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.adonet/topic42917.aspx

...the system.data namespace (ADONET) does not contain
unmanaged resources. Therefore there is no need to dispose any of those as
long as you have not added yourself something special to it.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 125 total points
ID: 41857017
Hi  dhenderson12;

You do not need to call DataTable.Dispose() because the object does not contain any unmanaged components and therefor no need execute Dispose method. Now if you were to somehow attach a unmanaged object to the DataTable then you would need to call dispose on it. You can read this web page  Using Objects That Implement IDisposable to see examples.
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
ID: 41857747
Seems the overall recommendation is that you don't need to call .Dispose() even though the DataTable class does implement a class that implements IDisposable.

A little research indicates that the class that DataTable implements that implements IDisposable is MarshalByValueComponent.

You can read the definition, but it appears to be that if you're implementing remotable components that are marshalled by value -- passing copies of serialized objects -- then you need to Dispose.

I don't think anyone does this. So, you're likely extremely safe not calling Dispose or worrying about it. I've used DataTables for years without Dispose and no one has yelled at me, yet.

If you really, really, really want to Dispose, then do it in a using statement like this:

using (DataTable dt = myClassAsInstanceObject.CreateCoolDataTable())
{
   // do some cool data stuff
}

Open in new window

0
 

Author Closing Comment

by:dhenderson12
ID: 41858637
Thanks to all for your help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wav problem 4 28
Hey Audio! 9 44
I need help with Crystal Report's Parameter Prompt Form 2 48
Amazon S3 .Net error 5 15
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question