Monday, March 26, 2012

Call Web Service : can not return dataset

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 EmpServiceInherits System.Web.Services.WebServiceDim cnHimalayaAs New SqlConnection( _ ConfigurationManager.ConnectionStrings("HimalayaConnectionString").ConnectionString) <WebMethod()> _Public Function GetData(ByVal SearchCategoryAs String,ByVal NIKAs String)As Data.DataSetDim strSQLAs String Select Case SearchCategoryCase"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 sqlDaAs New SqlDataAdapter(strSQL, cnHimalaya)Dim dsDataAs New Data.DataSetIf cnHimalaya.State = Data.ConnectionState.ClosedThen cnHimalaya.Open()End If Try sqlDa.Fill(dsData)Return dsDataCatch exAs SqlExceptionThrow (New ApplicationException(ex.Message))Finally cnHimalaya.Close() dsData.Dispose()End Try End Function

thanks.

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


Thanks.

Do you have article that expalin more detail with example how to do that?

Rgds


Instead of the dataset-- why don't you just return Xml?

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.


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 firstfor (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>
 
+++ Rick -- 


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.

<jsonSerialization maxJsonLength="50000"><converters><add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter"/></converters></jsonSerialization>

Anyone have any ideas? Thanks.


Sorry, this does work. I forgot to download the January Futures CTP.

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


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


Hi again,

I missed these settings but still no go.

<jsonSerialization maxJsonLength="50000">
<converters>
<add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview"/>
<add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview"/>
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview"/>
</converters>
</jsonSerialization>

Thanks, LA Guy


I'm getting the same problem and am "stuck", thk


Guys I'm getting it working fine with the January CTP with the

<system.web.extensions>
<scripting>
<webServices>
<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
<jsonSerialization>
<!--<jsonSerialization maxJsonLength="500">-->
<converters>
<add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</converters>
</jsonSerialization>

Config setting and the reference added to the Preview dll.

Can we assume these implementations would be included in the future MS Ajax releases bcoz I'm going to use these JSON converters for a real product.


I am running the latest AJAX 1.0 product set. When I apply the changes as mentiond above, I no longer am able to access my webmethod. I get a javascript error that states that method is undefined? when I comment the section above, I get circular reference encountred (since I am trying to return a datatable)

What am I doing wrong?

No comments:

Post a Comment