{"record":{"id":"22a4cbb2fa2ae949","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringtransaction-commitnew","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringTransaction, \"CommitNew\", \"EditItem\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringTransaction, \"CommitNew\", \"EditItem\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs","lineNumber":649,"sourceCode":"                }\n            }\n\n            // raise events as if the new item appeared in the adjusted position\n            ProcessCollectionChanged(new NotifyCollectionChangedEventArgs(\n                                            NotifyCollectionChangedAction.Add,\n                                            newItem,\n                                            position));\n        }\n\n        /// <summary>\n        /// Complete the transaction started by <seealso cref=\"AddNew\"/>.  The new\n        /// item remains in the collection, and the view's sort, filter, and grouping\n        /// specifications (if any) are applied to the new item.\n        /// </summary>\n        public void CommitNew()\n        {\n            if (IsEditingItem)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringTransaction, \"CommitNew\", \"EditItem\"));\n            VerifyRefreshNotDeferred();\n\n            if (_newItem == NoNewItem)\n                return;\n\n            // commit the new item\n            ICancelAddNew ican = InternalList as ICancelAddNew;\n            IEditableObject ieo;\n\n            BindingOperations.AccessCollection(InternalList,\n                () =>\n                {\n                    ProcessPendingChanges();\n\n                    if (ican != null)\n                    {\n                        ican.EndNew(_newItemIndex);\n                    }","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L631-L667","documentation":"CommitNew() finalizes an AddNew transaction, but it throws InvalidOperationException if an EditItem transaction is still open. The view supports only one transaction at a time, so committing a new item while an item is being edited is an invalid state transition.","triggerScenarios":"Calling AddNew() then EditItem(...) (without CommitEdit/CancelEdit) and then CommitNew(); or EditItem() implicitly started and an outer code path calls CommitNew.","commonSituations":"DataGrid row-commit logic interleaved with manual AddNew/EditItem calls; nested handlers where one handler calls CommitNew while a cell edit is still open; missing CommitEdit before switching from edit to add.","solutions":["Call CommitEdit() or CancelEdit() before calling CommitNew().","Guard with IsEditingItem before calling CommitNew and handle the pending edit first.","Refactor so each UI action manages only one transaction (edit or add) at a time.","In DataGrid scenarios, let the grid's own commit pipeline run instead of calling CommitNew manually."],"exampleFix":"// before\nview.EditItem(item);\nview.AddNew();\nview.CommitNew();\n\n// after\nview.EditItem(item);\nview.CommitEdit();\nview.AddNew();\nview.CommitNew();","handlingStrategy":"validation","validationCode":"// C#\nif (view.IsEditingItem) view.CommitEdit(); // or CancelEdit\nview.CommitNew();","typeGuard":"bool CanCommitNew(IEditableCollectionView v) => !v.IsEditingItem && v.IsAddingNew;","tryCatchPattern":"try { view.CommitNew(); }\ncatch (InvalidOperationException ex) { Log.Warn(\"CommitNew blocked by open EditItem transaction\", ex); }","preventionTips":["Treat AddNew and EditItem as exclusive transactions.","Check IsEditingItem/IsAddingNew before committing.","Route all commit/cancel calls through one state-aware helper."],"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-21T21:30:21.729Z"}