RVTDocs.com
Namespace: Autodesk.Revit.DB Class: Transaction

RollBack

Method
Description:
Rolls back all changes made to the model during the transaction.
Remarks:
By rolling back a transaction, all changes made to the model are discarded. RollBack may only be called for a transaction that has been started. (Use the GetStatus()()()() method to check the current state.) Be aware that rolling back may be delayed (as a result of failure handling.) Callers should always check the returned status to test whether a transaction was rolled back successfully. Only after rolling back is fully completed, the transaction may be started again.
Syntax
public TransactionStatus RollBack()
Examples
public bool CreateGrid(Autodesk.Revit.DB.Document document, XYZ p1, XYZ p2)
{
   // 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 Grid"))
   {
      // Must start a transaction to be able to modify a document
      if (TransactionStatus.Started == transaction.Start())
      {
         // We create a line and use it as an argument to create a grid
         Line gridLine = Line.CreateBound(p1, p2);

         if ((null != gridLine) && (null != Grid.Create(document, gridLine)))
         {
             if (TransactionStatus.Committed == transaction.Commit())
             {
                return true;
             }
         }

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

         transaction.RollBack();
      }
   }
   return false;
}
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.