This is a migrated thread and some comments may be shown as answers.

object cannot be cast from dbnull to other types

1 Answer 352 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
D.SRINIVASA
Top achievements
Rank 2
D.SRINIVASA asked on 18 Jun 2011, 08:23 AM
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

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 23 Jun 2011, 08:06 AM
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.

Tags
ComboBox
Asked by
D.SRINIVASA
Top achievements
Rank 2
Answers by
Veli
Telerik team
Share this question
or