Question

im binding List<T> to my GridView where T is the object created by EntityFramework from my database. There is a foreign key in my said table and i want to display its corresponding text value in the GridView.

 <asp:TemplateField HeaderText="Foreign Key Type">
          <ItemTemplate>
              <asp:Label ID="LabelID" Visible="true"  runat="server"
                         Text="<%# Item.<foreignKeyTable>.Text %>"  >
              </asp:Label>                                               
          </ItemTemplate>
 </asp:TemplateField>

when i do that i get the following error

The operation cannot be completed because the DbContext has been disposed.

how can i get pass that

Was it helpful?

Solution

Most likely, you have query code that does the following:

using (var ctx = new SomeContext())
{
   var data = ctx.Data.Where(..).ToList();
   return data;
}

You may not have a using, but either way, the context has it's Dispose() method on it, and any unloaded navigation properties will always fail, because there is no context alive for it to attach to. Even though you may use the context within a method, and may not be storing it globally, as long as you aren't explicitly disposing the context, by calling the method or with a using statement, you will be fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top