ASP.NET Podcast Show #114 - ADO.NET Data Services in .NET 3.5 Service Pack 1 Beta1 with ASP.NET AJAX - More Wally - Wallace B. McClure
in

MoreWally.com

Giving people what they want, More Wally. This is the technical and personal blog site of
Wallace B. (Wally) McClure.

This Blog

Syndication

Technical Sites

More Wally - Wallace B. McClure

This blog will have all kinds of posts about Wally McClure. In it, there will be tons of .NET and computer programming posts as well as Wally's views on life in general. As you might guess, this site and blog help you get More Wally in your life. What more could anyone want? .NET, ADO.NET, ASP.NET, AJAX, Atlas, Microsoft AJAX Library, ASP.NET 2.0 AJAX Extensions, ............

ASP.NET Podcast Show #114 - ADO.NET Data Services in .NET 3.5 Service Pack 1 Beta1 with ASP.NET AJAX

Subscribe to everything.

Original Url: http://aspnetpodcast.com/CS11/blogs/asp.net_podcast/archive/2008/05/31/asp-net-podcast-show-114-ado-net-data-services-in-net-3-5-service-pack-1-beta1-with-asp-net-ajax.aspx 

Subscribe to WMV.

Subscribe to M4V for iPod.

Subscribe to MP3.

Download WMV.

Download W4V for iPod.

Download MP3.

Show Notes:

  • ADO.NET Data Services Viewing DataSql Server Database .
  • ADO.NET Data Services.
  • Javascript.
  • No SQL.
  • Getting data.
  • Insert a record.
  • Update a record.
  • Metadata.
  • I had a snafu while recording, so some audio may sound clipped.

Source Code:

ADO.NET Data Services Editing Data<%@ ServiceHost Language="C#" Factory="System.Data.Services.DataServiceHostFactory" Service="OrderItemDataService" %>

Codebehind file:

using System;

using System.Data.Services;

using System.Collections.Generic;

using System.Linq;

using System.ServiceModel.Web;

 

public class OrderItemDataService : DataService< ADONETDataServicesModel.ADONETDataServicesEntities >

{

    // This method is called only once to initialize service-wide policies.

    public static void InitializeService(IDataServiceConfiguration config)

    {

        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

        // For testing purposes use "*" to indicate all entity sets/service operations.

        // "*" should NOT be used in production systems.

        // Example for entity sets (this example uses "AllRead" which allows reads but not writes)

        // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);

        config.SetEntitySetAccessRule("*", EntitySetRights.All);

        config.UseVerboseErrors = true;

        //config.SetEntitySetAccessRule("tbOrder", EntitySetRights.All);

        // Example for service operations

        // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

  }

 

    // Query interceptors, change interceptors and service operations go here

}

Display Data:

<script language="javascript" type="text/javascript">

function pageLoad()

{

    var strOut;

    var Service = GetDataService();

 

    var QueryString = "/tbOrder";

    Service.query(QueryString, OnCompleteSuccess, OnCompleteFailure);

}

function GetDataService()

{

    return(new Sys.Data.DataService("WebDataService.svc"));

}

function OnCompleteSuccess(result) {

    var strReturn = "<br />";

    var Output = "On Complete Success Called." + strReturn;

    var i = 0;

    for (m in result)

    {

        Output += m + strReturn;

    }

 

    for (m in result[0]) {

        Output += m + strReturn;

    }

 

    Output += "<table>";

    Output += "<tr><th>Total Amount</th>" +

              "<th>Date of Order</th>" +

              "<th>Comment</th>" +

              "<th>Customer Name</th>" +

              "<th>Edit</th>" +

              "</tr>";

    for (i=0; i< result.length; i++)

    {

        Output += "<tr>" +

            "<td>" + result[i].TotalAmount + "</td>" +

            "<td>" + result[i].DateOrdered + "</td>" +

            "<td>" + result[i].Comment + "</td>" +

            "<td>" + result[i].CustomerName + "</td>" +

            "<td><a href='EditOrder.aspx?OrderID=" + result[i].OrderId + "'>Edit</td>" +

            "</tr>";

    }

    Output += "</table>";

    $get("OutputDiv").innerHTML = Output;

}

 

function OnCompleteFailure(result)

{

    alert("On Complete Failure Called.");

}

Add/Edit/Delete file:

        var OrderID=<%=Request.QueryString["OrderID"] %>;

       

        function pageLoad() {

            FillOrderData();

        }

        function FillOrderData()

        {

            var Service = GetDataService();

            var QueryString = "tbOrder(" + OrderID + ")?$expand=tbItem";

            Service.query(QueryString, OnCompleteSuccess, OnCompleteFailure);

        }

        function GetDataService()

        {

            return(new Sys.Data.DataService("OrderItemDataService.svc"));

        }

        function OnCompleteSuccess(result)

        {

            var i = 0;

            var Output = "<table><tr><th>Part</th><th>Price</th>" +

                "<th>Edit</th><th>Delete</th></tr>";

            for(i = 0; i < result.tbItem.length; i++)

            {

                Output += "<tr><td>" + result.tbItem[i].Part +

                    "</td><td>" + result.tbItem[i].Price + "</td>" +

                    "<td><a href='BLOCKED SCRIPTEditItem(" + result.tbItem[i].ItemId + ")'>Edit</a></td>" +

                    "<td><a href='BLOCKED SCRIPTDeleteItem(" + result.tbItem[i].ItemId + ")'>Delete</a></td>" +

                    "</tr>";

            }

            Output += "</table>";

            $get("OutputGrid").innerHTML = Output;

        }

       

        function OnCompleteFailure(result)

        {

            alert("OnCompleteFailure called.");

        }

       

        function DeleteItem( ItemId )

        {

            var Service = GetDataService();

           

            if ( window.confirm("Do you really want to delete this Item?" ) )

            {

                alert("Delete it.");

                Service.remove(null, "tbItem(" + ItemId + ")",

                    DeleteItemSucceed, DeleteItemFailure);

            }

        }

       

        function DeleteItemSucceed( result )

        {

            FillOrderData();

            alert("DeleteItemSucceed called.");

        }

       

        function DeleteItemFailure( result )

        {

            alert("DeleteItemFailure called.");

        }

       

        function EditItem( ItemId )

        {

            var Service = GetDataService();

            var QueryString = "tbItem(" + ItemId + ")";

            Service.query(QueryString, EditItemSucceed, EditItemFailure);

        }

       

        function EditItemSucceed( result )

        {

            $get("txtPrice").value = result.Price;

            $get("txtPart").value = result.Part;

            $get("hdItemId").value = result.ItemId;

            $get("OutputSpecifics").style.visibility = "visible";

            $get("OutputGrid").style.visibility = "hidden";

            FillOrderData();

        }

       

        function EditItemFailure( result )

        {

            alert("EditItemFailure called.");

        }

       

        function SaveItem()

        {

            var ThisItemId = $get("hdItemId").value;

            var Service = GetDataService();

           

            if (ThisItemId == "" )

            {

                var newProduct =

                {

                    Price:      $get("txtPrice").value,

                    Part:       $get("txtPart").value,

                    tbOrder:    { __metadata: {uri: "/tbOrder(" + OrderID + ")" }}

                };

                Service.insert(newProduct, "tbItem", InsertItemSucceed, InsertItemFailed);

            }

            else{

                var updateProduct=

                {

                    ItemId:     $get("hdItemId").value,

                    Price:      $get("txtPrice").value,

                    Part:       $get("txtPart").value,

                    tbOrder:    { __metadata: {uri: "/tbOrder(" + OrderID + ")" }}

                    //$get("hdOrderId").value

                }

                Service.update(updateProduct, "tbItem(" + $get("hdItemId").value + ")", UpdateItemSucceed, UpdateItemFailed, updateProduct);

            }

        }

        function InsertItemSucceed(result)

        {

            alert("InsertItemSucced called.");

            FillOrderData();

        }

        function InsertItemFailed(result)

        {

            alert("InsertItemFailed called.");

        }

        function UpdateItemSucceed(result)

        {

            alert("UpdateItemSucceed called.");

        }

        function UpdateItemFailed(result)

        {

            alert("UpdateItemFailed called.");

        }

        function BeginAdd()

        {

            $get("OutputGrid").style.visibility = "hidden";

            $get("OutputSpecifics").style.visibility = "visible";

            $get("txtPrice").value = "";

            $get("txtPart").value = "";

            $get("hdItemId").value = "";

        }

Comments

No Comments

About wallym

Wallace B. (Wally) McClure INETA Speaker's Bureau Microsoft MVP ASPInsider Co-author of "Beginning AJAX with ASP.NET" Co-author of "Professional ADO.NET Programming" Co-author of "Building Highly Scalable Database Applications with .NET" Master's Degree in Electrical Engineering from Georgia Tech Bachelor's Degree in Electrical Engineering from Georgia Tech
2006 - Wallace B. McClure
Powered by Community Server (Non-Commercial Edition), by Telligent Systems