Im trying to calculate the how many days there are between two dates and display the result in a textblock, i am using wpf. However i get a nullable object must have a value in the first line :S
private void button20_Click(object sender, RoutedEventArgs e)
{
DateTime start = datePicker1.DisplayDateStart.Value.Date;
DateTime finish = datePicker2.DisplayDateStart.Value.Date;
TimeSpan difference = start.Subtract(finish);
textBlock10.Text = Convert.ToString(difference);
}
As the error message implies,
DisplayDateStartis a nullable property, which means it can (and, by default, does) have no value. You have to handle this condition to produce sensible results.That said, the
DisplayDateStartproperty refers to the earliest date shown in the DatePicker’s calendar, not the date the user has picked: for that, you need theSelectedDateproperty, which is also nullable.There are a variety of ways you could handle a NULL value: display nothing in the TextBlock, display “N/A” or some other default, etc. Here’s an example: