0

First here I am tell all of you that I searched a lot in google, stack overflow and other forums. But didn't find any thing to solve my prob. So finally I am here to take my question in front of you developers.

Lets started: I am creating an web application in asp.net 4.0 using linqtosql, web method & ajax.

Below is my web method, which is works fine and return data into list:

[WebMethod]
public static List<Employee> getEmployee()
{
   var db = new BPDataContext();
   var elist = db.Employees.ToList(); //getting complete data inside elist successfully
   return elist;
}

Below is my javascript function, which is calling inside jquery document.ready and after that I am creating ajax call inside getEmployee() function :

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
    $(document).ready(function () {
        getEmployee();
    });
    function getEmployee() {
        $.ajax({
            url: "Emp_Page.aspx/getEmployee",
            data: null,
            contentType: "Application/json; charset=utf-8",
            responseType: "json",
            type: "POST",
            success: function (response) {
            alert(response.d);
            },
            error: function (xhr) {
                alert(xhr.status);
            },
            Failure: function (response) {
                alert(response);
            }
        });
    }
</script>
</script>
</asp:Content>

Problem: When page was loaded and alert(xhr.status) hits and shows me internal server error 500, but my web method works fine and retrieve all details which I needed when I put break point and check list. In chrome when I put break point before error:function(xhr), in response text its shows me below detailed error.

Message":"A circular reference was detected while serializing an object of type \u0027Adminpanel.Employee\u0027.","StackTrace":" at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)
at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)
at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"

Now I completely and briefly define my problem. Please help me out of it.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Ahmer Ali Ahsan
  • 5,636
  • 16
  • 44
  • 81
  • Well, you've got a circular reference somewhere inside of the data you're trying to serialize. That means that, for example, `object A` has a reference to `object B` and `object B` also has a reference to `object A`. This could be a much larger chain but that's the idea. To remove the error, you have to remove the circular reference. – Mike Cluck Feb 10 '16 at 23:28
  • You can debug your web method just fine because the problem is that the result cannot be serialized. Closing as duplicate. Check the accepted answer for a solution that should work in your case. – Eric J. Feb 10 '16 at 23:28
  • 2
    @MikeC: The WCF serializer can support circular references but that requires some special handling. See duplicate question. – Eric J. Feb 10 '16 at 23:29
  • Thanks for your comments. I read mentioned duplicated post. But to be very frank I am a beginner and need more time to practice and learn these attributes. I came here to mention my solution of my post. `db.Configuration.LazyLoadingEnabled = false;` I put this code after my `var db = new BPDataContext();` and I get free from my 500 internal server error. – Ahmer Ali Ahsan Feb 12 '16 at 04:39

0 Answers0