Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: How to pass linq-objects around

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....

like image 515
Vegar Avatar asked Apr 21 '09 18:04

Vegar


1 Answers

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

like image 184
Jose Basilio Avatar answered Sep 20 '22 20:09

Jose Basilio