Delete with ObjectDataSource does not work

J

Jürgen Bayer

Hi,

I just tried out the ObjectDataSource of ASP.NET 2.0. A simple application
works with a GridView bound to an ObjectDataSource. The ObjectDataSource is
set to a (factory) class (PersonFactory) with static methods (Select,
Update, Delete, see below). The factory methods work with instances of a
simple class (Person, see below). Select and Update works, but the Person
object passed to the Delete method is always empty (Id == 0; FirstName ==
null, LastName == 0). I tried to use factory methods with separate
parameters, but the (Person)Id parameter passed to the Delete method is also
0.

Anyone who knows what's wrong?

Juergen

The Person class
************
public class Person
{
private int id;
public int Id
{
get { return this.id; }
set { this.id = value; }
}

private string firstName;
public string FirstName
{
get { return this.firstName; }
set { this.firstName = value; }
}

private string lastName;
public string LastName
{
get { return this.lastName; }
set { this.lastName = value; }
}
}

The PersonFactory class (simplified)
***************************
public class PersonFactory
{
public static List<Person> Select()
{
List<Person> persons = new List<Person>();

// "Read" persons
Person p = new Person();
p.Id = 1001;
p.FirstName = "Zaphod";
p.LastName = "Beeblebrox";
persons.Add(p);

p = new Person();
p.Id = 1002;
p.FirstName = "Ford";
p.LastName = "Prefect";
persons.Add(p);

return persons;
}

public static void Update(Person p)
{
// Update person
// ...
}

public static void Delete(Person p)
{
// Delete Person
// But: p is an empty object (p.Id == 0) !???
}
}

The ObjectDataSource and the GridView on the Web form
*********************************************
<asp:ObjectDataSource ID="personDataSource" runat="server"
SelectMethod="Select"
TypeName="PersonFactory" DeleteMethod="Delete" InsertMethod="Insert"
UpdateMethod="Update"
DataObjectTypeName="Person">
</asp:ObjectDataSource>

<asp:GridView ID="personGridView" runat="server" AutoGenerateColumns="False"
DataSourceID="personDataSource"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="4"
ForeColor="Black" GridLines="Horizontal">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White"
/>
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right"
/>
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
</asp:GridView>
 
J

Jürgen Bayer

I found the answer myself: I have to set the DataKeyNames propery to the
name of the Id property.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top