简体   繁体   中英

DateTime as empty String or null ?How to check?

Q:

I want to check the DateTime against null value to empty the cell in my report if the datetime is null.but i don't know how to do this :it appears like this 1/1/0001 if it was null.and i want it to be empty cell.

This is the datatype in my dataset :

在此输入图像描述

and this is the expression value of my column :

=FormatDateTime(Fields!D_DateTime.Value,2)

As I told you in my comment, you should check if your date is DateTime.MinValue (the minimum value a date can assume, which is exactly 01/01/0001).

if (your_date_property == DateTime.MinValue)
{
    // Do what you need
}
=IIf(FormatDateTime(Fields!D_DateTime.Value,2)=CDate("1/1/0001"),"",FormatDateTime(Fields!D_DateTime.Value,2))

非常感谢,我认为这解决了我的问题。

Change the type of the field in the dataset ( rd:TypeName ) to System.Nullable (Of System.DateTime) . Then you can simply test =Fields!D_DateTime.Value Is Nothing .

Like @Marco suggested you can check for MinValue . And if you want to pass NULL to the nullable parameter, you can use the following code for reportviewer parameter.

Vb.Net

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

C#

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

As datetime is a struct rather than class ie a value type rather than a reference type; it must be initialized with some value. It cannot have null values.

Hence to check the default value you should check the equality with DateTime.MinValue

ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM