Using ASP.NET AJAX to Access Data Services
Introduction
You can interact with an ADO.NET Data Service from any application that can send an HTTP request to an ADO.NET Data Service URI, and that can process the response in the format that is returned from the data service. The ASP.NET Extensions Preview release provides the Sys.Data.DataService client script class for simplifying the interaction between an ASP.NET AJAX application and an ADO.NET Data Service. By using this class, you can create Web applications that interact with data through a data service on the Web site and that can update the Web page without a full postback to the Web server.
This topic contains information about how to create an instance of the DataService script class in an AJAX application, and how to use the script class to query or modify data through the data service.
Connecting to a Data Service using AJAX
You use the DataService script class to execute data operations of a data service. When you create an instance of the DataService class, you provide the relative URI of the data service that contains the data that you want to retrieve, as shown in the following example:
var exampleService =
new Sys.Data.DataService("/northwind.svc");
Querying Data
After you create an instance of the DataService class, you can call the query method to retrieve data from the data service. The following example shows how to return all the records in the Customers table.
exampleService.query("/Customers", cbSuccess, cbFailure);
The DataService class creates a URI for querying the data service by combining the value that you set in the class constructor with the value for the query parameter in the query method. The previous example combined with the class constructor example creates the URI with the following value:
/northwind.svc/Customers
The call to the query method references cbSuccess as the succeeded callback function, and cbFailure as the failed callback function. You add these callback functions to evaluate the results of the query.
You can also use the query method to specify conditions. The following example shows how to request the set of sales orders with the shipped date of 1998 or later for the customer who is represented by the key "ALFKI":
exampleService.query("/Customers('ALFKI')/Orders?$filter=ShippedDate ge '1998-01-01'", cbSuccess, cbFailure);
Modifying Data
The DataService class provides the following methods that you can call in order to modify data through a data service:
When you call the insert, update, or remove methods, the operation request is sent immediately to the data service. You use the createActionSequence method when you want to execute multiple data modification actions as a batch. The createActionSequence method returns an instance of the Sys.Data.ActionSequence class. You add data modification steps to the ActionSequence object and execute those steps by calling the executeActions method of the ActionSequence object.
The following example shows an insert operation that adds a new record in the Categories table with the value of "Decorations" for the CategoryName field.
var newCategory = {"CategoryName":"Decorations"};
exampleService.insert(newCategory,"/Categories");
The newCategory variable represents the data as a JavaScript object.
This topic is ASP.NET Extensions documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.