locked
Call Web Service : can not return dataset RRS feed

  • Question

  • User1515038421 posted

    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 Function
     

    thanks.

    Wednesday, November 15, 2006 12:04 AM

All replies

  • User933528419 posted

    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

    Wednesday, November 15, 2006 6:35 PM
  • User1515038421 posted

    Thanks.

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

    Rgds

    Wednesday, November 15, 2006 8:38 PM
  • User2073648832 posted
    Instead of the dataset-- why don't you just return Xml?
    Thursday, November 16, 2006 12:44 AM
  • User1515038421 posted

    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.

    Thursday, November 16, 2006 8:45 PM
  • User1622957740 posted

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

     

    Monday, November 20, 2006 6:11 PM
  • User733978539 posted

    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. 

    Thursday, January 25, 2007 10:45 AM
  • User733978539 posted
    Sorry, this does work. I forgot to download the January Futures CTP.
    Thursday, January 25, 2007 12:50 PM
  • User933528419 posted

    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

    Thursday, January 25, 2007 12:53 PM
  • User343140345 posted

    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

     

     

     

     

    Sunday, February 25, 2007 7:38 PM
  • User343140345 posted

    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 

     

    Monday, February 26, 2007 2:36 AM
  • User1371816079 posted

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

    Friday, May 25, 2007 2:05 PM
  • User994604737 posted

    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. 

    Monday, June 4, 2007 9:25 AM
  • User1445721 posted

    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?

    Friday, July 6, 2007 5:51 PM