Cast exception when trying to populate DatePicker when adding new record

4 posts, 1 answers
  1. Brad
    Brad avatar
    29 posts
    Member since:
    Jul 2010

    Posted 05 Aug 2010 Link to this post

    Hi all,

    I am using the below code to populate a DateInput when the user edits a record in a RadGrid so the Edit form is pre-populated with correct data. All is well until I try to Add a new record, because the ExpireDate variable is now NULL.

    The Exception is:

    An exception of type 'System.InvalidCastException' occurred in App_Web_xngshb9r.dll but was not handled in user code

    Additional information: Specified cast is not valid.

    And the code block (client-side):

    <

     

    telerik:RadDatePicker ID="RDP" runat="server" MinDate="01/01/1900" MaxDate="12/31/2100"

     

     

        SelectedDate='<%# Eval("ExpireDate") %>' TabIndex="2" OnSelectedDateChanged="RDP_SelectedDateChanged">

     

     

    </telerik:RadDatePicker>

    Any help would be greatly appreciated!

     

  2. Brad
    Brad avatar
    29 posts
    Member since:
    Jul 2010

    Posted 05 Aug 2010 Link to this post

    Figured it out. Just populated the DateInputs in Page_PreRender.
  3. Answer
    Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 06 Aug 2010 Link to this post

    Hello Brad,

    Instead of using: SelectedDate='<%# Eval("ExpireDate") %>' you should use DbSelectedDate='<%# Eval("ExpireDate") %>'. This way it can handle the null value. There's no need to populate it in the PreRender event.

    I hope that helps.
  4. Brad
    Brad avatar
    29 posts
    Member since:
    Jul 2010

    Posted 06 Aug 2010 Link to this post

    Worked like a charm :)

    Thanks!
Back to Top