{"record":{"id":"0e3e8a7357d4bc4b","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringtransaction-cancelnew","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringTransaction, \"CancelNew\", \"EditItem\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringTransaction, \"CancelNew\", \"EditItem\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs","lineNumber":696,"sourceCode":"            if (_newItem != NoNewItem)\n            {\n                int delta = (NewItemPlaceholderPosition == NewItemPlaceholderPosition.AtBeginning) ? 1 : 0;\n                NotifyCollectionChangedEventArgs args = ProcessCommitNew(_newItemIndex, _newItemIndex + delta);\n                if (args != null)\n                {\n                    base.OnCollectionChanged(InternalList, args);\n                }\n            }\n        }\n\n        /// <summary>\n        /// Complete the transaction started by <seealso cref=\"AddNew\"/>.  The new\n        /// item is removed from the collection.\n        /// </summary>\n        public void CancelNew()\n        {\n            if (IsEditingItem)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringTransaction, \"CancelNew\", \"EditItem\"));\n            VerifyRefreshNotDeferred();\n\n            if (_newItem == NoNewItem)\n                return;\n\n            // cancel the AddNew\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.CancelNew(_newItemIndex);\n                    }","sourceCodeStart":678,"sourceCodeEnd":714,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L678-L714","documentation":"ArgumentException from BindingListCollectionView.CancelNew: the edit-transaction API is being used in the wrong order — CancelNew is called while the open transaction is an EditItem transaction (not AddNew), and this member is not allowed during that transaction.","triggerScenarios":"Calling EditItem() and then CancelNew() without closing the edit transaction via CommitEdit() or CancelEdit().","commonSituations":"A cancel button that calls both CancelEdit and CancelNew but in the wrong order; error-recovery code that resets add state while an item edit remains open; user hits Escape during a DataGrid edit that started from AddNew.","solutions":["Match cancel calls to the transaction type: call EndEdit/CancelEdit for EditItem, CancelNew only for AddNew","Check IsAddingNew before calling CancelNew"],"exampleFix":"// before\nif (view.IsAddingNew) view.CancelNew();\n\n// after\nif (view.IsEditingItem) view.CancelEdit();\nif (view.IsAddingNew) view.CancelNew();","handlingStrategy":"validation","validationCode":"// C#\nif (view.IsEditingItem) view.CancelEdit();\nif (view.IsAddingNew) view.CancelNew();","typeGuard":"bool CanCancelNew(IEditableCollectionView v) => !v.IsEditingItem && v.IsAddingNew;","tryCatchPattern":"try { view.CancelNew(); }\ncatch (InvalidOperationException ex) { Log.Warn(\"CancelNew blocked by open EditItem transaction\", ex); }","preventionTips":["Always cancel the edit transaction before canceling the add.","Use a single reset helper for cancel/cleanup paths.","Check state flags instead of assuming which transaction is open."],"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"}