Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
string dateFromtemp = dr["DateFrom"].ToString();
 DateTime dateFrom = DateTime.ParseExact(dateFromtemp, "dd/MM/yyyy", null);

error is- String was not recognized as a valid DateTime.
Posted
Updated 30-Jan-14 22:56pm
v2
Comments
Richard MacCutchan 31-Jan-14 4:53am    
What is the content of dateFromtemp?
Bojjaiah 31-Jan-14 4:57am    
can you post the result of dateFromtemp?
sp_suresh 31-Jan-14 4:59am    
see your system datetime format .For this your application must convert proper datetime format.

Don't store your dates in your database as strings.

When you do, you get problems like this, because what one user considers the "correct" way to enter dates: 01/30/12 is not valid according to your parese rules: it's US short format. Alternativemy, it could be 30/JAN/12 which also fails.

Store values in databases in the natural format: integer, decimal, or floating point for numbers, and DateTime or Date for dates. Doing anything else wastes space, and causes p[roblem when you try to use them, or read them out.
 
Share this answer
 
Comments
Pete O'Hanlon 31-Jan-14 5:08am    
There's no guarantee that the date has been stored as a string here. DateTime.ParseExact expects strings, so that would be why he's parsing it to a string first.
OriginalGriff 31-Jan-14 5:11am    
Agreed - but check his other questions:
http://www.codeproject.com/Questions/718850/How-do-I-solve-this-line
I'm going to guess that the string you are retrieving doesn't match the format dd/MM/yyyy. If, for instance, the date is in American format, you could find that it's storing todays date as 01/31/2014 - as there aren't 31 months in a year, it blows your code apart. If you need a particular format of date from your database, then retrieve it in that format from your query - this assumes that you have stored your date as a DATETIME object in the database and not as a string.
 
Share this answer
 
Assuming that the value was stored in the correct format in the db, do
DateTime dateFrom = (DateTime)dr["DateFrom"];
 
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