0

How to declare the datetimepicker value as null? This is my code.

dateTimePicker.Value.ToString() == string.Empty

could you please edit my code. because it is unrunnable.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • 1
    `DateTimePicker.Value` is not a nullable type, you can't assign `null` to it – Mathias R. Jessen Feb 15 '16 at 02:15
  • 1
    Possible duplicate of [how to set datetimepicker with null value if date not selected(c# winforms)](http://stackoverflow.com/questions/2983563/how-to-set-datetimepicker-with-null-value-if-date-not-selectedc-winforms) – Ageonix Feb 15 '16 at 03:28
  • are the title and the post relevant? – Kumar C Feb 15 '16 at 04:44

3 Answers3

0

You cannot set null value there. DateTimePicker.Value is not nullable and its minimum value is 1/1/1753 00:00:00

If you are going to assign null just to check if it is initialized. You can check the value against MinDate property.

Read more here.

Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131
0

Because the datatype of DateTimePicker.Value property is DateTime, just checking whether the year is greater than 1900 would give only valid dates. You can change the year value to something more recent if your application demands so, but anything after 1900 is generally considered a valid date.

dateTimePicker.Value.Year > 1900
Soham Dasgupta
  • 5,061
  • 24
  • 79
  • 125
0

/* Here is a Method I wrote to validate expiry id ID in C# when selecting Date in DateTime Picker for Windows Forms. By demonguru18 aka techhowdy.com */

private bool isIDExpired()
        {
            DateTime objDateTime = DateTime.Parse(dtpIDExpiry.Value.ToString());
            DateTime today = DateTime.Today;
            int day = objDateTime.Day - today.Day;
            int month = objDateTime.Month - today.Month;
            int year = objDateTime.Year - today.Year;
            if (day == 0 && month == 0 && year == 0)
            {
                MessageBox.Show("Your ID is Expiring Today. Please Provide new ID Expiry Date.");
                return false;
            }
            else if (year < 0)
            {
                MessageBox.Show("Your ID has already Expired. Please Provide valid ID Expiry Date.");
                return false;
            }
            else if (month < 0 &&  year < 0)
            {
                MessageBox.Show("Your ID has already Expired. Please Provide valid ID Expiry Date.");
                return false;
            }

            else if (month == 0 && day < 0 && year == 0)
            {
                MessageBox.Show("Your ID has already Expired. Please Provide valid ID Expiry Date.");
                return false;
            }

            return true;
        }`