object cannot be cast from dbnull to other types

2 posts, 0 answers
  1. D.SRINIVASA
    D.SRINIVASA avatar
    34 posts
    Member since:
    Oct 2010

    Posted 18 Jun 2011 Link to this post

    Hi,

    I am Srinivasa

    in my project i have a problem for bind the Date form backend table to RadDatePicker in Rad Grid. But in my data Base in date column i have a empty value. 

    here i am using the client side coding for bind Date data to RadDatePicker in RadGrid like the following way...

                           <telerik:RadDatePicker ID="actstart" runat="server" MaxDate='<%# Convert.ToDateTime(Eval("early_start_display")).ToString("dd/MMM/yyyy") %>' Width="100px" Calendar-ShowOtherMonthsDays="false" 
                            Calendar-ShowRowHeaders="false" DbSelectedDate='<%# Bind("actual_start") %>' DateInput-DateFormat="MM/dd/yyyy">
                           </telerik:RadDatePicker>

    But it shows the error at the time of Date Column Null value in the DataBase...

    Error is :

    object cannot be cast from dbnull to other types

    Please Help me.

    Thanks

    D.Srinivasa
  2. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 23 Jun 2011 Link to this post

    Hello Srinivasa,

    Which of your fields contains the null value in this case - early_start_display or actual_start? You are binding the actual_start field to the DbSelectedDate property of the picker. This property (as the name suggests) expects a DB value and is designed to handle null values. Also, is the date picker in  the ItemTemplate or the EditItemTemplate of the grid column? Assuming the early_start_display field contains the DbNull value and the picker is in the EditItemTemplate, here is RadGrid's ItemDataBound event handler you can use to explicitly check for null values for this field:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            RadDatePicker picker = (RadDatPicker)e.Item.FindControl("actstart");
            object startDate = DataBinder.Eval(e.Item.DataItem, "early_start_display");
            if (startDate != null && startDate != DBNull.Value)
            {
                picker.MaxDate = Convert.ToDateTime(startDate);
            }
        }
    }

    With the above code, you should now remove the MaxDate='<%# Convert.ToDateTime(Eval("early_start_display")).ToString("dd/MMM/yyyy") %>' portion from your markup and have the ItemDataBound event handler handle this for  you.

    Veli
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Back to Top