Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
if (Convert.ToDateTime(txtTodate.Text).Date < DateTime.Now.Date && Convert.ToDateTime(txtTodate.Text).Date < Convert.ToDateTime(txtdesigndate.Text).Date)


How to resolve conversion error?

What I have tried:

I am trying to resolve conversion error
Posted
Updated 22-Feb-16 23:37pm
v2
Comments
glen205 23-Feb-16 5:08am    
Please provide a specific example of the text entered into the two TextBox controls: txtTodate and txtdesigndate.

Please also supply the exact text of the "conversion error" - i.e. can you give the stacktrace or exception message?

1 solution

Start by not using Convert.ToDateTime.
Instead, use DateTime.TryParse:
C#
DateTime toDate;
if (!DateTime.TryParse(txtToDate.Text, out toDate))
   {
   ... Report problem to user so he can  correct it
   return;
   }
toDate = toDate.Date;
DateTime designDate;
if (!DateTime.TryParse(txtdesignDate.Text, out designDate))
   {
   ... Report problem to user so he can  correct it
   return;
   }
designDate = designate.Date;
if (todate < DateTime.Now.Date && todate < designDate)
   {
   ...
 
Share this answer
 

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