Date time picker validations

早过忘川 提交于 2019-11-30 05:28:23

Please correct the code and see if it works

               if (dateInsert.Value.ToString() == "")
              {
                MessageBox.Show("Please select date!");
                dateInsert.Focus();
                return;
               }

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 = " ")

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>
if (string.IsNullOrEmpty(dateInsert.Text)
          {
            MessageBox.Show("Please select date!");
            dateInsert.Focus();
            return;
           }

hope this helps someone

Muhammad Awais Butt
if(datepicker.Text == " ")
messagebox.show("Please Select Date");

// this works 100 %
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!