Question

I'm adding several rows to a DataTable in my strongly-typed DataSet and use a TableAdapterManager to insert the changes into my database. Using the UpdateAll function of the TableAdapterManager results in case of a failure in a database rollback of all inserted rows. Unfortunately DataTable.RejectChanges does not "rollback" the same rows in the DataTable. In the call to DataTable.RejectChanges method only the last row is removed from the DataTable. I want the DataSet to have the same status as the database.
Isn't RejectChanges per MSDN documentation deleting all new (uncomitted) rows? Am I doing something wrong?

My code:

foreach (var item in List)
{
   DataSet.customerRow custRow = ds.customer.NewcustomerRow();
   custRow.name = item.Name;
   try
   {                         
       ds.customer.AddcustomerRow(custRow);   
   }
   catch (Exception ex)
   {
       ProcessException(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
       valid=false;
   }                   
}
if (valid)
{
    DataSetTableAdapters.TableAdapterManager adapterManager = new DataSetTableAdapters.TableAdapterManager();                    
    adapterManager.customerTableAdapter = new DataSetTableAdapters.customerTableAdapter();                    
    try
    {                        
        retryPolicy.ExecuteAction(() =>
            {
            adapterManager.UpdateAll(ds);
            });
    }
    catch (Exception ex)
    {                       
        ds.customer.RejectChanges();
    }
}
else
{
    ds.customer.RejectChanges();                     
}      
Was it helpful?

Solution

The solution is to set adapterManager.BackupDataSetBeforeUpdate = true; This creates an internal backup copy of the dataset which is "reused" in case of failures.

MSDN: Hierarchical Update Overview

"The backup copy is only in memory during the execution of the TableAdapterManager.UpdateAll method. Therefore, there is no programmatic access to this backup dataset because it either replaces the original dataset or goes out of scope as soon as the TableAdapterManager.UpdateAll method has finished running."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top