Newbie Newbie - 12 days ago 4
C# Question

Error on foreach Cannot access a disposed object

I have a table cleaner, and a table called timeconfirmation with column called cleaner id, with a foreign key back to the id column in cleaner.
Here is the code I am getting an error by second loop, Cannot access a disposed object. I can't figure out what I did wrong?

@foreach (var c in Model.Cleaners)
{
@foreach (var p in c.TimeConfirmations)
{
<span>@p.id</span>
}
}

Answer

When you are getting the list of cleaners are you loading the TimeConfirmations with it? If you are using linq to sql just do something like this

  var loadOptions = new DataLoadOptions();
            loadOptions.LoadWith<Cleaner>(c => c.TimeConfirmations);
            context.LoadOptions = loadOptions;

Hope this helps!

Comments