DateTime as empty String or null ?How to check?

帅比萌擦擦* 提交于 2020-01-01 15:36:30

问题


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)

回答1:


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
}



回答2:


=IIf(FormatDateTime(Fields!D_DateTime.Value,2)=CDate("1/1/0001"),"",FormatDateTime(Fields!D_DateTime.Value,2))

Thanks a lot ,i think this fixes my problem.




回答3:


As datetime is a struct rather than class i.e. 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

i.e.

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



回答4:


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.




回答5:


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);


来源:https://stackoverflow.com/questions/8985854/datetime-as-empty-string-or-null-how-to-check

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!