dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedDuringTransaction…

Error message

SR.Format(SR.MemberNotAllowedDuringTransaction, "CommitNew", "EditItem")

What it means

CommitNew finishes an AddNew transaction, so it cannot run while an EditItem transaction is open. If IsEditingItem is true, ListCollectionView.CommitNew throws InvalidOperationException naming CommitNew and EditItem.

Solutions

  1. Call CommitEdit() or CancelEdit() to close the EditItem transaction before calling CommitNew
  2. Ensure each AddNew/EditItem is paired with exactly one Commit/Cancel call in the right order
  3. Check IsEditingItem (and IsAddingNew) before invoking CommitNew

Example fix

// before
collectionView.CommitNew(); // throws if EditItem is open
// after
var ecv = (IEditableCollectionView)collectionView;
if (ecv.IsEditingItem) ecv.CommitEdit();
if (ecv.IsAddingNew) ecv.CommitNew();
Defensive patterns

Strategy: validation

Validate before calling

var ecv = (IEditableCollectionView)view;
if (ecv.IsEditingItem) ecv.CommitEdit();
if (ecv.IsAddingNew) ecv.CommitNew();

Try / catch

try { view.CommitNew(); }
catch (InvalidOperationException)
{
    // an EditItem transaction is open; commit/cancel it first
}

Prevention

When it happens

Trigger: Calling CommitNew() while an EditItem transaction is active (EditItem called, no CommitEdit/CancelEdit yet) — e.g. interleaved add/edit calls on IEditableCollectionView.

Common situations: MVVM command handlers where AddNew and EditItem flows overlap; a DataGrid committing a new row while a cell edit transaction is still open due to missing CommitEdit calls.

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

Appendix: source

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

            // raise events as if the new item appeared in the adjusted position
            ProcessCollectionChangedWithAdjustedIndex(
                                        new NotifyCollectionChangedEventArgs(
                                                NotifyCollectionChangedAction.Add,
                                                newItem,
                                                position),
                                        -1, position);
        }

        /// <summary>
        /// Complete the transaction started by <seealso cref="AddNew"/>.  The new
        /// item remains in the collection, and the view's sort, filter, and grouping
        /// specifications (if any) are applied to the new item.
        /// </summary>
        public void CommitNew()
        {
            if (IsEditingItem)
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringTransaction, "CommitNew", "EditItem"));
            VerifyRefreshNotDeferred();

            if (_newItem == NoNewItem)
                return;

            // grouping works differently
            if (IsGrouping)
            {
                CommitNewForGrouping();
                return;
            }

            // from the POV of view clients, the new item is moving from its
            // position adjacent to the placeholder to its real position.
            // Remember its current position (have to do this before calling EndNew,
            // because InternalCount depends on "adding-new" mode).
            int fromIndex = 0;
            switch (NewItemPlaceholderPosition)

View on GitHub (pinned to 81131a70a4)