473,723 Members | 2,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete with ObjectDataSourc e does not work

Hi,

I just tried out the ObjectDataSourc e of ASP.NET 2.0. A simple application
works with a GridView bound to an ObjectDataSourc e. The ObjectDataSourc e 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 ObjectDataSourc e and the GridView on the Web form
*************** *************** ***************
<asp:ObjectData Source ID="personDataS ource" runat="server"
SelectMethod="S elect"
TypeName="Perso nFactory" DeleteMethod="D elete" InsertMethod="I nsert"
UpdateMethod="U pdate"
DataObjectTypeN ame="Person">
</asp:ObjectDataS ource>

<asp:GridView ID="personGridV iew" runat="server" AutoGenerateCol umns="False"
DataSourceID="p ersonDataSource "
BackColor="Whit e" BorderColor="#C CCCCC" BorderStyle="No ne"
BorderWidth="1p x" CellPadding="4"
ForeColor="Blac k" GridLines="Hori zontal">
<Columns>
<asp:CommandFie ld ShowDeleteButto n="True" ShowEditButton= "True" />
<asp:BoundFie ld DataField="Id" HeaderText="Id" SortExpression= "Id" />
<asp:BoundFie ld DataField="Firs tName" HeaderText="Fir stName"
SortExpression= "FirstName" />
<asp:BoundFie ld DataField="Last Name" HeaderText="Las tName"
SortExpression= "LastName" />
</Columns>
<FooterStyle BackColor="#CCC C99" ForeColor="Blac k" />
<SelectedRowSty le BackColor="#CC3 333" Font-Bold="True" ForeColor="Whit e"
/>
<PagerStyle BackColor="Whit e" ForeColor="Blac k" HorizontalAlign ="Right"
/>
<HeaderStyle BackColor="#333 333" Font-Bold="True" ForeColor="Whit e" />
</asp:GridView>



May 16 '06 #1
1 1928
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
4069
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 works fine. But When a user accesses through Internet, this does not work.
4
4493
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
2310
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 field PK. This code works when executed from a module (both DAO and ADO work), but not from the query window. DELETE a1.*, a2.Field1 FROM a1 LEFT JOIN a2 ON a1.Field1 = a2.Field1 WHERE (((a2.Field1) Is Null));
2
1378
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 2000. ....Oh, but on IE6, Windows XP it seems to return always true (the result is always "ok", record is always deleted, users can be upset
4
14319
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 I get an error: No parameterless constructor defined for this object, and the delete method is never called. Anyone have any ideas?
1
3060
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 retrieved as one Dataset? I'm wondering how others out there work with ObjectDatasource? The only option I now see to get this fixed is to manually make the call to the Web services, retrieve the Dataset and assign the datatable to the controls (Or can...
1
1950
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 = "DELETE FROM TOrdAck WHERE OANo=" & Me!OANo Set dbs = CurrentDb dbs.Execute strSQL, dbFailOnError MsgBox dbs.RecordsAffected & " record(s) deleted"
11
3390
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 tasks. How do I get it to work? Thanks, Jim
1
1150
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 Each GrdItm In GrdFeedback.Items If CType(GrdItm.FindControl("ChkFeedbackDelete"), CheckBox).Checked = True Then feedbackId = feedbackId & Comm & CType(GrdItm.FindControl("LblId"), Label).Text 'THIS CODE WILL DELETE ALL...
0
9241
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9160
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8062
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6685
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5996
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3209
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2612
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2149
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.