当我提交空日期时,我收到错误,因为此字符串未被识别为有效日期时间 [英] When I Submit Empty Date I Got Error Like This String Was Not Recognized As A Valid Datetime
本文介绍了当我提交空日期时,我收到错误,因为此字符串未被识别为有效日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public ( string DateOfBirth);
{
manageusers.DateOfBirth1 = Convert.ToDateTime(DateOfBirth);
}
当我传递空日期
默认它将显示为此
manageusers.DateOfBirth1 = {1/1/0001 12 : 00 : 00 AM}
我想将日期时间指定为空
i声明这样的属性
public DateTime DateOfBirth
{
set {_DateOfBirth = value ; }
get { return _DateOfBirth; }
}
如果传递空日期空值插入,如果我传递日期日期将被插入。 ..我可以编写代码.......
解决方案
参考:如何在C#.net 中为nulltime分配空值[ ^ ]
是的...我解决了我的自我...........
SqlDateTime datenull;
datenull = SqlDateTime.Null;
if ( manageusers.DateOfBirth == )
{
vcmd.Parameters.AddWithValue ( @ DateOfBi rth,datenull);
}
else
{
vcmd.Parameters.AddWithValue( @ DateOfBirth,manageusers.DateOfBirth);
}
i将属性datetime更改为字符串
< span class =code-keyword> public stringDateOfBirth
{
set {_DateOfBirth = 值跨度>; }
get { return _DateOfBirth; }
}
public (string DateOfBirth);
{
manageusers.DateOfBirth1 = Convert.ToDateTime(DateOfBirth);
}
when i pass empty date
default it will display like this
manageusers.DateOfBirth1 = {1/1/0001 12:00:00 AM}
I want to assign datetime to null
i declared property like this
public DateTime DateOfBirth
{
set { _DateOfBirth = value; }
get { return _DateOfBirth; }
}
if pass empty date null value inserted and if i pass date date will be inserted...how can i write the code.......
解决方案
Refer: how to assign null value to datetime in C#.net[^]
yep... i solved my self...........
SqlDateTime datenull;
datenull = SqlDateTime.Null;
if (manageusers.DateOfBirth == "") { vcmd.Parameters.AddWithValue("@DateOfBirth", datenull); } else { vcmd.Parameters.AddWithValue("@DateOfBirth", manageusers.DateOfBirth); }
i changed property datetime to string
public stringDateOfBirth { set { _DateOfBirth = value; } get { return _DateOfBirth; } }
这篇关于当我提交空日期时,我收到错误,因为此字符串未被识别为有效日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文