Contents tagged with ADO.NET

  • 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='javascript:EditItem(" + result.tbItem[i].ItemId + ")'>Edit</a></td>" +

                        "<td><a href='javascript:DeleteItem(" + 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 = "";

            }

  • What's the best type of TSql/CLR Integration in Sql Server 2005

    One of the questions that came up when I was in South Bend is what is the best way to use CLR Objects?  Assuming that your solution needs a CLR Object, should you use a Dataset, Generic List (what I tend to show), or a TSQL/CLR integrated solution.  Honestly, I don't have an answer to that question, right now.  However, I should have something in a couple of days.  I have just completed writing the three scenarios that I have.  I am going to do some testing and I will be posting my findings after I complete my testing.