{"record":{"id":"1e99c46a5acf5c6d","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringtransaction-commitedit","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/BindingListCollectionView.cs","lineNumber":980,"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            IEditableObject ieo = _editItem as IEditableObject;\n            object editItem = _editItem;\n            SetEditItem(null);\n\n            if (ieo != null)\n            {\n                BindingOperations.AccessCollection(InternalList,\n                    () =>\n                    {\n                        ProcessPendingChanges();\n                        ieo.EndEdit();\n                    },\n                    true);","sourceCodeStart":962,"sourceCodeEnd":998,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L962-L998","documentation":"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.","triggerScenarios":"Calling AddNew() and then CommitEdit() without CommitNew in between; or an implicit CommitEdit triggered by UI focus change while a pending AddNew row exists.","commonSituations":"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.","solutions":["Call CommitNew() or CancelNew() before CommitEdit().","Guard: if (view.IsAddingNew) view.CommitNew(); else if (view.IsEditingItem) view.CommitEdit();","Track which transaction your code opened and close that one specifically.","Prefer the bound control's own commit mechanisms over manual calls to avoid interleaving."],"exampleFix":"// before\nif (view.IsAddingNew) view.CommitEdit();\n\n// after\nif (view.IsAddingNew) view.CommitNew();\nelse if (view.IsEditingItem) view.CommitEdit();","handlingStrategy":"validation","validationCode":"// C#\nif (view.IsAddingNew) view.CommitNew();\nelse if (view.IsEditingItem) view.CommitEdit();","typeGuard":"bool CanCommitEdit(IEditableCollectionView v) => !v.IsAddingNew && v.IsEditingItem;","tryCatchPattern":"try { view.CommitEdit(); }\ncatch (InvalidOperationException ex) { Log.Warn(\"CommitEdit blocked by open AddNew\", ex); view.CommitNew(); }","preventionTips":["Check IsAddingNew before CommitEdit.","Call CommitNew for pending adds, CommitEdit for pending edits.","Centralize commit logic in one state-aware method."],"tags":["wpf","data-binding","transaction-state"],"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-22T01:17:13.364Z"}