.Net ramblings
# Friday, 16 December 2005
GridView with ObjectDataSource, Delete parameters not working...

i'm just experimenting with the gridview control, and i got stuck trying to delete an object with the auto-included delete button.  the delete method would get invoked properly, but the ID parameter was always zero, which means the int parameter was uninitialised.

my problem was that i didn't have the DataKeyNames property set in the Gridview. I just set it to 'ID' and it worked fine then.


Friday, 16 December 2005 14:21:53 (GMT Standard Time, UTC+00:00)  #    Comments [18]  Asp.Net

Tuesday, 20 December 2005 09:33:21 (GMT Standard Time, UTC+00:00)
Wow, thanks so much I had the same problem and your post saved me about an hour of searching.
Wednesday, 21 December 2005 00:26:03 (GMT Standard Time, UTC+00:00)
glad you found it helpful.
Tim Mackey
Tuesday, 18 April 2006 11:58:57 (GMT Daylight Time, UTC+01:00)
Thanks very much, Tim - I was also having this problem.
Chris Fulstow
Friday, 18 August 2006 18:11:51 (GMT Daylight Time, UTC+01:00)
Thank you! Same problem here. Really - really annoying.
Grateful Coder
Tuesday, 03 October 2006 22:46:08 (GMT Daylight Time, UTC+01:00)
Same here! Thanks for posting it!!
Ariel
Friday, 06 October 2006 13:18:16 (GMT Daylight Time, UTC+01:00)
THANK YOU VERY MUCH!!

I have spent several hours trying to understand why this didn't work. I works fine with Update!

// Kristian
Kristian
Wednesday, 07 February 2007 20:29:32 (GMT Standard Time, UTC+00:00)
Thanks, that really helped me :)
Joachim
Friday, 09 February 2007 19:37:34 (GMT Standard Time, UTC+00:00)
Same problems.

thanks
Camel
Thursday, 05 April 2007 00:29:46 (GMT Daylight Time, UTC+01:00)
Thanks. It is extremely usefull.
Sunday, 15 April 2007 21:15:15 (GMT Daylight Time, UTC+01:00)
The DataKeyNames is neccesary also when the delete method take the whole object has parameters.

Thanks
Pbl
Thursday, 24 January 2008 00:05:42 (GMT Standard Time, UTC+00:00)
Thank you. I spent hours trying to figure this out.
Monday, 07 July 2008 14:21:12 (GMT Daylight Time, UTC+01:00)
What a HELP!!

Thankz, I will forward you post!
Wednesday, 05 November 2008 04:25:24 (GMT Standard Time, UTC+00:00)
My delete method requires 2 more parameters, yet they are being passed as NULL byt the objectdatasource to my delete method. I've checked the deleteparameters and they look ok. I've been pulling my hair out for a few days over this - can anyone help?
Holdfast
Friday, 07 November 2008 19:19:17 (GMT Standard Time, UTC+00:00)
I also spent several hours trying to figure this out. So glad I found your blog.

THANK YOU!
Will Hutton
Saturday, 20 June 2009 12:58:18 (GMT Daylight Time, UTC+01:00)
I have exactly the same problem and it's solved

thanks
Thursday, 02 July 2009 09:50:11 (GMT Daylight Time, UTC+01:00)
So after 4 years, people are still searching on this issue. Great post Tim - short and sweet and an apparent life saver after so long. I'd like to extend it a bit for others:

1. Microsoft's documentation on this is sorely lacking, so no, it's not you ;)

2. Although the DataSource wizard for ObjectDataSource doesn't show you the params for INSERT, UPDATE, and DELETE, you must still set them (at least in VS2008). Go to the properties window of your ObjectDataSource control and look at the data section. You'll find a property for each type (for example, for DELETE you'll see DeleteParameters) and it will be a collection. Expand the collection and it brings up a wizard to help you define your parameters. This is essential; why this isn't part of the main wizard (except for SELECT), I couldn't tell you. Establish your params here and you won't receive nulls.

3. For those programmatically creating their data: If you are using an ObjectDataSource which returns a DataSource you've constructed (ie, not from a DB), then be sure to set the PrimaryKey property of each table in the datasource. Since you may have more than one key, here's a snippet (VB.net)

'Create your table
dim dtTable as New DataTable()
'...put some data in it...

'Set primary key
Dim dcKey(1) As DataColumn
dcKey(1) = dtTable.Columns.Item(FIELD_FEATUREKEY)
dtTable.PrimaryKey = dcKey

The sneaky thing about the PrimaryKey property, is that it expects an array - not a column (in case your table has multiple keys).

Tie it all together and your DELETE and other methods should work fine.

Karim Sultan
Thursday, 31 March 2011 13:40:39 (GMT Daylight Time, UTC+01:00)
Thank you!
Olive Oil
Saturday, 25 February 2012 13:46:46 (GMT Standard Time, UTC+00:00)
U Saved Me........
Jerine George
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview