Printing text if DateTime column is NULL
-
- Posts: 38
- Joined: Thu Apr 02, 2009 5:54 am
Printing text if DateTime column is NULL
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
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
Printing text if DateTime column is NULL
Hello Robin,
You can use following expression:
Thank you.
You can use following expression:
Code: Select all
{myDataSource["MyDateColumn"] == DBNull.Value ? "Date is empty" : MyDataSource.MyDataColumn.ToString()}
-
- Posts: 38
- Joined: Thu Apr 02, 2009 5:54 am
Printing text if DateTime column is NULL
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
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
Printing text if DateTime column is NULL
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.
You should use following expression:
{myDataSource["MyDateColumn"] == DBNull.Value ? "Date is empty" : MyDataSource.MyDataColumn.ToString()}
Which problem you have with following expression?
Thank you.
-
- Posts: 38
- Joined: Thu Apr 02, 2009 5:54 am
Printing text if DateTime column is NULL
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
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
Printing text if DateTime column is NULL
Hello Robin,
Report engine does not apply type conversion when you use following expression:
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.
Thank you.
Report engine does not apply type conversion when you use following expression:
Code: Select all
myDataSource["MyDateColumn"]
Code: Select all
MyDataSource.MyDataColumn
-
- Posts: 38
- Joined: Thu Apr 02, 2009 5:54 am
Printing text if DateTime column is NULL
Thank you so much for this. It is now completely clear.
Re: Printing text if DateTime column is NULL
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!
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!
Re: Printing text if DateTime column is NULL
Hello,
Thank you for sharing this knowledge.
Thank you.
Thank you for sharing this knowledge.
Thank you.