DataSet Examples

The first example will read an XML file form the hard disk and return it as an DataSet object. The XML looks like this:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="de-DE">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="uid" type="xs:short" minOccurs="0" />
                <xs:element name="name" type="xs:string" minOccurs="0" />
                <xs:element name="type" type="xs:string" minOccurs="0" />
                <xs:element name="refdate" type="xs:dateTime" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Table>
    <uid>1</uid>
    <name>sysobjects</name>
    <type>S </type>
    <refdate>2002-12-17T14:36:10.0430000+01:00</refdate>
  </Table>
  <Table>
    <uid>1</uid>
    <name>sysindexes</name>
    <type>S </type>
    <refdate>2002-12-17T14:36:10.0430000+01:00</refdate>
  </Table>
  <Table>
    <uid>1</uid>
    <name>syscolumns</name>
    <type>S </type>
    <refdate>2002-12-17T14:36:10.0430000+01:00</refdate>
  </Table>
  <Table>
    <uid>1</uid>
    <name>systypes</name>
    <type>S </type>
    <refdate>2002-12-17T14:36:10.0430000+01:00</refdate>
  </Table>
  <Table>
    <uid>1</uid>
    <name>syscomments</name>
    <type>S </type>
    <refdate>2002-12-17T14:36:10.0430000+01:00</refdate>
  </Table>
</NewDataSet>

On the server-side .NET code I use this C# code to read the file and return it as an DataSet object:

[AjaxMethod]
public DataSet GetDataSet()
{
  DataSet ds = new DataSet();

  ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/data/dataset.xml"),
               XmlReadMode.ReadSchema);

  return ds;
}

On the client-side JavaScript code you get a new Ajax.Web.DataSet object. Using this object you can get the columns and rows for each table inside the dataset. Also you have several methods like .addColumn or .addRow.

function doTest1() {
  AJAXDemo.Examples.DataSets.Demo.GetDataSet(_callback1);
}

function _callback1(res) {
  var cols = res.value.Tables[0].Columns.length;
  var rows = res.value.Tables[0].Rows.length;
	
  alert(cols + " cols and " + rows + " rows");
}

Click here to get the count of columns and rows for the XML file. You can use the DataAdapter to fill a DataSet from a SQL Server or any other .NET database.

You can use any DataSet object you got from an Ajax.NET method to use it as an argument again. Following JavaScript code will get the DataSet from the first example and use it as an argument:

function doTest3() {
  var ds = AJAXDemo.Examples.DataSets.Demo.GetDataSet().value;	// sync invoke of GetDataSet
  AJAXDemo.Examples.DataSets.Demo.GetXmlFromDataSet(ds, _callback2);
}

Click here to get the XML source of the DataSet as a string.

Creating a DataSet on the client-side JavaScript

With the new version you can create a DataSet directly on the client-side JavaScript code. You may use this objects to save cart items or if you have other items that can be saved in tables. See following JavaScript code that will create a DataSet:

var ds = new Ajax.Web.DataSet();
var dt = new Ajax.Web.DataTable();
	
dt.addColumn("FirstName", "System.String");
dt.addColumn("Age", "System.Int32");
	
dt.addRow({"FirstName":"Michael","Age":28});
dt.addRow({"FirstName":"Tanja","Age":25});
	
ds.addTable(dt);

In this short example I configure two columns, the first one will hold a string, and the second column Age will use a integer. Now, we can add rows to the DataTable dt. There are two ways to add a new row:

dt.addRow({"FirstName":"Michael","Age":28});

// or using an object

var row = new Object();

row.FirstName = "Michael";
row.Age = 28;

dt.addRow(row);

Click here to create such a DataSet using JavaScript, send it to a Ajax.NET method and get the XML string back of the DataSet.

You can now save any data on the server that you may collect on the client.

Valid XHTML 1.0 Transitional