dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedDuringTransaction…

Error message

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

What it means

CommitEdit() completes an EditItem transaction, but throws InvalidOperationException if an AddNew transaction is active. The view permits only one open transaction, so committing an edit while a new item is being added is rejected — commit the new item (CommitNew/CancelNew) first.

Solutions

  1. Call CommitNew() or CancelNew() before CommitEdit().
  2. Guard: if (view.IsAddingNew) view.CommitNew(); else if (view.IsEditingItem) view.CommitEdit();
  3. Track which transaction your code opened and close that one specifically.
  4. Prefer the bound control's own commit mechanisms over manual calls to avoid interleaving.

Example fix

// before
if (view.IsAddingNew) view.CommitEdit();

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

Strategy: validation

Validate before calling

// C#
if (view.IsAddingNew) view.CommitNew();
else if (view.IsEditingItem) view.CommitEdit();

Type guard

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

Try / catch

try { view.CommitEdit(); }
catch (InvalidOperationException ex) { Log.Warn("CommitEdit blocked by open AddNew", ex); view.CommitNew(); }

Prevention

When it happens

Trigger: Calling AddNew() and then CommitEdit() without CommitNew in between; or an implicit CommitEdit triggered by UI focus change while a pending AddNew row exists.

Common situations: DataGrid commit pipeline interleaving with manual AddNew; code that always calls CommitEdit in a cleanup handler regardless of which transaction is open; LostFocus handlers committing edits during an add.

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/1e99c46a5acf5c6d. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs:980

                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;

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

            if (ieo != null)
            {
                BindingOperations.AccessCollection(InternalList,
                    () =>
                    {
                        ProcessPendingChanges();
                        ieo.EndEdit();
                    },
                    true);

View on GitHub (pinned to 81131a70a4)