views:

1809

answers:

4

When showing my main window, I make a list of objects from linq-to-sql:

 using (var context = new Data.TVShowDataContext())
 {
    Shows = new ObservableCollection<Data.Show>(context.Shows);
    listShows.ItemsSource = Shows;
 }

Now, when I doubleclick an item in my list, I want to use the selected object in a new usercontrol:

 ShowEpList epList = new ShowEpList();
 epList.DataContext = (Data.Show)listShows.SelectedItem;

Now, this results in an exception:

System.ObjectDisposedException was unhandled
Message="Cannot access a disposed object.\r\nObject name: 'DataContext accessed after Dispose.'."
Source="System.Data.Linq"
ObjectName="DataContext accessed after Dispose."

I guess this comes as a result of binding a list to the shows season-list, and the season-list needs to be populated or something:

<ListBox Name="listSeasons" ItemsSource="{Binding Seasons}"/>

Now, what I would like to know, is how this is supposed be done? Would I need to make a new DataContext-object and retrieve the selected show again, or is it possible to re-conntect my show with a new datacontext to populate the seasons-list?

I guess I'm a little lost....

+9  A: 

Do not use a "using" statement with your datacontext. Please refer to this post: Disposing DataContext

Jose Basilio
I thought this was the way to use datacontexts - create, use and dispose. I guess I was wrong.. :) Thanks.
Vegar
+3  A: 

When you use a using() block, it calls Dispose() on whatever you've put into its initializer (in this case, your context). When a LINQ to SQL context is disposed, the entities created with it are as well. I would suggest creating an instance variable on your Form for the context, and insert code into your Form's Dispose(bool) function (it's in the Designer.cs file) to dispose of the context.

Adam Robinson
+1  A: 

Is there a reason you can't maintain the DataContext object for longer, hold it perhaps as Resource in the page. Ordinarily you wouldn't want a DataContext living too long however if it used only for queries relevant to the current page and is disposed with the page that may be a good compromise.

You would want to be careful in a multi-user environment where the backend values represented in the Datacontext change quickly. However I would think that TV Schedules don't keep changing rapidly.

AnthonyWJones
It doesn't matter if the data changes rapidly. The only reason to dispose is to free up a database connection. Unless there is something I don't understand, in which case, enlighten me.
Richard Hein
Actually I just read that DataContext does not keep open connections, so there is no reason to dispose, unless you need to free up memory more frequently, because DataContext will track state changes. Only long running server applications are likely to run into problems with state tracking taking up too much memory. Eventually the GC will release the memory automatically, so you only need to dispose if you have problems with memory usage and this may help optimize things.
Richard Hein
A: 

Actually there are valid reasons to use those objects apart from the datacontext. what about passing the objects back to a web service call?