0
votes

I am trying to validate two data of birth text box fields if the member does not enter in these two boxes an error message will popped out like "pls enter valid date format"

by using the following code

       if (tbStartDate.Text != "0" && tbEndDate.Text != "0")
       { 

        string dob = tbStartDate.Text;
        DateTime dv = DateTime.Parse(dob);
        string format1 = dv.ToString("yyyy-MM-dd");
        string dob2 = tbEndDate.Text;
        DateTime dt2 = DateTime.Parse(dob2);
        string format2 = dt2.ToString("yyyy-MM-dd");
     }
      else
     {
            MessageBox.Show("pls enter valid date ");

     }

when i am trying to save with out entering the textbox values it was showing error like this "String was not recognized as a valid DateTime".

can any one help on this....

this is modified code still it was showing error at Datetime DV;

         private void btnCTimetablessave_Click(object sender, EventArgs e)
    {
        string dob = tbStartDate.Text;
        DateTime dv;
        if (!DateTime.TryParse(dob, out dv))
        {

            MessageBox.Show("pls enter a valid  start date");
            return;
        }

        string format1 = dv.ToString("yyyy-MM-dd");
        string dob2 = tbEndDate.Text;
        DateTime dt2;
        if (!DateTime.TryParse(dob2, out dt2))
        {
            MessageBox.Show("pls enter valid end date");
            return;
        }

        string format2 = dt2.ToString("yyyy-MM-dd");

still it was showing error like this "String was not recognized as a valid DateTime" at datetime Dv;

and this is modified code third time

       private void btnCTimetablessave_Click(object sender, EventArgs e)
      {
         string dob = tbStartDate.Text;
        DateTime dv;
        if (!DateTime.TryParse(dob, out dv))
        {

            MessageBox.Show("pls enter a valid  start date");
            return ;
        }

        string format1 = dv.ToString("yyyy-MM-dd");
        string dob2 = tbEndDate.Text;
        DateTime dt2;
        if (!DateTime.TryParse(dob2, out dt2))
        {
            MessageBox.Show("pls enter valid end date");
            return;
        }

        string format2 = dt2.ToString("yyyy-MM-dd");
     }
5

5 Answers

5
votes

Don't use DateTime.Parse. Use DateTime.TryParse instead. The result is a boolean value that returns true if the parsing was successfully.

Update As requested in your comment:

DateTime dv;
if(!DateTime.TryParse(dob,out dv)){
   MessageBox.Show("Please enter a valid date");
   return;
}
....

Cleary, you also can use Parse and catch the exception, but this would be bad style. Better to use TryParse. I have only checked for one date value. You can extend the logic for your needs. Hope this helped.

1
votes

It is because an empty textbox returns an empty string when you access the Text property and DateTime.Parse() will throw that exception if passed an empty string.

0
votes

Check using string.IsNullOrEmpty(tbStartDate.Text) whether its blank or not.

I would recommend using a DatetimePicker for selecting the date's which will return you a valid DateTime value which you can use for further processing

0
votes

You have to use Datetime.TryParse like this

if(!DateTime.TryParse(dob))
0
votes

I suggest you to use try catch block to catch FormatException so that if user doesn't enter date in proper format it will catch the exception and give message of your choice

try
{
    string dob = tbStartDate.Text;
    DateTime dv = DateTime.Parse(dob);
    string format1 = dv.ToString("yyyy-MM-dd");
    string dob2 = tbEndDate.Text;
    DateTime dt2 = DateTime.Parse(dob2);
    string format2 = dt2.ToString("yyyy-MM-dd");
}
catch (System.FormatException)
{
    MessageBox.Show("pls enter valid date format");
}

OR

using DateTimePicker is Always a better and user friendly approach