473,657 Members | 2,463 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 1923
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
4061
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
4478
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
2304
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
14316
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
3057
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
1944
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
3381
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
1145
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
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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
8513
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
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4173
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...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
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.