Printing text if DateTime column is NULL

Сonversation on different topics
Post Reply
robinpearce
Posts: 38
Joined: Thu Apr 02, 2009 5:54 am

Printing text if DateTime column is NULL

Post by robinpearce »

Hello,

Background to problem :-
We are using a SQL view as the source of our report and some of the columns are nullable DateTime fields. When we add this view as a datasource the columns in the Dictionary have a datatype of DateTime (NOT DateTime (nullable)). Anyway we can not change these datatypes because we automatically synchronize the views and the Dictionary using the Stimulsoft functions.

Problem :-
The problem is that we want to print, for example "The date is Empty" if the date is NULL, or the actual date, if one is present. I have spent some time attempting this by putting code in the GetValue event, and from what I can see as soon as I access the DateTime column it is throwing an exception, which presunably is being handled and then nothing is printed. I have tried comparing the column to null, DBNull, etc. The only solution I came up with is shown below.

try
{
e.Value = DateToStr(data.VALID_FROM);
}
catch (Exception)
{
e.Value = "The date is Empty";
}

What is the correct way to do this ?

Many thanks
Robin



Jan
Posts: 1265
Joined: Thu Feb 19, 2009 8:19 am

Printing text if DateTime column is NULL

Post by Jan »

Hello Robin,

You can use following expression:

Code: Select all

{myDataSource["MyDateColumn"] == DBNull.Value ? "Date is empty" : MyDataSource.MyDataColumn.ToString()}
Thank you.
robinpearce
Posts: 38
Joined: Thu Apr 02, 2009 5:54 am

Printing text if DateTime column is NULL

Post by robinpearce »

This did not work

Tried this is the GetValue event
------------------------------------
if (data.VALID_FROM == DBNull.Value)
{
e.Value = "The date is Empty";
}
else
{
e.Value = data.VALID_FROM.ToString();
}

For the following Error :Operator "==" cannot be applied to type System.DateTime & System.DBNull


Tried this in the expression
-------------------------------
Valid From : {data.VALID_FROM == DBNull.Value ? "Not specified" : data.VALID_FROM.ToString()}
Error : unreachable code


Thanks
Robin
Jan
Posts: 1265
Joined: Thu Feb 19, 2009 8:19 am

Printing text if DateTime column is NULL

Post by Jan »

Hello,

You should use following expression:

{myDataSource["MyDateColumn"] == DBNull.Value ? "Date is empty" : MyDataSource.MyDataColumn.ToString()}

Which problem you have with following expression?

Thank you.
robinpearce
Posts: 38
Joined: Thu Apr 02, 2009 5:54 am

Printing text if DateTime column is NULL

Post by robinpearce »

OK sorry it works now.

I wasn't aware how precise the syntax is.

Could you just explain the difference between the following 2 items so I understand
myDataSource["MyDateColumn"]
MyDataSource.MyDataColumn



Jan
Posts: 1265
Joined: Thu Feb 19, 2009 8:19 am

Printing text if DateTime column is NULL

Post by Jan »

Hello Robin,

Report engine does not apply type conversion when you use following expression:

Code: Select all

myDataSource["MyDateColumn"] 
You receive value of datacolumn as is. In second variant report engine try to convert value from MyDataColumn to type which specified in DataColumn in report dictionary. In our case it try to convert to DateTime type.

Code: Select all

MyDataSource.MyDataColumn
Thank you.
robinpearce
Posts: 38
Joined: Thu Apr 02, 2009 5:54 am

Printing text if DateTime column is NULL

Post by robinpearce »

Thank you so much for this. It is now completely clear.

d14pfq
Posts: 1
Joined: Tue Jul 02, 2013 2:06 pm

Re: Printing text if DateTime column is NULL

Post by d14pfq »

Know this is old but I found this useful!

One thing is thought - the change in case from: "MyDataSource" to "myDataSource" looks to be wrong - it's the use of the square bracket notation that determines the type conversion.

Also - just in case anyone else (apart from me!) gets caught out on this..

If you're doing this check, don't use the "IIF" function - use the inline if (i.e. with ? and : ) instead, due to the fact that it evaluates the arguments to the function first it won't work!
Andrew
Posts: 4107
Joined: Fri Jun 09, 2006 3:58 am

Re: Printing text if DateTime column is NULL

Post by Andrew »

Hello,

Thank you for sharing this knowledge.

Thank you.
Post Reply