I got an error : "A circular reference was detected while serializing an object of type 'System.Globalization.Cultureinfo' when returning a dataset variable. It didn't happen when returning string.
It also run smoothly when using PageMethods of Atlas.
What's wrong with the code :
<Microsoft.Web.Script.Services.ScriptService()> _
Public Class EmpService
Inherits System.Web.Services.WebService
Dim cnHimalaya As New SqlConnection( _
ConfigurationManager.ConnectionStrings("HimalayaConnectionString").ConnectionString)
<WebMethod()> _
Public Function GetData(ByVal SearchCategory As String, ByVal NIK As String) As Data.DataSet
Dim strSQL As String
Select Case SearchCategory
Case "Experience"
strSQL = _
"SELECT JobTitle,Company,Period,JobDesc DeptName FROM Ht_HRD_Experience " & _
"WHERE NIK = '" & NIK & "'"
Case "Training"
strSQL = _
"SELECT Training,Category,Year,Host,Location FROM Ht_HRD_Training " & _
"WHERE NIK = '" & NIK & "'"
Case "Education"
strSQL = _
"SELECT Education,School,Subject,YearGraduate FROM Ht_HRD_EmpEducation " & _
"WHERE NIK = '" & NIK & "'"
End Select
Dim sqlDa As New SqlDataAdapter(strSQL, cnHimalaya)
Dim dsData As New Data.DataSet
If cnHimalaya.State = Data.ConnectionState.Closed Then
cnHimalaya.Open()
End If
Try
sqlDa.Fill(dsData)
Return dsData
Catch ex As SqlException
Throw (New ApplicationException(ex.Message))
Finally
cnHimalaya.Close()
dsData.Dispose()
End Try
End Function
The problem is the DataSet is not natively supported by the JavaScriptSerializer.
In order to return a DataSet, you need to implement and register a custom converter for the DataSet in the config file.
There is a DataTable converter that comes with the ASP.NET 2.0 AJAX Futures November CTP that you can try to use.
HTH,
Maíra
This posting is provided "AS IS" with no warranties, and confers no rights.
Instead of the dataset-- why don't you just return Xml?
Eh because using XML is pretty much a pain to use in JavaScript? <s> Wno wants to parse XML on the client side when you could instead get an object back instead?
As to the DataTable converter it's broken in Beta 2 - it's still in Beta 2 but it's returning a bogus object which appears to be an internal object. It is possible to use the converter and grab the JSON it generates but this will likely change in the future
so it's probably not a good idea to use.
Here's an example (Client Code):
function GetCustomerTable_Callback(Result)
{
var List = ListControl.get_element(); // retrieve raw DOM element
// *** This is a HACK workaround AJAX Beta 1 until Microsoft brings back a real
// *** DataTable Converter currently it contains a string property that has be eval'd
// *** String has trailing ( that must be trimmed off
var Text= Result.dataArray.substring(0,Result.dataArray.length -1);
var Table = eval( Text);
// *** Clear the list first
for (x=List.options.length-1; x > -1; x--)
{
List.remove(0);
}
for (x=0; x < Table.length; x++ )
{
var Row = Table[x]; // Mozilla needs to assign
var option = document.createElement("option");
option.text = Row.CompanyName;
option.value = Row.CustomerId;
if ( window.navigator.appName.toLowerCase().indexOf("microsoft") > -1)
List.add(option);
else
List.add(option, null);
}
}
On the server (WebService or PageMethod):
[WebMethod]
public DataTable GetCustomerTable()
{
busCustomer Customer = new busCustomer();
if (Customer.GetCustomerList() < 0)
return null;
DataTable dt = Customer.DataSet.Tables["TCustomerList"];
return dt;
}
You also need to register the DataTableConverter:
<microsoft.web>
<scripting>
<webServices>
<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
<jsonSerialization maxJsonLength="50000">
<converters>
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter"/>
</converters>
</jsonSerialization>
</webServices>
</scripting>
</microsoft.web>
I just downloaded AJAX v1.0 and now I get the circular reference error. I had the following code but I don't know what I have to change it to for my code to work properly.
You usually get this error when you try to serialize a DataTable that has no custom converter registered for it.
The Ajax v1.0 does not contain the converters for the DataTable, they are part of the ASP.NET AJAX Futures January CTP. You need to install this and add a reference to in in your project.
Have you done this?
Maíra
This posting is provided "AS IS" with no warranties, and confers no rights.
JRLiem
Member
168 Points
46 Posts
Call Web Service : can not return dataset
Nov 15, 2006 04:04 AM|LINK
Hi,
I got an error : "A circular reference was detected while serializing an object of type 'System.Globalization.Cultureinfo' when returning a dataset variable. It didn't happen when returning string.
It also run smoothly when using PageMethods of Atlas.
What's wrong with the code :
<Microsoft.Web.Script.Services.ScriptService()> _ Public Class EmpService Inherits System.Web.Services.WebService Dim cnHimalaya As New SqlConnection( _ ConfigurationManager.ConnectionStrings("HimalayaConnectionString").ConnectionString) <WebMethod()> _ Public Function GetData(ByVal SearchCategory As String, ByVal NIK As String) As Data.DataSet Dim strSQL As String Select Case SearchCategory Case "Experience" strSQL = _ "SELECT JobTitle,Company,Period,JobDesc DeptName FROM Ht_HRD_Experience " & _ "WHERE NIK = '" & NIK & "'" Case "Training" strSQL = _ "SELECT Training,Category,Year,Host,Location FROM Ht_HRD_Training " & _ "WHERE NIK = '" & NIK & "'" Case "Education" strSQL = _ "SELECT Education,School,Subject,YearGraduate FROM Ht_HRD_EmpEducation " & _ "WHERE NIK = '" & NIK & "'" End Select Dim sqlDa As New SqlDataAdapter(strSQL, cnHimalaya) Dim dsData As New Data.DataSet If cnHimalaya.State = Data.ConnectionState.Closed Then cnHimalaya.Open() End If Try sqlDa.Fill(dsData) Return dsData Catch ex As SqlException Throw (New ApplicationException(ex.Message)) Finally cnHimalaya.Close() dsData.Dispose() End Try End Functionthanks.
maira.wenzel
Member
511 Points
111 Posts
Microsoft
Re: Call Web Service : can not return dataset
Nov 15, 2006 10:35 PM|LINK
The problem is the DataSet is not natively supported by the JavaScriptSerializer.
In order to return a DataSet, you need to implement and register a custom converter for the DataSet in the config file.
There is a DataTable converter that comes with the ASP.NET 2.0 AJAX Futures November CTP that you can try to use.
HTH,
Maíra
JRLiem
Member
168 Points
46 Posts
Re: Call Web Service : can not return dataset
Nov 16, 2006 12:38 AM|LINK
Thanks.
Do you have article that expalin more detail with example how to do that?
Rgds
daniellarson
Member
49 Points
9 Posts
Re: Call Web Service : can not return dataset
Nov 16, 2006 04:44 AM|LINK
atlas webservice
JRLiem
Member
168 Points
46 Posts
Re: Call Web Service : can not return dataset
Nov 17, 2006 12:45 AM|LINK
That's good idea. I'll use it for the next project because there is a lot of code i have to change for the current project.
thanks anyway.
rstrahl
Contributor
2233 Points
375 Posts
ASPInsiders
MVP
Re: Call Web Service : can not return dataset
Nov 20, 2006 10:11 PM|LINK
Instead of the dataset-- why don't you just return Xml?
Eh because using XML is pretty much a pain to use in JavaScript? <s> Wno wants to parse XML on the client side when you could instead get an object back instead?
As to the DataTable converter it's broken in Beta 2 - it's still in Beta 2 but it's returning a bogus object which appears to be an internal object. It is possible to use the converter and grab the JSON it generates but this will likely change in the future so it's probably not a good idea to use.
Here's an example (Client Code):
function GetCustomerTable_Callback(Result) { var List = ListControl.get_element(); // retrieve raw DOM element // *** This is a HACK workaround AJAX Beta 1 until Microsoft brings back a real // *** DataTable Converter currently it contains a string property that has be eval'd // *** String has trailing ( that must be trimmed off var Text= Result.dataArray.substring(0,Result.dataArray.length -1); var Table = eval( Text); // *** Clear the list first for (x=List.options.length-1; x > -1; x--) { List.remove(0); } for (x=0; x < Table.length; x++ ) { var Row = Table[x]; // Mozilla needs to assign var option = document.createElement("option"); option.text = Row.CompanyName; option.value = Row.CustomerId; if ( window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) List.add(option); else List.add(option, null); } }On the server (WebService or PageMethod):
[WebMethod] public DataTable GetCustomerTable() { busCustomer Customer = new busCustomer(); if (Customer.GetCustomerList() < 0) return null; DataTable dt = Customer.DataSet.Tables["TCustomerList"]; return dt; }You also need to register the DataTableConverter:
West Wind Technologies
Making waves on the Web
www.west-wind.com/weblog
dhoenig
Member
420 Points
131 Posts
Re: Call Web Service : can not return dataset
Jan 25, 2007 02:45 PM|LINK
I just downloaded AJAX v1.0 and now I get the circular reference error. I had the following code but I don't know what I have to change it to for my code to work properly.
Anyone have any ideas? Thanks.
dhoenig
Member
420 Points
131 Posts
Re: Call Web Service : can not return dataset
Jan 25, 2007 04:50 PM|LINK
maira.wenzel
Member
511 Points
111 Posts
Microsoft
Re: Call Web Service : can not return dataset
Jan 25, 2007 04:53 PM|LINK
You usually get this error when you try to serialize a DataTable that has no custom converter registered for it.
The Ajax v1.0 does not contain the converters for the DataTable, they are part of the ASP.NET AJAX Futures January CTP. You need to install this and add a reference to in in your project.
Have you done this?
Maíra
laguy
Member
22 Points
10 Posts
Re: Call Web Service : can not return dataset
Feb 25, 2007 11:38 PM|LINK
Hi folks,
I'm getting the same problem and am "stuck", so I appreciate some help !
I installed the ASP.NET AJAX Futures January CTP msi file.
I added a reference to my \BIN
C:\Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Futures January CTP\v1.0.61025\Microsoft.Web.Preview.dll
I put this in the web.config:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000">
<converters>
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter"/>
</converters>
</jsonSerialization>
</webServices>
But still no go...
Do I have to MERGE the web_CTP.config with my standard AJAX 1.0 web.config or replace it entirely ?
It seems fairly complex to do so. Is their a pre-existing AJAX 1.0 / Jan CTP combined web.config available ?
Thanks, LA Guy
AJAX Jan CTP Dataset serialization