ASP.NET Datagrid Date Formatting with NULLS
Post # 77 permalink Topic #77 by mreschke on 2008-06-11 16:29:53 (viewed 1,158 times)

When displaying a formated date string in an ASP.NET datagrid, you get an error if the dataset field is NULL

Example of Bad code[-][- -][++]

If closeDate is not NULL, this works fine, otherwise it errors.

Code Snippet
<asp:Label id=lblCloseDate runat="server" text='<%# Format(container.dataitem("closeDate"), "MM/dd/yyyy") %>'>

This works if closeDate is not NULL, otherwise it displays MM/dd/yyyy (in that format, not date format, does \
not display date)

Code Snippet
<asp:Label id=lblCloseDate runat="server" text='<%# Format(container.dataitem("closeDate").tostring, "MM/dd/yyyy") %>'>

Example of Good code[-][- -][++]

This works if closeDate is null (displays nothing), or contains a date. This is what you want to use.

Code Snippet
<asp:Label id=lblCloseDate runat="server" text='<%# string.Format("{0:MM/dd/yyyy}",container.dataitem("closeDate")) %>'>