Note that there are some explanatory texts on larger screens.

plurals
  1. POSolving "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection" InvalidOperationException
    primarykey
    data
    text
    <p>I am trying to populate a <code>GridView</code> using Entity Frameworkm but every time I am getting the following error:</p> <blockquote> <p>"Property accessor 'LoanProduct' on object 'COSIS_DAL.MemberLoan' threw the following exception: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."</p> </blockquote> <p>My code is:</p> <pre><code> public List&lt;MemberLoan&gt; GetAllMembersForLoan(string keyword) { using (CosisEntities db = new CosisEntities()) { IQueryable&lt;MemberLoan&gt; query = db.MemberLoans.OrderByDescending(m =&gt; m.LoanDate); if (!string.IsNullOrEmpty(keyword)) { keyword = keyword.ToLower(); query = query.Where(m =&gt; m.LoanProviderCode.Contains(keyword) || m.MemNo.Contains(keyword) || (!string.IsNullOrEmpty(m.LoanProduct.LoanProductName) &amp;&amp; m.LoanProduct.LoanProductName.ToLower().Contains(keyword)) || m.Membership.MemName.Contains(keyword) || m.GeneralMasterInformation.Description.Contains(keyword) ); } return query.ToList(); } } protected void btnSearch_Click(object sender, ImageClickEventArgs e) { string keyword = txtKeyword.Text.ToLower(); LoanController c = new LoanController(); List&lt;COSIS_DAL.MemberLoan&gt; list = new List&lt;COSIS_DAL.MemberLoan&gt;(); list = c.GetAllMembersForLoan(keyword); if (list.Count &lt;= 0) { lblMsg.Text = "No Records Found"; GridView1.DataSourceID = null; GridView1.DataSource = null; GridView1.DataBind(); } else { lblMsg.Text = ""; GridView1.DataSourceID = null; GridView1.DataSource = list; GridView1.DataBind(); } } </code></pre> <p>The error is mentioning the <code>LoanProductName</code> column of the <code>Gridview</code>. Mentioned: I am using C#, ASP.net, SQL-Server 2008 as back end DB.</p> <p>I am quite new to Entity Framework. I can't understand why I am getting this error. Can anyone help me please?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload