Thursday night, I had the pleasure of presenting to the Hampton Roads SQL Server User Group. During my talk I showed how you can use the new ObjectDataSource control in ASP.NET 2.0, specifically, how you can add a new DataSet object to the App_Code folder, and the IDE will invoke a wizard which will help you configure the DataSet to use a DataTableAdapter that configures the DataSet to act as the source for the ObjectDataSource, thus giving you a layer of abstraction between your back-end database and your UI.
While my demo worked great from the read standpoint, I ran into an issue with updates, namely that I got an error similar to: “ObjectDataSource ‘ObjectDataSource1’ could not find a non-generic method ‘Update’ that has the parameters…”, etc.
After a little looking around, it turns out that the cause of this error is a slight mismatch between the GridView control (which I was using for my databinding) and the DataTableAdapter. The GridView control assumes that a Primary key column should be read-only (which typically, they are), and so does not pass the PK column when invoking the Update statement specified in the ObjectDataSource. By default, the DataTableAdapter’s Update Method includes the all columns from the Select query from which it is generated.
There are two ways you can fix this mismatch:
- Go to the column definition for the primary key column in your GridView and set its readOnly property to False. Don’t do it this way unless you don’t mind having the primary key edited.
- Modify the Update query of the DataTableAdapter to remove the primary key column. If you used the technique described above (adding a DataSet to App_Code), you can simply open the DataSet in the IDE, click on the tablenameTableAdapter title bar, and view its properties. Then click on the + sign next to UpdateCommand in the properties window, and edit the CommandText to remove the offending column.
Thanks to everyone who came out to the meeting.