{"record":{"id":"88a27d36c6dcdc33","repo":"dotnet/wpf","slug":"sr-format-sr-invaliddatatypeofparameter-datetime-or-string","errorCode":null,"errorMessage":"SR.Format(SR.InvalidDataTypeOfParameter, \" DateTime or string \")","messagePattern":"SR\\.Format\\(SR\\.InvalidDataTypeOfParameter, \" DateTime or string \"\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs","lineNumber":1140,"sourceCode":"            if (val is DateTime)\n            {\n                systemTime = CreateSystemTimeFromDateTime((DateTime)val);\n                fValid = true;\n            }\n            // PerSharp/PreFast will flag this as warning 6507/56507:\n            // Prefer 'string.IsNullOrEmpty(valString)' over checks for null and/or emptiness.\n            // Null and Empty string mean different things here.\n#pragma warning suppress 6507\n            else if (valString != null)\n            {\n                systemTime = CreateSystemTimeFromDateTime(\n                                System.DateTime.Parse(valString, CultureInfo.InvariantCulture));\n                fValid = true;\n            }\n\n            if (!fValid)\n            {\n                throw new ArgumentException (\n                    SR.Format(SR.InvalidDataTypeOfParameter, \" DateTime or string \"), \"val\");\n            }\n\n            return systemTime;\n        }\n\n        private int CalendarIndexFromPoint (int x, int y)\n        {\n            NativeMethods.Win32Rect rcMonth;\n            CalcPositions (0, out rcMonth);\n\n            NativeMethods.Win32Rect rc = new NativeMethods.Win32Rect ();\n            NativeMethods.Win32Rect rcSingleMonth = new NativeMethods.Win32Rect ();\n\n            if (!Misc.GetClientRectInScreenCoordinates(_hwnd, ref rc))\n            {\n                return -1;\n            }","sourceCodeStart":1122,"sourceCodeEnd":1158,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs#L1122-L1158","documentation":"The WindowsCalendar helper that converts a value into a Win32 SYSTEMTIME accepts only DateTime or string inputs; if 'val' is neither (and string parsing fails to yield a valid date), it throws ArgumentException(SR.Format(SR.InvalidDataTypeOfParameter, \" DateTime or string \")) with parameter name 'val'.","triggerScenarios":"Calling the calendar's value-conversion helper (used when setting the selected date) with a non-DateTime, non-string object such as int, long, or null.","commonSituations":"Passing numeric date representations (ticks, file times) or binding values of unexpected runtime types; strings that are not culture-invariant-parseable dates also funnel here.","solutions":["Convert the value to DateTime (or an invariant-format date string) before passing it.","Ensure strings parse with DateTime.Parse(value, CultureInfo.InvariantCulture).","Catch ArgumentException and inspect val's runtime type to route the conversion correctly."],"exampleFix":"// before\ncalendar.SetValue(dateTicks); // long\n// after\ncalendar.SetValue(new DateTime(dateTicks));","handlingStrategy":"type-guard","validationCode":"if (!(val is DateTime || (val is string s && DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out _))))\n    throw new ArgumentException(\"val must be DateTime or invariant date string\", nameof(val));","typeGuard":"bool IsValidDateValue(object val) => val is DateTime || (val is string s && DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out _));","tryCatchPattern":"try { calendar.SetValue(val); }\ncatch (ArgumentException ex) when (ex.ParamName == \"val\") { val = ConvertToDateTime(val); retry(); }","preventionTips":["Coerce values to DateTime before passing","Use invariant culture when formatting date strings","Validate runtime type at call boundaries"],"tags":["uiautomation","argument-exception","type-mismatch"],"backgroundTag":"invalid-argument-format","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"}