Commit Method


Commits all changes made to the model during the transaction.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 16.0.0.0 (16.0.0.0)

Syntax

C#
public TransactionStatus Commit()
Visual Basic
Public Function Commit As TransactionStatus
Visual C++
public:
TransactionStatus Commit()

Return Value

If finished successfully, this method returns TransactionStatus.Committed.

Note it is possible the RolledBack status is returned instead as an outcome of failure handling. If TransactionStatus::Pending is returned it means that failure handling has not been finalized yet and Revit awaits a user actions. Until committing is fully finalized, no changes to the document can be made (including starting of new transactions).

The returned status does not have to be necessarily the same as the status returned by GetStatus even when the method is called immediately after committing the transaction. Such a difference may happen due to actions made by a transaction finalizer, if there was one set. (See FailureHandlingOptions for more details.)

Remarks

By committing a transaction, all changes made to the model during the transaction are accepted. A new undo item will appear in the Undo menu in Revit, which allows the user to undo the changes. The undo item will have this transaction's name. Commit may only be called for a transaction that has been started. (Use the GetStatus method to check the current state.) Be aware that committing may fail or can be delayed (as a result of failure handling.) Callers should always check the returned status to test whether a transaction was committed successfully. Only after a transaction is successfully committed (or rolled back as a result of handling transaction failures), it may be started again.

Examples

Copy C#
public bool CreateLevel(Autodesk.Revit.DB.Document document, double elevation)
{
   // All and any transaction should be enclosed in a 'using'
   // block or guarded within a try-catch-finally blocks
   // to guarantee that a transaction does not out-live its scope.
   using (Transaction transaction = new Transaction(document, "Creating Level"))
    {
       // Must start a transaction to be able to modify a document

       if( TransactionStatus.Started == transaction.Start())
       {
          if (null != Level.Create(document, elevation))
          {
             // For many various reasons, a transaction may not be committed
             // if the changes made during the transaction do not result a valid model.
             // If committing a transaction fails or is canceled by the end user,
             // the resulting status would be RolledBack instead of Committed.
             return (TransactionStatus.Committed == transaction.Commit());
          }

          // For we were unable to create the level, we will roll the transaction back
          // (although on this simplified case we know there weren't any other changes)

          transaction.RollBack();
       }
    }
    return false;
}
Copy VB.NET
Public Function CreateLevel(document As Autodesk.Revit.DB.Document, elevation As Double) As Boolean
    ' All and any transaction should be enclosed in a 'using'
    ' block or guarded within a try-catch-finally blocks
    ' to guarantee that a transaction does not out-live its scope.
    Using transaction As New Transaction(document, "Creating Level")
        ' Must start a transaction to be able to modify a document


        If TransactionStatus.Started = transaction.Start() Then
       If Level.Create(document, elevation) IsNot Nothing Then
          ' For many various reasons, a transaction may not be committed
          ' if the changes made during the transaction do not result a valid model.
          ' If committing a transaction fails or is canceled by the end user,
          ' the resulting status would be RolledBack instead of Committed.
          Return (TransactionStatus.Committed = transaction.Commit())
       End If

            ' For we were unable to create the level, we will roll the transaction back
            ' (although on this simplified case we know there weren't any other changes)


            transaction.RollBack()
        End If
    End Using
    Return False
End Function

Exceptions

Exception Condition
Autodesk.Revit.Exceptions InvalidOperationException The current status of the transaction is not 'Started'. Transaction must be started before calling Commit or Rollback. -or- The transaction's document is currently in failure mode. No transaction operations are permitted until failure handling is finished.

See Also