ManuelAbadia.com
ExtendedObjectDataSource samples
Samples



Product Information

Modifying the parameters when using Custom Objects and the AffectedRows auto assignment

The ObjectDataSource does not allow us to modify the parameters collection when using custom objects as the data transfer objects in the Insert, Update and Delete operations. However, as you can see in this sample, the controls in the ExtendedObjectDataSource package let you modify the parameters collection without any problem.

In this sample there is a details view in order to be able to insert new data. In the insert operation we add a new parameter to get the generated id.

If we're using paging with the ObjectDataSource, it is mandatory to set the AffectedRows property after the delete operation because if we don't set it and we delete the last record on the last page of a control like the GridView, the control doesn't work properly and it doesn't produce any HTML output. This is because by default, the AffectedRows property is set to -1 in the ObjectDataSource, so when the GridView completes the delete operation, it will check if the delete operation affected any record to recalculate the page of data that it is showing. So if we do not capture the Deleted event and manually assign the AffectedRows property, the control will not function properly.

Fortunately, the CompatObjectDataSource and the ExtendedObjectDataSource have a property called AutoAssignAffectedRows that when set to true will automatically set the AffectedRows property to 1 if the Insert, Update or Delete method return a positive integer. Otherwise, the AffectedRows will be set to 0, so the GridView behaves properly without writing any code.

 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="ModifParams.aspx.cs" Inherits="ModifParams" Title="Untitled Page" %>

 

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

 

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

<h1>Modifying the parameters when using Custom Objects and the AffectedRows auto assignment</h1>

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

        AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"

        DataKeyNames="Id" DataSourceID="ObjectDataSource1" PageSize="4"
        CssClass
="gridview"

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

        EmptyDataText="Sorry, there are no products that matched your search criteria">

            <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:ExtendedObjectDataSource ID="ObjectDataSource1" runat="server"
            DataObjectTypeName
="Product" TotalRowCountParameterName="totalRowCount"

            DeleteMethod="Delete" InsertMethod="AddWithOutput"
            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" SortParameterName="sortedBy"
            OnInserting
="ObjectDataSource1_Inserting">

        </manu:ExtendedObjectDataSource>

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

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

using System.Web.UI;

using System.Web.UI.WebControls;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

 

    protected void ObjectDataSource1_Selected(object s, ObjectDataSourceStatusEventArgs e)

    {

    }

 

    protected void ObjectDataSource1_Inserting(object s,ObjectDataSourceMethodEventArgs e)

    {

        // add a parameter to the add method

        e.InputParameters.Add("generatedId", -1);

    }

 

    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;

    }

}