引发异常-String未被识别为有效的DateTime-- [英] Exception raised -String was not recognized as a valid DateTime.-

查看:92
本文介绍了引发异常-String未被识别为有效的DateTime--的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我面临的问题是关于

字符串未被识别为有效的DateTime。并且异常类型格式异常被捕获激活当我试图点击cellcontent时抛出gridview列的Click事件



代码 -

objCardDet.SubscribeDate = DateTime.Parse(dtGvCardDetails。行[e.RowIndex] .Cells [4] .Value.ToString());



请帮助我..

Thanx in advance

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

推荐答案

这是因为您正在解析字符串而未指定表示时间的字符串的确切格式,这与默认情况下预期的不同。请使用 System.String.DateTime.ParseExact 之一准确指定格式.System.String.DateTime.TryParseExact

http://msdn.microsoft.com/en-us /library/system.datetime.aspx [ ^ ]。



请记住 ParseExact 抛出异常是识别失败,所以您应该处理异常,或使用 TryParseExact 表单。



格式说明符请参阅:

http://msdn.microsoft.com/en-us/library/az4se3k1 .aspx [ ^ ] ,

http://msdn.microso ft.com/en-us/library/8kb3ddd4.aspx [ ^ ]。



-SA
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


这篇关于引发异常-String未被识别为有效的DateTime--的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆