how to set the selecteddate property of .NET asp:Calendar Control  when the bound item is null?

Posted on 2009-04-29
Last Modified: 2012-06-27
I have a calendar control bound  in a gridview, on add of a new item, I get the following error because the enrol_date is null:
Object cannot be cast from DBNull to other types.

If enrol_date is null I need to set cal1's  date to a different control's date eg: <asp:Calendar id="cal2" /> (and this calendar control is in the .aspx page not inside the gridview though).

Can this be done and if so how ?
<asp:Calendar ID="cal1" runat="server"   SelectedDate='<%#  Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "ENROL_DATE")) %>'>


Open in new window

Question by:tech_question
    1 Comment
    LVL 12

    Accepted Solution

    Check for NULL and use Cal2's value...

    Some code below not pretty
    SelectedDate='<%#  Convert.ToDateTime( (DataBinder.Eval(Container.DataItem, "ENROL_DATE") == DBNull ? ((Calender)FindControl(cal2)).value : DataBinder.Eval(Container.DataItem, "ENROL_DATE")) ) %>

    Open in new window


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now