Tuesday, November 25, 2008

How to delete a row in the DataTable (solution) .

How to delete a row in the DataTable (solution)

The VS .NET / ADO .NET documentation is very poor in the parts about deleting rows from the table. It should tell you the following, but it doesn't.

Let's say you want to delete a record. You read the help, and you write something like:


or, you write something like:


which is equivalent to the code above.

All is good and well. You have deleted the row from the data table. Now, you want to update the physical database, and you call the Update method.

BZZZZZZZT! Wrong answer! The database rows won't be deleted at all!

What you need to do (and the MS documentation doesn't tell you - just check in the parts about RemoveAt, Delete, Update - it is nowhere to be found) is:

row.Delete(); // mark row for deletion

...Update(); // update the physical database

table.AcceptChanges(); // remove the rows marked for deletion from the dataset

Microsoft, please update your documentation! You could easily put this in the help pages for Delete(), RemoveAt(), or Update()!

Thank you very much!

taken from: http://discuss.fogcreek.com/dotnetquestions/default.asp?cmd=show&ixPost=1950