dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedDuringTransaction…

Error message

SR.Format(SR.MemberNotAllowedDuringTransaction, "CommitEdit", "AddNew")

What it means

CommitEdit throws InvalidOperationException when the view is still in an AddNew transaction (IsAddingNew is true). CommitEdit only finishes an EditItem transaction; if AddNew is active, the correct closing call is CommitNew, so the view refuses CommitEdit to prevent mixing transactions.

Solutions

  1. Branch on state: call CommitNew() when IsAddingNew, CommitEdit() when IsEditingItem.
  2. Cancel the pending add (CancelNew) if the new item should not be committed, then call CommitEdit if an edit is pending.
  3. Restructure save logic to end whichever transaction is open before starting another.

Example fix

// before
collectionView.CommitEdit();

// after
if (collectionView.IsAddingNew)
    collectionView.CommitNew();
else if (collectionView.IsEditingItem)
    collectionView.CommitEdit();
Defensive patterns

Strategy: validation

Validate before calling

if (view.IsAddingNew) view.CommitNew();
else if (view.IsEditingItem) view.CommitEdit();

Type guard

bool CanCommitEdit(ICollectionView v) => !v.IsAddingNew && v.IsEditingItem;

Try / catch

try { view.CommitEdit(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("CommitEdit"))
{ view.CommitNew(); /* correct transaction closer */ }

Prevention

When it happens

Trigger: Calling view.CommitEdit() after view.AddNew() without an intervening CommitNew()/CancelNew(), e.g. when code assumes an edit is pending but an add is actually pending.

Common situations: Shared save handlers that always call CommitEdit regardless of whether the last transaction was AddNew or EditItem; automation/tests that interleave AddNew and EditItem.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/8e93c5b11bd0e68d. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs:1177

                CommitNew();    // implicitly close a previous AddNew
            }

            CommitEdit();   // implicitly close a previous EditItem transaction

            SetEditItem(item);

            IEditableObject ieo = item as IEditableObject;
            ieo?.BeginEdit();
        }

        /// <summary>
        /// Complete the transaction started by <seealso cref="EditItem"/>.
        /// The pending changes (if any) to the item are committed.
        /// </summary>
        public void CommitEdit()
        {
            if (IsAddingNew)
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringTransaction, "CommitEdit", "AddNew"));
            VerifyRefreshNotDeferred();

            if (_editItem == null)
                return;

            object editItem = _editItem;
            IEditableObject ieo = _editItem as IEditableObject;
            SetEditItem(null);

            ieo?.EndEdit();

            // see if the item is entering or leaving the view
            int fromIndex = InternalIndexOf(editItem);
            bool wasInView = (fromIndex >= 0);
            bool isInView = wasInView ? PassesFilter(editItem)
                                    : SourceList.Contains(editItem) && PassesFilter(editItem);

            // editing may change the item's group names (and we can't tell whether

View on GitHub (pinned to 81131a70a4)