{"record":{"id":"1a887e5100787c87","repo":"dotnet/wpf","slug":"sr-calendar-onselecteddatechanged-invalidvalue-1a887e","errorCode":null,"errorMessage":"SR.Calendar_OnSelectedDateChanged_InvalidValue","messagePattern":"SR\\.Calendar_OnSelectedDateChanged_InvalidValue","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedDatesCollection.cs","lineNumber":199,"sourceCode":"                        addedItems.Add(item);\n\n                        RaiseSelectionChanged(this._removedItems, addedItems);\n                        this._removedItems.Clear();\n                        int monthDifference = DateTimeHelper.CompareYearMonth(item, this._owner.DisplayDateInternal);\n\n                        if (monthDifference < 2 && monthDifference > -2)\n                        {\n                            this._owner.UpdateCellItems();\n                        }\n                    }\n                    else\n                    {\n                        this._addedItems.Add(item);\n                    }\n                }\n                else\n                {\n                    throw new ArgumentOutOfRangeException(SR.Calendar_OnSelectedDateChanged_InvalidValue);\n                }\n            }\n        }\n\n        /// <summary>\n        /// Removes the item at the specified position.\n        /// </summary>\n        /// <param name=\"index\"></param>\n        protected override void RemoveItem(int index)\n        {\n            if (!IsValidThread())\n            {\n                throw new NotSupportedException(SR.CalendarCollection_MultiThreadedCollectionChangeNotSupported);\n            }\n\n            if (index >= this.Count)\n            {\n                base.RemoveItem(index);","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedDatesCollection.cs#L181-L217","documentation":"InsertItem() throws ArgumentOutOfRangeException when the value being inserted is not a valid selectable date — specifically when it is DateTime.MinValue or otherwise rejected as an invalid value for SelectedDate. The Calendar treats such values as invalid selection entries and refuses the insert.","triggerScenarios":"Calling selectedDates.Add(DateTime.MinValue) or inserting a DateTime that fails the collection's validity check (e.g. a default-initialized DateTime from an uninitialized field or failed parse).","commonSituations":"Binding or passing a DateTime? that defaulted to DateTime.MinValue; parsing dates that failed silently and produced default(DateTime).","solutions":["Check for DateTime.MinValue/default(DateTime) before adding and skip or substitute a valid date.","Use Nullable<DateTime> in your model so 'no date' is null instead of MinValue.","Validate parsed dates (e.g. DateTime.TryParse with range checks) before inserting."],"exampleFix":"// before\nDateTime d = default; // DateTime.MinValue\ncalendar.SelectedDates.Add(d); // throws\n// after\nif (d != DateTime.MinValue)\n    calendar.SelectedDates.Add(d);","handlingStrategy":"validation","validationCode":"if (date == DateTime.MinValue || date == default(DateTime))\n{\n    // skip or substitute a valid date before calling Add/Insert\n    return;\n}\ncalendar.SelectedDates.Add(date);","typeGuard":"bool IsValidSelectableDate(DateTime? date) => date.HasValue && date.Value != DateTime.MinValue;","tryCatchPattern":"try\n{\n    calendar.SelectedDates.Add(date);\n}\ncatch (ArgumentOutOfRangeException)\n{\n    // log and skip the invalid date\n}","preventionTips":["Use Nullable<DateTime> so 'no date' is null, never DateTime.MinValue.","Validate all parsed dates with DateTime.TryParse before use.","Check for default(DateTime) from uninitialized fields or failed conversions."],"tags":["wpf","calendar","argumentoutofrange","datetime"],"backgroundTag":"argument-out-of-range","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"}