Delete vs. Remove

  • Thread starter Thread starter Tom Archer
  • Start date Start date
T

Tom Archer

I'm trying to clarify the key differences between these
two methods:

1) An ADO.NET book that I'm reading states that both
DataRow::Delete and DataRowCollection::Remove are used to
delete rows. My testing indicates that Delete deletes the
row (sets DataRow::State to Deleted and deletes the row
from the data store upon DataAdapter::Update). On the
other hand, Remove simply removes the row from the
collection (sets the DataRow::State to Detached) and
DataAdapter::Update does *not* remove the row. Therefore,
it appears as though the book is incorrect or am I
misunderstanding something?

2) If I call Delete and then call Remove, the
DataRowCollection::Count remains the same as before the
Delete. However, reversing these calls does detach the row
and then mark it for deletion. This leads me to believe
that internally the DataRowCollection won't remove a row
designated as Deleted. Is that true?

3) The previous two points leads me to believe that if I
wanted to give the user the ability to view deleted rows
so that they could undo a deletion, I could:

3-1) Remove *and* Delete the row so that the DefaultView
won't show it and it would be deleted upon updating the
data adapter.
3-2) Use a second view in order to enumerate rows marked
as Deleted so that the user can select from them if they
want to undo a delete. Is this correct?
 
3-1) Remove *and* Delete the row so that the DefaultView
won't show it and it would be deleted upon updating the
data adapter.
3-2) Use a second view in order to enumerate rows marked
as Deleted so that the user can select from them if they
want to undo a delete. Is this correct?

Looks like I can't remove the row and then set up a view
to search on Detached rows as that is not a valid
DataViewRowState enumeration value. Therefore, it appears
like I need to change my steps to this:

3-1 Delete the row (and *don't* Remove)
3-2 Use the DefaultView when enumerating my table (instead
of the bare Rows collection
3-3 Create a DataView with Deleted as the filter in order
to display deleted rows and give the user the ability to
undo deletes.
 
You may have misinterpreted the text? The DataRowCollection.Remove() is not
meant to "delete" the record. It only removes it from the collection. To
delete a record from the database, you must use DataRow.Delete(). If the
text contradicts what I've just stated then you should inform the author,
and hope he posts a correction on the books website.

Michael Lang, MCSD
 
It's one of those situations where the author's text is
very misleading. It makes sense after some testing, but
that's what the book is supposed to do for me - save time.
Oh well. The majority of the book is good. It's just weak
in this a few other areas.
 
Back
Top