Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application i have fromDate & ToDate Calender Control.When selecting Date To date is greater than or equal to FromDate. but when Selecting To date greater or equal to FromDate i got Exception "String was not recognized as a valid DateTime."
On Following line on code.
if any idea suggest me.

sqlParam.Value = Convert.ToDateTime(sqlParam.Value).ToString("yyyy-MM-dd");
Posted
Updated 25-Jul-11 19:31pm
v2

First fix the datetime format at input point from where you are inputting date.

Use DateTimeFormatInfo...

System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
dateInfo.ShortDatePattern = "yyyy-MM-dd";//the format of input point
sqlParam.Value = Convert.ToDateTime(sqlParam.Value, dateInfo)
 
Share this answer
 
If you are working with DateTime and want to compare them. Then do not convert them to a String to do a compare.
Just compare the DateTime with the other DateTime.

I can see that there is a sqlParam, if it is a SqlParameter[^] Class, you do not need to convert the DateTime to a string.
 
Share this answer
 
Try This and tell if it worked

sqlParam.Value = DateTime.Parse(sqlParam.Value.ToShortDateString);
 
Share this answer
 
v2

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