{"record":{"id":"1a4bb0805f25f295","repo":"dotnet/wpf","slug":"throw-new-invalidoperationexception-sr-windowsdatetimepicker","errorCode":null,"errorMessage":"throw new InvalidOperationException(SR.OperationCannotBePerformed);","messagePattern":"throw new InvalidOperationException\\(SR\\.OperationCannotBePerformed\\);","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsDateTimePicker.cs","lineNumber":221,"sourceCode":"            return null;\n        }\n\n        #endregion\n\n        #region Value Pattern\n\n        // Sets the text in the edit part of the Combo\n        void IValueProvider.SetValue (string val)\n        {\n            // Make sure that the control is enabled\n            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n            {\n                throw new ElementNotEnabledException();\n            }\n\n            if (!SetValue(DateTime.Parse(val, CultureInfo.CurrentCulture)))\n            {\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }\n        }\n\n        // Request to set the value that this UI element is representing as a string\n        string IValueProvider.Value\n        {\n            get\n            {\n                int cLen = Misc.ProxySendMessageInt(_hwnd, NativeMethods.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);\n                if (cLen > 0)\n                {\n                    cLen++;\n                    StringBuilder sb = new StringBuilder (cLen);\n\n                    Misc.ProxySendMessage(_hwnd, NativeMethods.WM_GETTEXT, new IntPtr(cLen), sb);\n\n                    return sb.ToString ();\n                }","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsDateTimePicker.cs#L203-L239","documentation":"After passing the enabled check, SetValue parses the string with DateTime.Parse (current culture) and calls the private SetValue(DateTime). If that native operation fails (returns false), the proxy throws InvalidOperationException with SR.OperationCannotBePerformed, indicating the control refused the value.","triggerScenarios":"Calling ValuePattern.SetValue with a string that fails DateTime.Parse under the current culture (wrong format, e.g. \"2024-01-02\" in a MM/dd/yyyy culture), or a date the DateTimePicker natively rejects (out of MinDate/MaxDate range).","commonSituations":"Culture mismatch between test machine and app (locale-dependent date strings), setting dates outside the control's configured range, passing time-only or empty strings.","solutions":["Format the value using CultureInfo.CurrentCulture (e.g. DateTime.Now.ToString(\"d\", CultureInfo.CurrentCulture)) so DateTime.Parse succeeds.","Ensure the date is within the control's MinDate/MaxDate range.","Use UIA SendKeys on the control instead of ValuePattern if the native SetValue path keeps failing."],"exampleFix":"// before\nvaluePattern.SetValue(\"2024-01-02\");\n// after\nstring s = new DateTime(2024, 1, 2).ToString(\"d\", CultureInfo.CurrentCulture);\nvaluePattern.SetValue(s);","handlingStrategy":"validation","validationCode":"DateTime dt;\nif (!DateTime.TryParse(text, CultureInfo.CurrentCulture, DateTimeStyles.None, out dt)) throw new FormatException(\"Value must be a culture-valid date: \" + text);\nif (dt < picker.MinDate || dt > picker.MaxDate) throw new ArgumentOutOfRangeException(nameof(text));","typeGuard":null,"tryCatchPattern":"try { valuePattern.SetValue(text); } catch (InvalidOperationException) { /* correct format/range and retry */ }","preventionTips":["Always format dates with CultureInfo.CurrentCulture","Respect MinDate/MaxDate of the control","Pin test-machine locale to avoid culture drift"],"tags":["uiautomation","datetime","culture"],"backgroundTag":"unsupported-operation","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"}