{"record":{"id":"ea455e0e11db8c87","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringtransaction-canceledit","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringTransaction, \"CancelEdit\", \"AddNew\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringTransaction, \"CancelEdit\", \"AddNew\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs","lineNumber":1019,"sourceCode":"            // editing may change the item's group names (and we can't tell whether\n            // it really did).  The best we can do is remove the item and re-insert\n            // it.\n            if (_isGrouping)\n            {\n                RemoveItemFromGroups(editItem);\n                AddItemToGroups(editItem);\n                return;\n            }\n        }\n\n        /// <summary>\n        /// Complete the transaction started by <seealso cref=\"EditItem\"/>.\n        /// The pending changes (if any) to the item are discarded.\n        /// </summary>\n        public void CancelEdit()\n        {\n            if (IsAddingNew)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringTransaction, \"CancelEdit\", \"AddNew\"));\n            VerifyRefreshNotDeferred();\n\n            if (_editItem == null)\n                return;\n\n            IEditableObject ieo = _editItem as IEditableObject;\n            SetEditItem(null);\n\n            if (ieo != null)\n            {\n                ieo.CancelEdit();\n            }\n            else\n                throw new InvalidOperationException(SR.CancelEditNotSupported);\n        }\n\n        private void ImplicitlyCancelEdit()\n        {","sourceCodeStart":1001,"sourceCodeEnd":1037,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L1001-L1037","documentation":"CancelEdit() discards an EditItem transaction, but throws InvalidOperationException if an AddNew transaction is active. The add and edit transactions are mutually exclusive; cancel the AddNew with CancelNew first.","triggerScenarios":"Calling AddNew() then CancelEdit() (instead of CancelNew); a global reset/cleanup routine that unconditionally calls CancelEdit while a new item is pending.","commonSituations":"Dialog cancel buttons wired to CancelEdit regardless of state; error-handling paths that roll back edits without checking for pending adds; Escape handling in grids where the user was adding a new row.","solutions":["Call CancelNew() when IsAddingNew is true; only call CancelEdit() when IsEditingItem is true.","Order guards: check IsAddingNew before IsEditingItem since AddNew implicitly closes an edit first.","Write a single ResetTransactions(view) helper that checks both flags and calls the matching cancel.","Let the bound control (DataGrid) handle cancel so the correct transaction is targeted."],"exampleFix":"// before\nview.CancelEdit(); // cleanup\n\n// after\nif (view.IsAddingNew) view.CancelNew();\nelse if (view.IsEditingItem) view.CancelEdit();","handlingStrategy":"validation","validationCode":"// C#\nif (view.IsAddingNew) view.CancelNew();\nelse if (view.IsEditingItem) view.CancelEdit();","typeGuard":"bool CanCancelEdit(IEditableCollectionView v) => !v.IsAddingNew && v.IsEditingItem;","tryCatchPattern":"try { view.CancelEdit(); }\ncatch (InvalidOperationException ex) { Log.Warn(\"CancelEdit blocked by open AddNew\", ex); view.CancelNew(); }","preventionTips":["Check IsAddingNew before CancelEdit.","Use a single transaction-reset helper for cancel paths.","Prefer the bound control's own cancel handling."],"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"}