ASP.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a ASP.NET Expert!

Join 300,406 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 1,652 people online right now. Registration is fast and FREE... Join Now!




Circular Reference error {RESOLVED}

 

Circular Reference error {RESOLVED}

PsychoCoder

29 Jun, 2009 - 03:22 PM
Post #1

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Not sure if I should have posted this here or in the JQuery forum since I'm not 100% sure where the error's occurring. I'm working with ASP.NET w/ C# and JQuery. I have JQuery communicating with a Web Service which is getting data from a SQL table. In the method I'm converting the Dataset to an XMLDocument, then converting that to an XDocument with the help of an extension method. Here's the code

Web Service
CODE

[WebMethod]
public static IEnumerable GetPriceListByAnimal(string animal)
{
    SQL sql = new SQL("GctConn");
    DataSet list = new DataSet();
    string query = "uspGetPriceListByAnimal";

    using (SqlConnection conn = new SqlConnection(sql.GetConnectionString()))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@animal_name", animal);
            cmd.Connection = conn;
            adapter.SelectCommand = cmd;
            adapter.Fill(list);

            XmlDocument xd = new XmlDocument();
            xd.LoadXml(list.GetXml());

            XDocument doc = xd.ToXDocument();

            var priceList = from s in doc.Elements("NewDataSet").Elements("Table")
                            select new
                            {
                                MountType = s.Element("TypeName").Value,
                                Price = s.Element("MountPrice").Value
                            };
            return priceList;
        }
    }
}


Extension Method
CODE

public static class XmlDocumentExtensions
{
  public static XDocument ToXDocument(this XmlDocument xd)
  {
    return xd.ToXDocument(LoadOptions.None);
  }

  public static XDocument ToXDocument(this XmlDocument xd, LoadOptions opt)
  {
    using (XmlNodeReader reader = new XmlNodeReader(xd))
    {
      return XDocument.Load(reader, opt);
    }
  }
}



Then in the page I'm using this in I have the following JQuery code for calling the method and displaying the results using AJAX

CODE

<script type="text/javascript">
    $(document).ready( function()
    {
        IMG.ImageRollover.init();
        /* Initialize Tabs */
        $("#NavTabs").tabs("#TabPanes > div", { effect: 'fade' });

        //click event for each tab
        $("a.trigger").click(function()
        {
            var animal = $(this).attr("id");
            //alert("Tab ID: " + animal);
            //do the AJAX call
            $.ajax(
            {
                type: "POST",
                url: "GCTService.asmx/GetPriceListByAnimal",
                data: "{ 'animal': '" + animal + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg)
                {
                   FormatResults(msg);
                },
                error:function(XMLHttpRequest, textStatus, errorThrown)
                {
                    $('#Results').html(XMLHttpRequest.status +"\r\n" + XMLHttpRequest.responseText);
                }
            });
        });            
    });
    function FormatResults(msg)
    {
        var table = '<table><thead><tr><th>Mount Type</th><th> </th><th>Price</th></thead><tbody>';

          for (var s in msg)
          {
            var row = '<tr>';
        
            row += '<td>' + msg[s].MountType + '</td>';
            row += '<td> </td>';
            row += '<td>' + msg[s].Price + '</td>';
        
            row += '</tr>';
        
            table += row;
          }
        
          table += '</tbody></table>';
        
          $('#Results').html(table);
    }
</script>



When I click one of the tabs I get a big error (and I do mean big

QUOTE

500 {"Message":"A circular reference was detected while serializing an object of type \u0027System.Xml.Linq.XElement\u0027.","StackTrace":" at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\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)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\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)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\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)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\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)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\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)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\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)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\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)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\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.InvalidOperationException"}


First question, anyone know what's causing this error? Is it the conversion from an XmlDocument to an XDocument? Is it an error in the JQuery AJAX itself (If so let me know so I can move this to the JQuery forum), Any help would be greatly appreciated as I've not ran into anything like this before.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:54PM

Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month