Datatable.acceptchanges() commits data to the table

33,286

Solution 1

The purpose of AcceptChanges() is to let the DataTable know that its data has been saved to the database. All DataRows in the DataTable have their row.RowState set to DataRowState.Unchanged.

It does not save the data to the database. That is done separately.

Solution 2

Here's the documentation:

When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. The DataRowState also changes: all Added and Modified rows become Unchanged, and Deleted rows are removed.

The AcceptChanges method is generally called on a DataTable after you attempt to update the DataSet using the DbDataAdapter.Update method.

So your actual database is unaffected.

Solution 3

Will only affect the datatable. Not any DB table.

Solution 4

Some people choose to manipulate the data in a datatable, examples are deleting certain rows or hanging all names to uppercase. removing NULL's and so on.

AcceptChanges just saves these changes to the datatable.

If you want to commit the changes you need to use a dataadaptor or use another method to get the data from the datarows in the datatable and commit the changes.

Share:
33,286
SmartestVEGA
Author by

SmartestVEGA

Updated on December 30, 2020

Comments

  • SmartestVEGA
    SmartestVEGA over 3 years

    Datatable.acceptchanges commits data to the table...means

    will it insert data to the table ..or datatable?

  • Bill Roberts
    Bill Roberts over 9 years
    This is still somewhat confusing... If I add a row, and it is marked as ADDED, seems by calling DT.ACCEPTCHANGES, the state of the row becomes UNCHANGED... I suppose the confusion comes into play, if after doing so, and I a do DATAADAPTER.UPDATE(DT), does the call look at the now UNCHANGED state and not bother trying to make a change to the database? Or instead does the UPDATE compare the rows in the DATATABLE against those in the database, regardless of the DT rowstate?
  • Bill Roberts
    Bill Roberts over 9 years
    OKay, if I read this correctly, looks like code calling DT.ACCEPTCHANGES before DA.UPDATE(DT), prevents the dataadapter from doing it's thing. That is, if you ADD/DELETE/UPDATE a row in datatable, perform an ACCEPTCHANGES, then do a DA.UPDATE(DT), the UPDATE method won't process the row because to it, the "row" is unchanged. So it seems really, to call it essentially prevents the change from reaching the DB? msdn.microsoft.com/en-us/library/…
  • Katie Kilian
    Katie Kilian over 9 years
    The DataAdapter normally manages the DataRow.RowState property. It uses the DataRow.RowState to determine what action to take for that row. If the RowState of given row is DataRowState.Unchanged, it takes no action for that row.
  • Shawn Kovac
    Shawn Kovac over 7 years
    I see it differently. You stated: 'AcceptChanges just saves these changes to the datatable.' But the changes are in actuality already saved to the DataTable. AcceptChanges really 'marks' the current changes as not really being changes anymore. That's all.
  • Unbreakable
    Unbreakable about 6 years
    But I thought AcceptChanges is called automatically when we call the Update method on DataAdaptopr? Do we need to call AcceptChanges EXPLICIT LY?