我想将null值传递给数据库。 [英] I want to pass null value into database.

查看:114
本文介绍了我想将null值传递给数据库。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将null值传递给数据库。

我将属性值设置为null



private DateTime? dtStartDate;

公共DateTime? StartDate

{

set {this.dtStartDate = value; }

get {return this.dtStartDate; }

}





objProjects.StartDate = Convert.ToDateTime(txtStartDate.Text)== null? null:Convert.ToDateTime(txtStartDate.Text);



但是这里我得到了一个错误

条件表达式的类型无法确定,因为那里在'< null>'和'System.DateTime'之间没有隐式转换

任何人都可以给出答案

I want to pass null value into database.
i set the property values null like

private DateTime? dtStartDate;
public DateTime? StartDate
{
set { this.dtStartDate = value; }
get { return this.dtStartDate; }
}


objProjects.StartDate = Convert.ToDateTime(txtStartDate.Text) == null ? null : Convert.ToDateTime(txtStartDate.Text);

But here i got an error
Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'
Can anyone give the answer please

推荐答案

试试以下:

Try the following :
objProjects.StartDate = null;
if(txtStartDate.Text != "")
   objProjects.StartDate = Convert.ToDateTime(txtStartDate.Text);


另一个解决方案可能是:

Another solution could be:
DateTime? value = objProjects.StartDate = null;
if (DateTime.TryParse(txtStartDate.Text, out value) {
   objProjects.StartDate = value;
}


嗨Mehdi Gholam我已经和你的一样做了并得到了答案但我的qts是什么错误在这个



objProjects.StartDate = Convert.ToDateTime(txtStartDate.Text)== null?null:Convert.ToDateTime(txtStartDate.Text);
Hi Mehdi Gholam i already done the same as yours and got the answer but my qts is whats the error in this

objProjects.StartDate = Convert.ToDateTime(txtStartDate.Text) == null ? null : Convert.ToDateTime(txtStartDate.Text);


这篇关于我想将null值传递给数据库。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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