Hi,
if you don't need to cast to a date first, you could simply have a
databinding expression in the aspx. For example:
<asp:TemplateColumn>
<ItemTemplate>
<%#
DataBinder.Eval(Container.DataItem,"database_field _name","{0:dd.MM.yyyy}")%>
</ItemTemplate>
</asp:TemplateColumn>
where "{0:dd.MM.yyyy}" indicates the formatting. Now, databinding
expressions should not blow up with null values (DBNull), they just produce
empty string.
However, if you need to cast to a date in code, you can nothing but check
for the DbNull value from the field with IsDbNull function (VB) or
Convert.IsDbNull (Framework/C#) which return boolean value indicating if the
value is DbNull. When there is a DbNull (per previous methods) you just
outout empty string (or other predefined string such as -No date-), you
cannot cast it to a Date. So it means that this check should happen before
trying casting.
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke