473,326 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Delete with ObjectDataSource does not work

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>



May 16 '06 #1
1 1912
I found the answer myself: I have to set the DataKeyNames propery to the
name of the Id property.
May 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cv | last post by:
Hello All, I have used MultipartRequest like the following to upload images. MultipartRequest multi = new MultipartRequest(request, "../webapps/coreprogram/dealerlogos", 1024 * 1024); It...
4
by: Field | last post by:
Hi, the following snippet shows once executed this output: 2 2 I'd have rather expected this output: 2 10
2
by: Matthew Wells | last post by:
I want to delete records from the "one" table of a one to may relationship. There are no actual Access relationships set up. The "one" table has a single field PK and the "many" table has a two...
2
by: Spongebob | last post by:
Hello! I tried to use this code from Peter Torr: Foo.Attributes("onclick") = "if (confirm('delete?') == false) return false; " + Foo.Attributes("onclick") It works nomral on IE5, Windows...
4
by: John Bailey | last post by:
I am using a formview with an ObjectDatasource in ASP .Net 2.0. Everything works fine until I try the delete. The ItemDeleting event fires, so I know it should fire my delete method, but instead...
1
by: Felix_WafyTech | last post by:
Hi, I'm working with the ObjectDatasource and the application is getting more and more chatty. Is there a way I could make the ObjectDatasource support multiple DataTables that could be...
1
by: Swinky | last post by:
I have added code to delete a record (and records in related tables) in the OnClick property: MsgBox "Confirm delete. Once you confirm, record will no longer exist.", vbOKCancel strSQL =...
11
by: Jim | last post by:
Hi, I want to schedule a Python program that reads the command line for input. However, when adding an argument to the command line Python will not pick it up when using Windows scheduled...
1
by: sagar shah | last post by:
Protected Sub BtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnDelete.Click Dim feedbackId, Comm As String Dim GrdItm As DataGridItem For...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.