The .AcceptChanges() clears flags that indicate that the row "Is Dirty" and requires an update (delete, insert, or update).
A good example for using this would be… if after your insert you needed to requery the row to get default values (such as date created).
Here is an example code that binds to the dataadapter’s RowUpdated event (using Sql Server).
private static void sqlDataAdapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
try
{
#region get the primary key after the insert
if (e.StatementType == StatementType.Insert
&& e.Status != UpdateStatus.ErrorsOccurred)
{
if (e.Row.Table.PrimaryKey.Length == 1
&& e.Row.Table.PrimaryKey[0].DataType.Name.StartsWith("Int", StringComparison.CurrentCultureIgnoreCase)
&& e.Row.Table.PrimaryKey[0].AutoIncrement)
{
SqlDataAdapter da = (SqlDataAdapter)sender;
string tableName = getTableNameFromSelectCommand(da.SelectCommand);
SqlCommand cmdSelect = da.SelectCommand;
SqlCommand cmd = da.SelectCommand.Connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = string.Format("Select * from {0} where {1} = @@Identity"
, tableName
, e.Row.Table.PrimaryKey[0].ColumnName);
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
da.SelectCommand = cmdSelect;
// update a fields to make sure the DataRow matches what is in the database
if (dt != null && dt.Rows.Count > 0)
{
DataRow drNew = dt.Rows[0];
foreach (DataColumn dc in e.Row.Table.Columns)
e.Row[dc.ColumnName] = drNew[dc.ColumnName];
}
// remove "Is Dirty" flag because the changes that have been made don't need to be passed back to the database
e.Row.AcceptChanges();
}
}
#endregion
}
catch (Exception ex)
{
handleException(ex);
}
}
Tom Brothers
MCSD (C# & VFP)