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 ServiceCODE
[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 MethodCODE
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
bigQUOTE
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.