Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi Guys,
I am facing the problem regarding
"String was not recognized as a valid DateTime".and type of exception "Format Exception was caught" excpetion throws when i am trying to click cellcontent Click event of gridview column

the code -
objCardDet.SubscribeDate = DateTime.Parse(dtGvCardDetails.Rows[e.RowIndex].Cells[4].Value.ToString());

please help me..
Thanx in advance
Posted
Comments
indrajeet jadhav 10-Apr-13 0:50am    
and I already try objCardDet.SubscribeDate=convert.TodateTime(dtGvCardDetails.Rows[e.RowIndex].Cells[4].Value.ToString());
Sergey Alexandrovich Kryukov 10-Apr-13 0:56am    
Don't try anything. Use the debugger and figure out what's going on.
—SA
indrajeet jadhav 10-Apr-13 1:03am    
objCardDet.SubscribeDate = {01-01-0001 AM 12:00:00} this is it..showing in after debugging..in that code line.n look at the date..

1 solution

This is because you are parsing the string without specifying exact format of the string representing time, which is not the same which is expected by default. Please specify the format exactly using one of the System.String.DateTime.ParseExact or System.String.DateTime.TryParseExact:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

Remember that ParseExact throws exception is the recognition is failed, so you should either handle the exception, or use the TryParseExact form.

For format specifiers, please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

—SA
 
Share this answer
 

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