{"record":{"id":"d61c55ed3f4b5bb5","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringtransaction-canceledit-d61c55","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/ListCollectionView.cs","lineNumber":1285,"sourceCode":"                    toIndex = AdjustBefore(NotifyCollectionChangedAction.Add, editItem, SourceList.IndexOf(editItem));\n                    ProcessCollectionChangedWithAdjustedIndex(\n                                new NotifyCollectionChangedEventArgs(\n                                            NotifyCollectionChangedAction.Add,\n                                            editItem,\n                                            toIndex+delta),\n                                -1, toIndex+delta);\n                }\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":1267,"sourceCodeEnd":1303,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L1267-L1303","documentation":"CancelEdit throws InvalidOperationException when the view is in an AddNew transaction. CancelEdit only rolls back an EditItem transaction; with an add pending, CancelNew is the matching operation, so the view rejects CancelEdit to keep the transaction pair consistent.","triggerScenarios":"Calling view.CancelEdit() while IsAddingNew is true (AddNew is open and CommitNew/CancelNew was never called).","commonSituations":"Generic 'cancel' buttons wired to CancelEdit that run while a new-item row is being added; error handlers that always cancel edits on failure without checking the transaction type.","solutions":["Call CancelNew() when IsAddingNew instead of CancelEdit().","Branch on transaction state before cancelling: IsAddingNew -> CancelNew, IsEditingItem -> CancelEdit.","Fix code paths that open AddNew and later attempt edit-style cleanup."],"exampleFix":"// before\ncollectionView.CancelEdit();\n\n// after\nif (collectionView.IsAddingNew)\n    collectionView.CancelNew();\nelse if (collectionView.IsEditingItem)\n    collectionView.CancelEdit();","handlingStrategy":"validation","validationCode":"if (view.IsAddingNew) view.CancelNew();\nelse if (view.IsEditingItem) view.CancelEdit();","typeGuard":"bool CanCancelEdit(ICollectionView v) => !v.IsAddingNew && v.IsEditingItem;","tryCatchPattern":"try { view.CancelEdit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"CancelEdit\"))\n{ view.CancelNew(); /* matching operation for add transaction */ }","preventionTips":["Wire cancel buttons to a handler that checks IsAddingNew first.","Use CancelNew for the add-new row, CancelEdit only for edits.","Reset transaction state in error handlers before cleanup."],"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"}