Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
DateTime dateofbirth = Convert.ToDateTime(txt_DOB.Text);
  DateTime Weddingday  = Convert.ToDateTime(txt_wedding.Text);

    if (dateofbirth.Year !=1900)
    SendSMS(txt_DOB.Text.Trim());
    else
   if (Weddingday.Year != 1900)
   SendSMS(txt_wedding.Text.Trim());



run this code error shows "String Was Not Recognized a Valid Date Time";

from my above code what is the mistake or error or problem.please find out & help me.

Rgds,
narasiman P
Thanks,
Code Project.
Posted
Updated 29-Dec-12 3:25am
v2
Comments
Richard MacCutchan 29-Dec-12 9:00am    
How can we guess? Look at the content of your text fields to see why they are not being recognised.
Oleksandr Kulchytskyi 29-Dec-12 9:01am    
As far as i remember you have asked such question!

1 solution

hen you use Convert.ToDateTime, it tries to parse the input string using the Culture settings of the computer that your code is running on. If the string input by the user is not in that format (for example, the user enters "29 Dec 2012" and your Culture is set to US and expects "12/29/2012") then you will get an exception. You will get the same problem if the user enters an invalid date such as "sdfs/wewe/22334"

There are two solutions: use TryParseExact[^] instead, and report the problem or the user, or prevent the user from entering a wrong date altogether by using a DateTimePicker[^] instead.

Personally, I prefer to prevent problems, and present the user with a "nicer" input method at the same time, so I use a DateTimePicker whenever possible.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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