tags:

views:

91

answers:

1

Hi,

I am trying to format the date time (yyyy/MM/dd) in a repeater which is binded to an ObjectDataSource as shown

(THIS WORKS)

<%# ((MyType)Container.DataItem).CreateDateTime.ToString("yyyy/MM/dd")%>

(THIS DOES`NT WORKS)

<%# String.Format("{0:yyyy/MM/dd}",((MyType)Container.DataItem).UpdateDateTime)%>

I want to have both things working because sometimes the property UpdateDateTime is null, in such cases the second line of code handles smart.

Thank you for the help in advance.

+1  A: 

I just tested you second line of code that you say doesn't work and it works fine when UpdateDateTime is null assuming that it's datatype is datetime?.

The your first line of code will in fact error out if CreateDateTime is null and that can be fixed by just doing it like your second line:

<%# String.Format("{0:yyyy/MM/dd}",((MyType)Container.DataItem).CreateDateTime) %>

Can you provide more information as to what the datatype is and what error you are receiving?

Another solution would be to use (again assuming DateTime? datatype but you could check for DBNull as well):

<%# (((MyType)Container.DataItem).UpdateDateTime == null) ?
    "No Date Text" :
    ((MyType)Container.DataItem).UpdateDateTime.Value.ToString("yyyy/MM/dd") %>
Kelsey