Click here to Skip to main content
15,066,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, 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 whcih worked for other textboxes and not working for dateimepicker control is:

C#
if (dateInsert.Value.ToString() = string.Empty)
           {
               MessageBox.Show("Please select date!");
               txtDescription.Focus();
               return;
           }
Posted
Comments
[no name] 11-Sep-13 6:25am
   
At the very least you are missing an =. if (dateInsert.Value.ToString() == string.Empty)
Other than that, I would have no idea what "not working" means.
karthik reddy mereddy 11-Sep-13 6:36am
   
I am sorry for the typo, I am using if (dateInsert.Value.ToString() == string.Empty).
When the page is loading it is coming with the todays date in datetimepicker control. What requieremnt is, I should definitely click or change the date to previous on the datetimepicker control in order to submit.
[no name] 11-Sep-13 7:27am
   
Okay so set the date picker to the previous date. What is the problem there? What does that have to do with anything in your posting?
Love 2 code 11-Sep-13 6:25am
   
Hi, the out of the box DateTimePicker control does not allow null values, so you could google for a nullable DateTimePicker control.

Have a look here: DateTimePicker.Value[^] property, which gets or sets the date/time value assigned to the control.

If you would like to check if the value of DateTimePicker has a proper datetime value, please, use DateTime.TryParse[^] method.

C#
DateTime dtValue = DateTime.Now();
bool bres = DateTime.TryParse(dateInsert.Value.ToString(), dtValue);

if (bres==true) 
//proper date
else
//unable to convert!


More:
Parsing Date and Time Strings[^]
   
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePicker1.CustomFormat = " ";
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
        }

        private void dateTimePicker1_MouseEnter(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Short;
        }
    }
}
   
Comments
karthik reddy mereddy 12-Sep-13 2:25am
   
If we use dateTimePicker1.CustomFormat = " "; the control is becoming null and on submitting it is taking the default value. My requirement is it should give a error message and should not accept the Null value.
karthik reddy mereddy 12-Sep-13 2:26am
   
Actually, by using "blank space i the string" it is taking that space as value and displaying as null, but actually it is not becoming Null.
U mean to say every time user have to change the default date ?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900