Click or drag to resize
CalculationWorks Logo
Using Transactions

All data updates in a BcfDataSet a executed transactional. When you update a cell value or add a row without using transactions a transaction will be started and committed implicit.

When a cell is updated and the cell is related input to a computed cell the computed cell will be recomputed when transaction commits. The computed cell update will be stored in the transactions compensation repository. BCF Undo-Redo feature uses processed transactions to work. Use explicit transaction handling when:

  • group multible data updates as one undo item

  • prevent unnecessary calculation on computed columns

For example: Columns "Firstname", "Lastname", "Fullname"; "Fullname" is computed (Firstname + " " + Lastname). The user updates Firstname and Lastname by selecting a person from a list. Without explicit transaction Fullname would be computed twice. (First time when the first name part will be set; second when second name part will be set.) The first calculation is unnecessary and causes the model to be in an invalid state for a short time (combining first- and lastname of different persons). When using undo the user may restore the invalid state. "Simple Transaction Processing" exsample below shows how to solve the issues.

This topic contains the following sections:

Simple Transaction Processing

This example shows simple transaction processing. The method will set two values within one transaction.

public void SetPersonNames(BcfRow personModel, string firstname, string lastname) {
    var transaction = myColumn.Table.DataSet.BeginTransaction();
    personModel["FirstName"].Value = firstname;
    personModel["LastName"].Value = lastname;
    transaction.Commit();
}
Sub Transaction Processing

This example shows sub transaction processing. The method will try to set all values of the given column to null. If setting fails or subtransaction cannot commit the cell will not being updated.

public int TrySetAllValuesNull(BcfColumn myColumn) {
    int valuesNotReset = 0;
    var transaction = myColumn.Table.DataSet.BeginTransaction();
    foreach (var row in myColumn.Table.Rows) {
        var subTransaction = transaction.BeginSubTransaction();
        try {
            row[myColumn].Value = null;
            subTransaction.Commit();
        }
        catch (Exception) {
            valuesNotReset++;
        }
    }
    transaction.Commit();
    return valuesNotReset;
}
See Also