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"
Now my Update goes sweeter than a hob-nailed boot through a Cane Toad’s spinal cord. Try one today.
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.