You must Sign In to post a response.
  • Category: ASP.NET

    The ObjectContext instance has been disposed and can no longer be used for operations that require a

    Hi All,
    I got this exception when fetch data using web method in aspx page, how can handle it, but i receive data from server side, but not display in aspx page.


    No data found....{"Message":"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.","StackTrace":" at System.Data.Objects.ObjectContext.EnsureConnection()\r\n at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)\r\n at System.Data.Objects.ObjectQuery`1.Execute(MergeOption mergeOption)\r\n at System.Data.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption)\r\n at System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption)\r\n at System.Data.Objects.DataClasses.RelatedEnd.Load()\r\n at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()\r\n at System.Data.Objects.DataClasses.EntityCollection`1.System.Collections.IEnumerable.GetEnumerator()\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ObjectDisposedException"}
  • #765721
    Hi,
    Have you used .AsEnumerable() inside web method? it will return a lazy enumerator i.e. a context or data source may gets closed before you receive data into your datastructure. So use .ToArray() or .ToList() instead.
    Also go through following URL's:
    http://forums.asp.net/t/1685186.aspx?The+ObjectContext+instance+has+been+disposed+and+can+no+longer+be+used+for+operations+that+require+a+connection+

    http://stackoverflow.com/questions/4206634/system-objectdisposedexception-the-objectcontext-instance-has-been-disposed-and

  • #765724
    Can you post your code so that we can easily identify the issue. As per the error message, I understood that you are trying to access the object which you already disposed. please check the code using the debugger and also check are you trying to access the object which already disposed.

    If you post the code we can easily identify the issue exactly,

    By Nathan
    Direction is important than speed

  • #765725
    Are you using entity framework ? if yes, then You are trying to access an association property after the data context has been disposed. Entity Framework, by default, loads association properties lazily. In other words, it makes another trip to the database when you try to access the association property for the first time. To make this trip to the database, Entity Framework must use a data context
    By default Entity Framework uses lazy-loading for navigation properties. That's why these properties should be marked as virtual - EF creates proxy class for your entity and overrides navigation properties to allow lazy-loading
    checkout below link for more detail
    http://stackoverflow.com/questions/22007025/the-objectcontext-instance-has-been-disposed-and-can-no-longer-be-used-for-opera
    http://stackoverflow.com/questions/18398356/how-to-solve-the-error-the-objectcontext-instance-has-been-disposed-and-can-no-l

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments