1

I am trying to save Time with entity framework. I am not really bothered about the date. I have declared my property as DateTime in Code and I am using CodeFirst.

In the xaml I use xceed TimePicker to display the Time as below

<Xceed:TimePicker Grid.Row="6" Grid.Column="1" Margin="5" Value="{Binding SelectedItem.UpdateTimer, Mode=TwoWay}" Format="LongTime" />

The problem is that when I run my application for the first time, The Datetime received by my property in code is '01/0/1/0001' for date followed by the time I have entered. When I try to save my context, I get this error which probably means that SQL Server does not like my date. How should I fix this?

"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."

My Property code is

     public DateTime UpdateTimer
         {
             get
             {
                 return Data.UpdateTimer;
             }
             set
             {
                 if (value != Data.UpdateTimer)
                 {
                     Data.UpdateTimer = value;
                     OnPropertyChanged("UpdateTimer");
                 }
             }
         }
Jack Gray
  • 309
  • 2
  • 14

2 Answers2

1

From what I understand form your code, you don't set your date it the setter of the UpdateTimer and then you bind it with your TimePicker, what the setter return to your control is probably null, that's because you get this error, try setting the value in the setter see if you still get an error, also take a look at this question see if it solves your problem.

Community
  • 1
  • 1
Hamid Mosalla
  • 3,279
  • 2
  • 28
  • 51
1

I initialized my field with today's date which solved my problem

        DateTime date = DateTime.Now;    
        UpdateTimer = new DateTime(date.Year, date.Month, date.Day, 0, 0, 5); 
Jack Gray
  • 309
  • 2
  • 14