5

I am sorry for posting this question as it may be find silly to all, but I am not getting the exact solution.

The question is: I have a Date time picker in my project, it comes after the 3 textboxes in the form, if there is no text is entered in the textbox and enter on submit, it gives a message(validation) that data to be entered. In the same way, if the date is not selected, it should proceed further.

What is the code to do that, the code which worked for other textboxes and not working for datetimepicker control is:

       if (dateInsert.Value.ToString() = string.Empty)
        {
            MessageBox.Show("Please select date!");
            dateInsert.Focus();
            return;
        }
Piotr Justyna
  • 4,888
  • 3
  • 25
  • 40
karthik reddy
  • 454
  • 2
  • 6
  • 20
  • 1
    I would check if the Date is valid via: `DateTime _temp; DateTime.TryParse(dtPicker.Value.ToString(), out _temp);` If this is true, then the DateTime is valid. – jAC Sep 11 '13 at 10:25
  • 1
    Check if your code works after putting == in if (dateInsert.Value.ToString() == string.Empty) – user2749421 Sep 11 '13 at 11:33

5 Answers5

5

Please correct the code and see if it works

               if (dateInsert.Value.ToString() == "")
              {
                MessageBox.Show("Please select date!");
                dateInsert.Focus();
                return;
               }
user2749421
  • 284
  • 1
  • 3
  • 7
2

There is no direct solution to empty DateTimePicker. Only way to empty DateTimePicker is to set CustomFormat and then set empty space as value.

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";

Even if you do it value will be cleared in the control but if you access the value property of the control in code it will return the current date time. So your condition will always be false.

//This is always false
dateInsert.Value.ToString() = string.Empty

SOLUTION

Instead of using Value use Textin the condition.

if(dateInsert.Text = " ")
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
  • 1
    If I am using the code you provided, the datetimepicker value is going to null, I am very about it, but it solved only half problem, remaining is that, here even though I did not select the date it is getting submitted, If I did not select/change the date the value should not be submitted. I am sorry for causing confusion. – karthik reddy Sep 11 '13 at 11:02
  • @karthikreddy Are you clearing the datetimepicker value on form load ? what is the value it initially holds ? default date time ? – Kurubaran Sep 11 '13 at 11:04
  • Yes, I am clearing from form load only and value I am getting is today's date. – karthik reddy Sep 11 '13 at 11:06
  • @karthikreddy So Current date is invalid ? i mean users are only allowed to enter future or past dates ? – Kurubaran Sep 11 '13 at 11:09
  • Yes, the users should select the past date. The form should not be submitted until the user selects the date. Thanks for your patience in working with me. – karthik reddy Sep 11 '13 at 11:11
0

if you are using visual studio.....use this code to validate empty textbox

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="None"
        ErrorMessage="Select Date" ControlToValidate="dateInsert" ValidationGroup="validation"> </asp:RequiredFieldValidator>
Karthik_SD
  • 633
  • 1
  • 6
  • 22
0
if (string.IsNullOrEmpty(dateInsert.Text)
          {
            MessageBox.Show("Please select date!");
            dateInsert.Focus();
            return;
           }

hope this helps someone

D4RKCIDE
  • 3,439
  • 1
  • 18
  • 34
-1
if(datepicker.Text == " ")
messagebox.show("Please Select Date");

// this works 100 %
Alexan
  • 8,165
  • 14
  • 74
  • 101