dbContext.Save Handling error when SqlTypeException

8 posts, 0 answers
  1. Olivier
    Olivier avatar
    146 posts
    Member since:
    Nov 2010

    Posted 12 Aug 2015 Link to this post

    Hello

     When i save with dbContextSave(), i get an error on SqlTypeException

    Can i get a Handling SqlTtype Exception when i Save my data ?

    in try catch  ?
    Cause i want to find the name of column, which not set a good value.
    And the error is not explicit . i Get ????????????

    So i want to write in Log output to see that

    thanks

     

    System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

     

  2. Pavel Uzunov
    Admin
    Pavel Uzunov avatar
    14 posts

    Posted 17 Aug 2015 Link to this post

    Hello Olivier,

    On the first look, I can see that some DateTime property throws exception. That often happens when some posted property is not initialized or has date before 1/1/1753. My recommendation in this case, is to review all of your properties from DateTime type and see is there any properties with not correct data.
    After that you can try with log statements (generated by the OpenAccessContext). Please take a look at this article that actually illustrates how to enable tracing and logging. 

    I hope the provided information is helpful.

    Regards,
    Pavel Uzunov
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
  4. Olivier
    Olivier avatar
    146 posts
    Member since:
    Nov 2010

    Posted 17 Aug 2015 in reply to Pavel Uzunov Link to this post

    Hello

     Have you a sample to browse each properties which are in DateTime ?

    to show the name and the value ?

     thanks
    Olivier

  5. Pavel Uzunov
    Admin
    Pavel Uzunov avatar
    14 posts

    Posted 21 Aug 2015 Link to this post

    Hello,

    You can use our change tracking API (as shown here: How to get changes that will be performed during the next commit. ) to list all changed objects right before calling the context.SaveChanges() method and then inspect each of the properties. You could do it manually during debugging or write some reflection code that would do it automatically. 

    I hope this helps

    Regards,
    Pavel Uzunov
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  6. George
    George avatar
    9 posts
    Member since:
    Mar 2015

    Posted 21 Aug 2015 in reply to Olivier Link to this post

    I have encountered this exception before using nhibernate (I was careful not to do it again in Data Access).

    I am pretty sure this is the result of not initializing a non-nullable DateTime field then saving.  The default value for a DateTime is not in the range valid SqlTimes.

     

    Using LinqPad:

    var d = new DateTime();
    d.Dump(); // "1/1/0001 12:00:00 AM"

     

    You need to either initialize the date to a valid value or make it nullable.  What you do depends on if the field is nullable in the database.

  7. Pavel Uzunov
    Admin
    Pavel Uzunov avatar
    14 posts

    Posted 26 Aug 2015 Link to this post

    Hi George,

    Your are right, in most of the cases this is the case for such issues. 

    Regards,
    Pavel Uzunov
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  8. Olivier
    Olivier avatar
    146 posts
    Member since:
    Nov 2010

    Posted 26 Aug 2015 Link to this post

    Hello,

    I came across my error, testing all date fields, and fields had the date of 01/01/0001 therefore not Gregorian.

    But it would be simpler, that the save, (I know that launches a procedure stored after)
    we can say is the date_update fields.

    If this is not possible, I have found a book that makes me gain more time.

    Thank you
  9. Pavel Uzunov
    Admin
    Pavel Uzunov avatar
    14 posts

    Posted 31 Aug 2015 Link to this post

    Hello,

    You can use our change tracking API to detect the objects to be inserted and using reflection to set the DateTime properties to the desired value, right before calling the context.SaveChanges() method. You can take a look at this article.

    Another solution is to make nullable your DateTime properties (in case this is not an issue for your data model). This will instruct DataAccess to not include them in the insert statements. 

    I hope this helps.

    Regards,
    Pavel Uzunov
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top
DevCraft banner