DateTime为空String还是null?如何检查?

时间:2012-01-24 11:15:24

标签: c# asp.net datetime reporting-services ssrs-2008

问:

如果datetime为null,我想检查DateTime对null value以清空报表中的单元格。但我不知道如何执行此操作:如果它1/1/0001,则显示为=FormatDateTime(Fields!D_DateTime.Value,2) 是null。我希望它是空单元格。

这是我的数据集中的数据类型:

enter image description here

这是我的列的表达式值:

{{1}}

4 个答案:

答案 0 :(得分:9)

正如我在评论中告诉你的那样,你应该检查你的日期是DateTime.MinValue(日期可以假定的最小值,也就是01/01/0001)。

if (your_date_property == DateTime.MinValue)
{
    // Do what you need
}

答案 1 :(得分:1)

因为datetime是结构而不是类,即值类型而不是引用类型;它必须用一些值初始化。它不能具有空值。

因此,要检查默认值,您应该检查与DateTime.MinValue

的相等性

即。

if(D_DateTime.Value == DateTime.MinValue)
{
   //write code here for default value handling
}

答案 2 :(得分:1)

将数据集中的字段类型(rd:TypeName)更改为System.Nullable (Of System.DateTime)。然后,您只需测试=Fields!D_DateTime.Value Is Nothing

答案 3 :(得分:1)

像@Marco建议您可以查看MinValue。如果您想将NULL传递给可以为null的参数,则可以对reportviewer参数使用以下代码。

<强> Vb.Net

Dim rpFrom As New ReportParameter("FromDate", New String() {Nothing}, False)

<强> C#

ReportParameter rpFrom = new ReportParameter("FromDate", new string[] { null }, false);