1
votes

I have a datetime picker dtOtMonth which I need to display only month and the year.So I have formatted it as follows in the event of form load.

dtOtMonth.Format = DateTimePickerFormat.Custom;
dtOtMonth.CustomFormat = "MM/yyyy";

But when I try to edit the date with up/down arrows in run time it gives me the following error.

Year, Month, and Day parameters describe an un-representable DateTime.

System.ArgumentOutOfRangeException was unhandled Message="Year, Month, and Day parameters describe an un-representable DateTime." Source="mscorlib" StackTrace: at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day) at System.Windows.Forms.DateTimePicker.SysTimeToDateTime(SYSTEMTIME s) at System.Windows.Forms.DateTimePicker.WmDateTimeChange(Message& m) at System.Windows.Forms.DateTimePicker.WmReflectCommand(Message& m) at System.Windows.Forms.DateTimePicker.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m) at System.Windows.Forms.Control.WmNotify(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.GroupBox.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message& m) at System.Windows.Forms.Control.DefWndProc(Message& m) at System.Windows.Forms.Control.WmKeyChar(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.DateTimePicker.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at Attendence_Module.Program.Main() in E:\HR System\HRProject\Attendence_Module\Attendence_Module\Program.cs:line 17 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

3
I opened a winforms project - dragged a datetimepicker - copied your code in the Form_Load event, and all works. can it be some other thing in your code?G.Y
See the following stackoverflow.com/a/1608804/1071091 it might help youCaptain0
WOW found something.. VS-2005??? make haste and switch to 2015!!!G.Y

3 Answers

1
votes

As OP discovered, the error occurrs when only the month changes, it is possible to change to a date that doesn't exist (e.g. "February 31st").

There are a couple of ways to prevent the error. One way is to ensure that the DateTimePicker always uses the first day of the month. Here's a quick example that makes sure the Day property of the value of the DateTimePicker is always set to 1. In this example, I use a read-only textbox to show the output:

public MyTestForm()
{
    InitializeComponent();

    SetDateTimePickerFirstOfMonth();
    UpdateTextbox();
}

private void SetDateTimePickerFirstOfMonth()
{
    DateTime current = dtOtMonth.Value;
    if (current.Day != 1)
    {
        DateTime newValue = new DateTime(current.Year, current.Month, 1);
        dtOtMonth.Value = newValue;
    }
}

private void UpdateTextbox()
{
    int year = dtOtMonth.Value.Year;
    int month = dtOtMonth.Value.Month;

    txtResult.Text = string.Format("Year is {0} and month is {1}", year, month);
}

private void dtOtMonth_ValueChanged(object sender, EventArgs e)
{
    SetDateTimePickerFirstOfMonth();
    UpdateTextbox();
}

Using this code, I can still use the arrow keys to change the year or month in the DateTimePicker, and it will ensure that the DateTimePicker value is always set to the first of the selected month, even if a specific date is selected.

Here's a screenshot, of the demo application. The textbox changes its value as soon as the DateTimePicker's value is changed.

Example Winform

1
votes

It needs a day to increment a value. Seems like it's parsing a date and can't do that as there is no day.

1
votes

At last I found the bug.

In a date time picker if not we initialize the default date even though we take month and year only, inside it takes default date(here today's date in my case).

So when I change the month, the selected date remain same for each month.Therefore today is 30th March 2016 and when I try to decreases the month by one it is February and the date time picker try to initialize the date 30th to February and it generates the error.

In my case as far as I use month and year I leave the date apart.

I solved this by initializing the date for 1 st of the month on form load event where each and every month has a day1 st (one) not like 29th,30th,31th

dtOtMonth.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
dtOtMonth.Format = DateTimePickerFormat.Custom;
dtAdvanceStartDate.CustomFormat = "MM/yyyy";

Here are some very useful links where I found this is a .NET bug.

https://connect.microsoft.com/VisualStudio/feedback/details/553375/system-argumentoutofrangeexception-year-month-and-day-parameters-describe-an-un

https://social.msdn.microsoft.com/Forums/windows/en-US/69b10b5b-a034-426e-a045-2bed8a1637a3/bug-in-datetimepicker-control?forum=winforms