Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I write this code then it shows error :
DateTime.TryParse(DTLocal.Rows[0]["Date"].ToString())
But the following does not show any error :
DateTime Date=DateTime.Parse(DTLocal.Rows[0]["Date"].ToString());
Please explain what is the reason for such conversion.
Posted

1 solution

You have not told us the error you got, but I am pretty sure it is a compile time one, since TryParse is expecting a second out bound parameter (see: http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx[^]).
The difference is there in on the msdn manual page:
Quote:
The DateTime.TryParse(String, DateTime) method is similar to the DateTime.Parse(String) method, except that the TryParse(String, DateTime) method does not throw an exception if the conversion fails.
See this short sample code:
C#
DateTime theDT1;
bool result1 = DateTime.TryParse("2013.01.01", out theDT1); // date is converted, resutl is true

DateTime theDT2;
bool result2 = DateTime.TryParse("not a date", out theDT2); // date is not converted, result is false

DateTime theDT3 = DateTime.Parse("not a date"); // date is not converted, exception thrown
 
Share this answer
 
Comments
Sumon562 28-Apr-13 2:11am    
Thank you Mr.Zoltán Zörgő for your kind response
Zoltán Zörgő 28-Apr-13 2:16am    
You are welcome. If you found my answer helpful, feel free to accept it.

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