CalendarExtender and SelectedDate if date null solution here RRS feed

  • Question

  • User-1420987958 posted

    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
    dateField Is DBNull.Value Then
    Return Nothing
    Return CType
    (dateField, DateTime)
    End If
    End Function


    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")) %>'


    If anyone cares to explain how (and if it's better) I'd do the same with javascript, including "CommonFunctions" class.. that's cool [cool].


    Friday, May 16, 2008 6:39 AM

All replies

  • User1083585561 posted

    your solution works great for me.  Thank you 

    Monday, February 8, 2010 2:13 PM
  • User-1337876127 posted

    Another way to do this inline:

    <cc1:CalendarExtender ID="calActivityDate"
                                    SelectedDate='<%# Convert.IsDBNull(Eval("activityDate")) ? null : Eval("activityDate") %>'
                                    PopupButtonID="imgActivityDate" />

    Notice the SelectedDate parameter and how it checks for dbnull and returns either null or the date from the table.

    Thursday, June 16, 2011 8:55 AM