Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 22-Feb-16 23:37pm
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:
DateTime toDate;
if (!DateTime.TryParse(txtToDate.Text, out toDate))
   ... Report problem to user so he can  correct it
toDate = toDate.Date;
DateTime designDate;
if (!DateTime.TryParse(txtdesignDate.Text, out designDate))
   ... Report problem to user so he can  correct it
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