This is a migrated thread and some comments may be shown as answers.

dbContext.Save Handling error when SqlTypeException

7 Answers 97 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Olivier
Top achievements
Rank 2
Olivier asked on 12 Aug 2015, 09:09 PM

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.

 

7 Answers, 1 is accepted

Sort by
0
Pavel Uzunov
Telerik team
answered on 17 Aug 2015, 01:20 PM
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.
0
Olivier
Top achievements
Rank 2
answered on 17 Aug 2015, 02:07 PM

Hello

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

to show the name and the value ?

 thanks
Olivier

0
Pavel Uzunov
Telerik team
answered on 21 Aug 2015, 11:25 AM
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.
0
George
Top achievements
Rank 1
answered on 21 Aug 2015, 02:26 PM

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.

0
Pavel Uzunov
Telerik team
answered on 26 Aug 2015, 11:50 AM
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.
0
Olivier
Top achievements
Rank 2
answered on 26 Aug 2015, 02:25 PM
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
0
Pavel Uzunov
Telerik team
answered on 31 Aug 2015, 01:01 PM
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.
Tags
General Discussions
Asked by
Olivier
Top achievements
Rank 2
Answers by
Pavel Uzunov
Telerik team
Olivier
Top achievements
Rank 2
George
Top achievements
Rank 1
Share this question
or