{"record":{"id":"8e93c5b11bd0e68d","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringtransaction-commitedit-8e93c5","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringTransaction, \"CommitEdit\", \"AddNew\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringTransaction, \"CommitEdit\", \"AddNew\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":1177,"sourceCode":"                CommitNew();    // implicitly close a previous AddNew\n            }\n\n            CommitEdit();   // implicitly close a previous EditItem transaction\n\n            SetEditItem(item);\n\n            IEditableObject ieo = item as IEditableObject;\n            ieo?.BeginEdit();\n        }\n\n        /// <summary>\n        /// Complete the transaction started by <seealso cref=\"EditItem\"/>.\n        /// The pending changes (if any) to the item are committed.\n        /// </summary>\n        public void CommitEdit()\n        {\n            if (IsAddingNew)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringTransaction, \"CommitEdit\", \"AddNew\"));\n            VerifyRefreshNotDeferred();\n\n            if (_editItem == null)\n                return;\n\n            object editItem = _editItem;\n            IEditableObject ieo = _editItem as IEditableObject;\n            SetEditItem(null);\n\n            ieo?.EndEdit();\n\n            // see if the item is entering or leaving the view\n            int fromIndex = InternalIndexOf(editItem);\n            bool wasInView = (fromIndex >= 0);\n            bool isInView = wasInView ? PassesFilter(editItem)\n                                    : SourceList.Contains(editItem) && PassesFilter(editItem);\n\n            // editing may change the item's group names (and we can't tell whether","sourceCodeStart":1159,"sourceCodeEnd":1195,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L1159-L1195","documentation":"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.","triggerScenarios":"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.","commonSituations":"Shared save handlers that always call CommitEdit regardless of whether the last transaction was AddNew or EditItem; automation/tests that interleave AddNew and EditItem.","solutions":["Branch on state: call CommitNew() when IsAddingNew, CommitEdit() when IsEditingItem.","Cancel the pending add (CancelNew) if the new item should not be committed, then call CommitEdit if an edit is pending.","Restructure save logic to end whichever transaction is open before starting another."],"exampleFix":"// before\ncollectionView.CommitEdit();\n\n// after\nif (collectionView.IsAddingNew)\n    collectionView.CommitNew();\nelse if (collectionView.IsEditingItem)\n    collectionView.CommitEdit();","handlingStrategy":"validation","validationCode":"if (view.IsAddingNew) view.CommitNew();\nelse if (view.IsEditingItem) view.CommitEdit();","typeGuard":"bool CanCommitEdit(ICollectionView v) => !v.IsAddingNew && v.IsEditingItem;","tryCatchPattern":"try { view.CommitEdit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"CommitEdit\"))\n{ view.CommitNew(); /* correct transaction closer */ }","preventionTips":["Branch commit logic on IsAddingNew vs IsEditingItem.","Never open EditItem while AddNew is pending (or close it first).","Keep a single transaction manager around the view rather than ad-hoc calls."],"tags":["wpf","collectionview","transaction"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}