ManuelAbadia.com
ExtendedObjectDataSource samples
Samples



Product Information

Custom Objects with Optimistic Concurrency

In this sample we use a CompatObjectDataSource to show, edit and update data using a GridView and to insert data using a DetailsView. Note that we are using custom objects as the parameters to the CompatObjectDataSource.

In this sample we have enabled optimistic concurrency in the CompatObjectDataSource (setting the ConflictDetection property to "CompareAllValues") so if you open the same page in two instances of the browser, put the same row in edit mode, and try to update the same product you will notice that the first one does it (it shows affected rows = 1) and the second one does not update it (affected rows = 0).

 IdNameDescriptionPrice
Edit Delete1Product 1Full description for product number 12
Edit Delete2Product 2Full description for product number 24
Edit Delete3Product 3Full description for product number 36
Edit Delete4Product 4Full description for product number 48
123456789

Name
Description
Price
Insert Cancel

Sample source code

The ASPX file for this sample is:


<%@ Page Language="C#" MasterPageFile="~/DefaultMaster.master" AutoEventWireup="true" CodeFile="CustomObjectOC.aspx.cs" Inherits="CustomObjectOC" Title="Untitled Page" %>

 

<%@ Register Assembly="ExtendedObjectDataSource" Namespace="Manu.Web.UI.WebControls" TagPrefix="manu" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="Content" Runat="Server">

    <h1>Custom Objects with optimistic concurrency</h1>

 

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"

    AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
    AutoGenerateEditButton
="True"

    CssClass="gridview" BackColor="#335789" BorderColor="#133769" BorderStyle="Solid"
    BorderWidth
="1px"

    DataKeyNames="Id" DataSourceID="ObjectDataSource1" PageSize="4">

        <Columns>

            <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" 
            SortExpression
="Id" />

            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

            <asp:BoundField DataField="Description" HeaderText="Description" 
            SortExpression
="Description" />

            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />

        </Columns>

        <RowStyle BackColor="White" ForeColor="#436799" />

        <HeaderStyle ForeColor="White" />

        <PagerStyle ForeColor="White" />

    </asp:GridView>

    <manu:CompatObjectDataSource ID="ObjectDataSource1" runat="server"
        DataObjectTypeName
="Product"

        DeleteMethod="DeleteOC" InsertMethod="Add" OldValuesParameterFormatString="old_
        {0}"
ConflictDetection="CompareAllValues"

        SelectMethod="LoadAll" TypeName="ProductsDAL" UpdateMethod="Edit"

        OnDeleted="ObjectDataSource1_Deleted" OnInserted="ObjectDataSource1_Inserted"
        OnSelected
="ObjectDataSource1_Selected" OnUpdated="ObjectDataSource1_Updated"
        MaximumRowsParameterName
="maxRows" StartRowIndexParameterName="startIndex"
        EnablePaging
="true" SelectCountMethod="CountAll" SortParameterName="sortedBy">

    </manu:CompatObjectDataSource>

    <br />

    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
        DataKeyNames
="Id"

        DataSourceID="ObjectDataSource1" DefaultMode="Insert" Height="50px" Width="125px"
        CssClass
="htmltable">

        <Fields>

            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

            <asp:BoundField DataField="Description" HeaderText="Description"
            SortExpression
="Description" />

            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />

            <asp:CommandField ShowInsertButton="True" />

        </Fields>

        <CommandRowStyle ForeColor="White" />

    </asp:DetailsView>

    <br />

    <asp:Label ID="lbResult" runat="server" EnableViewState="False"></asp:Label>

</asp:Content>


The code-behind file (ASPX.CS) is:


using System;

using System.Web.UI.WebControls;

 

public partial class CustomObjectOC : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

 

    protected void ObjectDataSource1_Selected(object s, ObjectDataSourceStatusEventArgs e)

    {

    }

 

    protected void ObjectDataSource1_Inserted(object s, ObjectDataSourceStatusEventArgs e)

    {

        lbResult.Text = String.Format("Affected Rows = {0}. Generated Id = {1}",

                                                                             e.AffectedRows, (int)e.ReturnValue);

    }

 

    protected void ObjectDataSource1_Updated(object s, ObjectDataSourceStatusEventArgs e)

    {

        lbResult.Text = "Affected Rows = " + e.AffectedRows;

    }

 

    protected void ObjectDataSource1_Deleted(object s, ObjectDataSourceStatusEventArgs e)

    {

        lbResult.Text = "Affected Rows = " + e.AffectedRows;

    }

}