ObjectDataSource UpdateParameters null

Playing around with ObjectDataSource I found that ‘Update’ didn’t work. Specifically, I had an ObjectDataSource being used as a Data Source for a GridView. The ODS.Select was returning a DataSet drawn from an Access database. When I clicked on the GridView ‘Update’ link (after clicking the ‘Edit’ Command link) the data in the DataGrid would return to its pre-edited state. The Update was not being processed, but why ?

Through A Glass Darkly
Peering into the ODS.UpdateParameters I noticed that the parameters corresponding to the original DB values were null. This meant that the Update statement could not select any record to update. To check that the Update would work if correct values were passed in, I edited the DefaultValue of the UpdateParameters to a hardcoded value in Markup. With this in place the DB Update worked. So I had to discover why the Original Values were not being initialized.

This Blog post on Asp.Net Help, Using Parameters with the Object Data Source Control pointed me in the right direction by giving me a pointer to the MSDN article, “How a Data Source Control Creates Parameters for Data-bound Fields” which explains exactly how the ODS Parameters are generated.

Gold In Them Thar Incomprehensible Articles
The precise nugget of MSDN wisdom which assisted me was this:

For an update operation, the data source control populates its UpdateParameters collection with values from the name/value pairs in the Keys, NewValues, and OldValues collections. For a delete operation, the data source control populates its DeleteParameters collection with values from the name/value pairs in the Keys and OldValues collections.

The OldValues collection is not populated by default. It is populated only when the data-source control’s ConflictDetection property is set to CompareAllValues.

Too Easy (or ‘How to be a Genius With Retrospective Understanding’)
I had configured my Dataset to use Optimistic Concurrency, that’s why the ODS Update method was trying to generate parameters for the Original DB Values (original_ID and so on) BUT the KeysCollection from which these parameter values are read, ‘OldValues’, is not generated unless Markup has ConflictDetection=CompareAllValues.

asp:ObjectDataSource ID="teamDataSource" runat="server" DeleteMethod="Delete"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetData" ConflictDetection="CompareAllValues"

Now my Update goes sweeter than a hob-nailed boot through a Cane Toad’s spinal cord. Try one today.

ObjectDataSource Resources

Scott Mitchell of 4GuysFromRolla has a great set of tutorials on how to use ObjectDataSource in a multi-tier application. Tutorials 1, 2 and 4 through 6 give detailed instructions on how to create the basic tiers (Data Access, Business Logic, Presentation) using the ODS.


Tags: ,

2 Responses to “ObjectDataSource UpdateParameters null”

  1. johnny Says:

    the tag says ObjectContainerDataSource, but the post seems to be looking at the ObjectDataSource. I am currently having a problem with old values not being populated – my DataSource is an ObjectContainerDataSource and has no ‘ConflictDetection’ property.

  2. baraholka1 Says:

    Hiya johnny,

    Thanks for pointing out the Tag error.
    Sorry, I don’t have a definitive answer for your issue. The only experience I have remotely related is using an OCDS with an asp:UpdatePanel control from the Ajax Toolkit.
    I found that DropDownLists inside an UpdatePanel which was referenced by Trigger controls from another UpdatePanel would not unbind to the OCDS on a FormView.UpdateItem.
    I had to manually unbind these controls by doing a FindControl and writing the value to my target object (a DTO) directly.
    Are you using any AJAX on your Form ?



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: