将空值设置为默认值 [英] set the null value into default value

查看:106
本文介绍了将空值设置为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何从asp.net后面的代码中将空值设置为默认值?

(使用c#....默认值为1/1/2011)

请帮忙.

Hi,

How can I set the null value into default value from code behind of asp.net?

(using c#....default value is 1/1/2011)

Please Help.

推荐答案

嘿..
首先声明可为null的datetime变量,然后使用if语句检查您的默认值.如果是,则将其设置为null ...
hey..
first declare nullable datetime variable and then use if statement to check for ur default value. if it is yes then set it to null...
Nullable<datetime> mydate;


if ((Txtdate.Value == "1/1/2011") || (Txtdate.Value == "01/01/2011"))
        {
           mydate  = null;
        }
        else
        {
           mydate  = Convert.ToDateTime(Txtdate.Value);
        }



希望对您有帮助.



hope this help u..



在我的表格字段createdon中以日期时间格式显示,例如5/5/2011 ..在此字段中,任何null值表示...应显示在2011年1月1日中


为此,您可以为解决方案编写数据库查询..

就像在使用datatime选择列时一样,您可以这样编写..

从您的表名"中选择SELECT isull(createdon,''01/01/2011'')as CreatedOn

这将首先检查该字段是否为空,如果为空,则选择您的值,例如01/01/2011;如果不为空,则其值为您的实际值...
有关isull的更多详细信息,请参见此..
<a href="http://msdn.microsoft.com/en-us/library/ms184325.aspx">ISNULL</a>[<a href="http://msdn.microsoft.com/en-us/library/ms184325.aspx" target="_blank" title="New Window">^</a>]

<a href="http://www.w3schools.com/sql/sql_isnull.asp">ISNULL2</a>[<a href="http://www.w3schools.com/sql/sql_isnull.asp" target="_blank" title="New Window">^</a>]


您还可以使用CASE..WHEN在SELECT查询中...
有关CASE的更多详细信息.何时看到此

in my table field createdon shown in the date time format like 5/5/2011..in this field any of the value null means...it should be display into 1/1/2011


with this regard you can write your database query for your solution..

like while selecting your column with datatime you can write like this..

SELECT isnull(createdon,''01/01/2011'') as CreatedOn FROM "YOUR TABLE NAME"

this will check first that that filed is null or not, if it is null then select your value like 01/01/2011 or if it is not null then its value will be your actual value...
for more detail on isnull see this..
<a href="http://msdn.microsoft.com/en-us/library/ms184325.aspx">ISNULL</a>[<a href="http://msdn.microsoft.com/en-us/library/ms184325.aspx" target="_blank" title="New Window">^</a>]
or
<a href="http://www.w3schools.com/sql/sql_isnull.asp">ISNULL2</a>[<a href="http://www.w3schools.com/sql/sql_isnull.asp" target="_blank" title="New Window">^</a>]

OR
You can use a CASE..WHEN in SELECT Query also...
for more detail on CASE..WHEN see This
<a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx">CASE..WHEN</a>[<a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx" target="_blank" title="New Window">^</a>]<br />


<a href="http://www.1keydata.com/sql/sql-case.html">CASE..WHEN 2</a>[<a href="http://www.1keydata.com/sql/sql-case.html" target="_blank" title="New Window">^</a>]<br />


<a href="http://www.java2s.com/Code/SQL/Flow-Control/UseCASEWHENstatementinSELECT.htm">CASE..WHENE 3</a>[<a href="http://www.java2s.com/Code/SQL/Flow-Control/UseCASEWHENstatementinSELECT.htm" target="_blank" title="New Window">^</a>]


朋友你好..
首先,您需要清除您的问题,意味着您想要什么.您需要代码隐藏代码中的空值或将空值存储到数据库中.
并放一些代码,这样对我们理解您的问题很有帮助.

意思是尝试这个...

在C#

为了给空值不直接接受,但是为此您需要定义这样的变量.

Hello friend..
first of all you need to clear you question, means what you want. you want null value in codebehind code or to store null value to your database.
and also put some code, so its good for us to understand your problem.

mean while try this...

in c#

for giving null value its not accept directly, but for this you need to define variable like this.

int a = 0 // valid

int a = null // gives error

//but for assining null to it you need to declare it as

int? a = null //valid



希望对您解决问题有帮助.如果它已完全为您使用,请接受并对其进行评分.



i hope its help to you to solve your problem. if its use full for you please accept as ans and also rate it.


这篇关于将空值设置为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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