AJAX Futures December CTP: Returning DataSets, DataTables, and DataRows from a WebService or PageMethod

In the previous two CTPs, you simply could not do this (if you tried, you likely received some sort of circular reference serialization error), even though way back in July, pre-beta, you could.

Well, its back. But to take advantage of this, you will need to add the following to your web.config:

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization>
          <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>
      </webServices>
    </scripting>
  </system.web.extensions>

... and hence forth you shall be able to call webservices or page methods that return System.Data.DataSet/DataTable/DataRow! And there was much rejoicing.

One thing you should know. Way back pre-beta, the client side type you received as a result of calling one of these ado.net loving methods was Sys.Data.DataTable filled with Sys.Data.DataRow and Sys.Data.DataColumn rich client side objects. No longer. Converters now are restricted to returning proper JSON, which cannot and should not include type information. Just data. So the JSON you get is what you would expect for a DataTable. You might see this as negative thing... but in reality it makes much more sense than the previous model. The format is much more compact now. And since all you get back is data, you aren't forced into using the built in client side types. You can more easily roll your own. Also, since you can simply work directly with the data, no client side parsing is needed and no extra types are created.

Plus... If you really need the old type back, you can still get it. Just pass the JSON object into the following method:

var table = Sys.Preview.Data.DataTable.parseFromJson(obj);

And you can once again work with it with the rich client side type. Most likely you should be able to use the json object directly, that would be more performant anyway. If you need to provide the data to one of the built in data controls (Sys.Preview.UI.Data.*), they will automatically convert the object if needed.

If you wish to work with the JSON directly, the following is the format the DataTable will take:

{ columns: [ column1, column2, .... ], rows: [ row1, row2, ...] }

A column in itself will look like:

{ name: "TableName", dataType: ClientSideType(e.g., String),
  defaultValue: jsonSerializedValue, readOnly: true|false, isKey: true|false }

Possible data types are: String, Number, Boolean, and Date. isKey is true if the column is or is part of the column's primary key. The rest are pretty obvious.

A row in itself will look like:

{ column1: value1, column2: value2, .... }

Where "column1" is the name of the first column of the data table.

So for example, given a Table's JSON, you can access the 2nd row's BirthDate value like so:

var bday = dataTable.rows[1].BirthDate;
The ability to return ADO.NET objects from WebServices and consume them on the client was sorely missed. Hopefully its return is a happy one!

Happy coding!

9 Comments

  • I&#39;m glad to see this is back, although it&#39;s a bummer it&#39;s in the CTP and not in the core libray...

  • i am trying to return a datatable using pagemethods, i got an error

    System.StackOverflowException was unhandled

  • kris -- Can I see the entire exception with the stack trace? Did you add the necessary web.config entries? What is the schema of the datatable you are returning?

  • re:
    i am trying to return a datatable using pagemethods, i got an error

    System.StackOverflowException was unhandled


    -----------------------------------------------------------------------------------response:::
    check ur web.config file.
    ***************
    check if the maxlength attribute of is set.If yes, remove it

  • Do you have a sample that shows how to call a webservice that returns a datatable..I can't seem to get it to work for me, ither I get javascript errors or something else.

    Thanks

  • I'm trying to serialize a datatable to json in a pagemethod and after reading this and a few other forums, still receive the circular reference error.

    I have the converters in web.config; no maxlength set; extensions and jan ctp installed in gac.

    Not sure where to go from here.

  • The Preview dll is not GAC'd, unless you put it there. I suggest you put it in your BIN directory. Also by the way, Jan CTP is pretty out dated by now -- you should update to the latest which is July I believe. I assume you're on ajax RTM already?

    If the gac/bin thing doesn't help email me at blog[at]infinity88[dot]com with some code and we'll figure it out.

  • I am looking for complete examples that show filtering, sorting, etc (use of animation wouldn't hurt either) all on the client without returning to the server after a web service or page method call. This would seem to be the advantage that could set this ajax framework apart and the reason for having ado on the client, but can't seem to find them (examples) where data tables and the like are involved. Am I the long ranger in thinking to this sort of thing?

    Dev XPress, RAD controls and others seem to be doing similar but going to the server to do it even if only a few records are involved. It would be great to present an alternative using our very own framework.

  • Is it still necessary to use the futures CTP to get the DataSetConverter for VS2008? Will it be merged into the VS2008 SP1 update?

Comments have been disabled for this content.