ManuelAbadia.com
ExtendedObjectDataSource samples
Samples



Product Information

Simple Types without Optimistic Concurrency

In this sample we use a CompatObjectDataSource to show, edit and update data using a GridView and to insert data using a few TextBoxs controls. Note that we are using simple types as the parameters to the CompatObjectDataSource.

 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
Add Product

Sample source code

The ASPX file for this sample is:


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

 

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

 

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

    <h1>Simple Types without 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"

        DeleteMethod="Delete" InsertMethod="Add" OldValuesParameterFormatString="old_{0}"

        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 />

    <table cellpadding="0" cellspacing="0" class="htmltable">

        <tr>

            <td>name</td>

            <td style="width: 100px">

                <asp:TextBox ID="txtName" runat="server" EnableViewState="False"> 
                </
asp:TextBox></td>

        </tr>

        <tr>

            <td style="width: 100px">

                description</td>

            <td style="width: 100px">

                <asp:TextBox ID="txtDescription" runat="server" EnableViewState="False">
                </
asp:TextBox></td>

        </tr>

        <tr>

            <td style="width: 100px">

                price</td>

            <td style="width: 100px">

                <asp:TextBox ID="txtPrice" runat="server" EnableViewState="False">
                </
asp:TextBox></td>

        </tr>

    </table>

    <asp:LinkButton ID="lbAdd" runat="server" OnClick="lbAdd_Click">Add Product 
    </
asp:LinkButton><br />

    <br />

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

</asp:Content>


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


using System;

using System.Data;

using System.Web.UI.WebControls;

 

public partial class SimpleTypesNC : 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.OutputParameters["generatedId"]);

    }

 

    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;

    }

 

    protected void lbAdd_Click(object sender, EventArgs e)

    {

        Parameter outputParam = new Parameter();

            outputParam.Name = "generatedId";

            outputParam.Type = TypeCode.Int32;

            outputParam.Direction = ParameterDirection.Output;

 

        ObjectDataSource1.InsertParameters.Add(new
        ControlParameter("name", TypeCode.String, "txtName", "Text"));

        ObjectDataSource1.InsertParameters.Add(new
        ControlParameter("description", TypeCode.String, "txtDescription", "Text"));

        ObjectDataSource1.InsertParameters.Add(new

        ControlParameter("price", TypeCode.String, "txtPrice", "Text"));

            ObjectDataSource1.InsertParameters.Add(outputParam);

 

            ObjectDataSource1.Insert();

 

            txtName.Text = "";

            txtDescription.Text = "";

            txtPrice.Text = "";

    }

}