DCSIMG
Building ADO.NET Data Services Ajax Queries Using a QueryBuilder - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2011 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Building ADO.NET Data Services Ajax Queries Using a QueryBuilder

Building ADO.NET Data Services Ajax Queries Using a QueryBuilder

In today’s post I’m Building ADO.NET Data Services Ajax Queries Using a QueryBuilder
going to explain how
to use the QueryBuilder
object of the ASP.NET Ajax
client library
for building
queries to an ADO.NET data
service
.

The QueryBuilder Object

In the ASP.NET Ajax client library there is a new namespace – Sys.Data. This
namespace hosts the main client side objects to interact with data services.
You can recall that I wrote about the ASP.NET Ajax client library in a previous
post
.
The QueryBuilder object is a part of the Sys.Data namespace.
The QueryBuilder provides methods and properties for manually creating a
data service query and enables us to build the queries through an API.
It can simplify the building of queries from the client side a lot because you
don’t need to remember all the syntax to create the query.
The following code is an example of how to create a QueryBuilder with a
Course Uri which is the resource we want to query:

var queryBuilder = new Sys.Data.QueryBuilder("Course");  

In order to get to know the QueryBuilder’s API you can go to the following link.

QueryBuilder Example

The following example is a simple example to show how to work with the
QueryBuilder object in order to create a credits filter and an order by clause on a
course resource. The example is based on the ConsumeDataService example that 
I wrote in the session I delivered lately. You can download the session’s
examples from here.

<%@ Page Title="" Language="C#" MasterPageFile="~/Master/AjaxClient.Master" AutoEventWireup="true" CodeBehind="QueryBuilder.aspx.cs" Inherits="DataServiceAjaxClient.QueryBuilder.QueryBuilder" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <label>Insert Credits to filter the results:</label><br />
    <label>Credits:</label><input id="txtCredits" type="text" /><br/>
 
    <input id="ButtonQuery" type="button" value="Perform Courses Query" onclick="doQuery()" /><br />
    <br />
    <span id="spanResults"></span>
 
    <script type="text/javascript">
        function doQuery() {
            var queryBuilder = new Sys.Data.QueryBuilder("Course");           
            
            // set the filters if neccesery            
            queryBuilder.set_filter(buildFilter("txtCredits", "Credits eq "));
            
            // set the order by the title
            queryBuilder.set_orderby("Title");
            
            // perform the query itself
            proxy.query(queryBuilder.toString(), onSuccess, onFailure);                                  
        }
        
        function buildFilter(txtName, query) {
            debugger;
            var txt = $get(txtName);
            if (txt.value != null && txt.value != '')
            {
                return query + txt.value;
            }
            return '';
        }
 
        function onFailure(error, context, operation) {
            $get("spanResults").innerHTML =
               BuildErrorString(error, operation);
        }
 
        function BuildErrorString(error, operation) {
            var sb = new Sys.StringBuilder("Error occurred while performing operation ");
            sb.append(operation);
            sb.append("!");
            return sb.toString();
        }
 
        function onSuccess(result, context, operation) {
            var spanResults = $get("spanResults");
            spanResults.innerHTML = '';
      
            // Get Customers list and render by using an HTML table.
            var sb = new Sys.StringBuilder();
            sb.append("<table cellpadding='1' cellspacing='1' border='1' bordercolor='green'>");
 
            var firstRowOutput = false;
            for (index in result) {
                var course = result[index];
 
                // If this is first row write the table header
                if (!firstRowOutput) {
                    sb.append("<tr>");
                    for (key in course) {
                        if (key != "__metadata") {
                            sb.append("<th>");
                            sb.append(key);
                            sb.append("</th>");
                        }
                    }
                    sb.append("</tr>");
                    firstRowOutput = true;
                }
 
                // Display the data.
                sb.append("<tr>");
                for (key in course) {
                    if (key != "__metadata") {
                        sb.append("<td>");
                        sb.append(course[key]);
                        sb.append("</td>");
                    }
                }
                sb.append("</tr>");
            }
 
            sb.append("</table>");
            spanResults.innerHTML = sb.toString();
        }
    </script>
</asp:Content>

In the example, you can fill a credits textbox to filter the returned result set
by credits and also I order the result set by the Title property. The only
interesting part of the example is the doQuery function which creates a
QueryBuilder object, sets it’s filter to the credits in the textbox and sets  
the order by property. After the settings of the QueryBuilder properties we
can extract the query through the toString function of the object and use the
query function of the DataService object (the proxy object) with that query.

QueryBuilder Futures

As a part of ASP.NET 4.0 all the Sys.Data namespace is being refactored.
The QueryBuilder is going to be called AdoNetQueryBuilder and to deliver
almost the same functionality like I showed in this post.

Summary

Lets sum up, in the post I explained what is the Sys.Data.QueryBuilder and
I showed how to use it. That object is very useful in order to
create queries without the need to remember how to build the query string
for the data service in order to make a data service query.

DotNetKicks Image

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# January 3, 2009 10:31 AM