Last post Jun 16, 2011 08:55 AM by adiel
May 16, 2008 06:39 AM|okkko|LINK
Problem: SelectedDate property won't work if you field in database is null. For example I tried: SelectedDate='<%# Eval("dateField") %>' . The page says "Specified cast is not valid.".
Ugh, that took me a while to get and couldn't find the solution anywhere online so I'm posting this if anyone has the same problem.
Write a simple function:
Public Shared Function selectDate(ByVal dateField As Object) As Object
If dateField Is DBNull.Value Then
Return CType(dateField, DateTime)
Then you just call your SelectedDate like this: SelectedDate='<%# selectDate(Eval("dateField")) %>'
I added my function in a separate class called CommonFunctions in App_Code folder, because I will need it several times.
In this case I call it like this: SelectedDate='<%# CommonFunctions.selectDate(Eval("fst_date")) %>'
Feb 08, 2010 02:13 PM|ThaoBond|LINK
your solution works great for me. Thank you
Jun 16, 2011 08:55 AM|adiel|LINK
Another way to do this inline:
SelectedDate='<%# Convert.IsDBNull(Eval("activityDate")) ? null : Eval("activityDate") %>'
Notice the SelectedDate parameter and how it checks for dbnull and returns either null or the date from the table.