RadTimePicker SelectedTime.Value Nullable Object must have Value

2 posts, 0 answers
  1. Jackie
    Jackie avatar
    18 posts
    Member since:
    Jan 2016

    Posted 19 Sep Link to this post

    I'm reading in values from several TimePickers that may or may not be null. SelectedTime.Value throws the mentioned error when the value is a null and using DbSelectedDate with a (TimeSpan?) cast is invalid. Currently I'm accepting it as a (DateTime?) instead and breaking it down in an extra step. Is there a more elegant solution or is this the standard practice?
  2. Eyup
    Eyup avatar
    3333 posts

    Posted 22 Sep Link to this post

    Hello Jackie,

    Yes, you are correct. The pickers return nullable objects because the user could also have removed any date or time from the input. You can use some if statements whether this is the case using the RadTimePicker1.SelectedDate.HasValue boolean. Of course, there are also other ways to achieve that:
    protected void Page_Load(object sender, EventArgs e)
        DateTime? date = RadDateTimePicker1.SelectedDate;
        DateTime? date2 = RadTimePicker1.SelectedDate;
        TimeSpan? time = RadTimePicker1.SelectedTime;
        TimeSpan nonNullableValue = RadTimePicker1.SelectedTime ?? new TimeSpan(0);

    I hope this will prove helpful.

    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top