我有一个asp.net应用程序,我有一个文本框输入datetime值,它保存在数据库中.
现在,当我尝试检索日期时,如果日期为空,则显示1/1/0001 12:00:00 AM.
this.FirstReceivedDate = DateTime.Parse(dr["FirstReceivedDate"].ToString());
道歉,要求改变了.如果FirstReceivedDate为null,我想显示空白(”).
我怎样才能做到这一点?
解决方法:
您可以尝试以下内容:
DateTime date = DateTime.Parse(dr["FirstReceivedDate"].ToString());
this.FirstReceivedDate = date != DateTime.MinValue ? date : DateTime.Now;
因为它正确解析,这应该工作.
或者,如果您想要一个日期为null的值,您可以尝试这样的事情:
DateTime date;
if(DateTime.TryParse(dr["FirstReceivedDate"], out date))
this.FirstReceivedDate = date != DateTime.MinValue ? date : DateTime.Now;
else
this.FirstReceivedDate = DateTime.Now; // or whatever you want to do if "FirstReceivedDate" is not a valid date.