none
The conversion of a datetime2 data type to a datetime data type error RRS feed

  • Question

  • Hi, I've read a few articles regarding this issue but none seem to match what I'm seeing with the code I'm working on.

    I'm using MVC with Angular as my front end. I have three separate pages using a date input field. They all appear to be set up the same way but the third one is throwing a "The conversion of a datetime2 data type to a datetime data type" error when saving to my db.

    All three screens use the following to get the current date and time: var currentdateandtime = new Date(); which yields Fri Apr 10 12:53:23 CDT 2015 as a format.

    All three screens then have the currentdatetime assigned to the view model object LastReviewDate : currentdatetime. this so when the page loads the current date/time is displayed in the Last Review Date input field.

    When saving on screen 1 the date/time is then in this format: 2015-04-10T00:00:00 and when passed to the controller in this format: 4/10/2015 12:00:00 AM

    When saving screen 2 the date/time is then in this format: Fri Apr 10 12:59:17 CDT 2015 and when passed to the controller in this format: 4/10/2015 05:59:17 PM

    Screen 3, the one I'm having issues with, has the date/time in this format when saving:  Fri Apr 10 13:03:57 CDT 2015 and when passed to the controller in this format: 4/10/2015 06:03:57 PM.

    All three are set up in their respective entities as follows:

    public DateTime LastReviewDate { get; set; }

    All three have db column datatype of date and are set to null. Each screen has their own table.

    I can't see why I'm getting an error for Screen 3.

    I should also note that Screen 1 and Screen 2 are using date pickers. Screen 3 was originally using a disabled input field with the date set as var currentdateandtime = new Date().toLocaleString().replace(/\u200E/g, '');. I had to change it to use a date picker like the others. For some reason when I change the date to use the above it saves with no issues. The problem the date displays as Friday, April 10, 2015 02:07:45 PM when the page loads and then if I choose a date it displays as 08-Apr-2015.

    Regardless, I don't understand why when removing toLocaleString().replace(/\u200E/g, '') causes it to fail.

    • Edited by rsford31 Monday, April 13, 2015 9:40 PM
    Friday, April 10, 2015 6:51 PM

Answers

  • I'm using the entity framework as well. At any rate, I figured it out. I had originally two dates being set in the controller: the one described above and a LastUpdateDate. I removed the LastUpdateDate and meant to set it in the Save() method to DateTime.Now. This was what was causing the error, not the other one. But THANKS!
    Monday, April 13, 2015 9:45 PM

All replies

  • Hello rsford31,

    From your description, this issue is related with MVC project, i suggest you post it to the MVC forum:http://forums.asp.net/1146.aspx, the current forum you posted to is for Entity Framework.

    Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 13, 2015 3:02 AM
    Moderator
  • I'm using the entity framework as well. At any rate, I figured it out. I had originally two dates being set in the controller: the one described above and a LastUpdateDate. I removed the LastUpdateDate and meant to set it in the Save() method to DateTime.Now. This was what was causing the error, not the other one. But THANKS!
    Monday, April 13, 2015 9:45 PM