I'm using ado.net entity data model. When update entity object, this error shown "String or binary data would be truncated" or "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.". I know why this error shown.
How to handle this error created on which column ?
You probably have an uninitialized DateTime property on your object. The default value, which is in the year 0, cannot be stored in SQL Server's weirdly limited DATETIME columns. Set the property to a reasonable value or make it nullable in your DB.
In addition to Craig you could set both - the db-field and the variable - to nullable. Might be a bit more performant.
Related
When I set a nullable date time to DateTime.Max and save it to a database field of postgres type
timestamp without time zone
it saves it as
10000-01-01 00:00:00
Then when I reload a page that merely reads from this table I get the following error:
System.InvalidCastException: Specified cast is not valid.
a) Is DateTime? the correct C# conversion type?
b) What is the best way to set the DateTime value?
I checked the docs here http://www.postgresql.org/docs/9.2/static/datatype-datetime.html and it specifies the max value is 294276 AD... but that's lower than is currently set so it can't be that breaking it. I am using the latest version of NpgSql and entity framework
Many thanks
The problem lies within your C# code. According to MSDN:
The DateTime.MaxValue field represents the largest possible value of
DateTime, which is December 31, 9999 in the Gregorian calendar.
Your date value is just above this value, hence the exception.
I would suggest explicitly saving a very large date value in the database, e.g. December 31, 9999, in case the date value is not available.
I am writing a unit test which stores an object with a DateTime parameter into a DATETIME2 SQL Server database column. I then create a temporary DateTime object called new_date_time and set that value to DateTime.Now.
The new_date_time value is then used to update the previous value and the SQL query to do this completes successfully.
When re-reading the object back from the database I receive the correct datetime values for days/hours/minutes but the .Ticks value is different from the new_date_time variables .Ticks property. The value returned from the read call returns the last 4 digits of the .Ticks property as zeros.
Why is this rounding occurring making my Assert.AreEqual fail?? :)
Thanks
I guess you are using Parameters.AddWithValue when writing the date to Sql Server. From MSDN the inferred type of a CLR DateTime is SqlDbType.DateTime and not SqlDbType.DateTime2 so the precision is being lost when writing your date to the database.
Explicitly setting the type to datetime2 will solve the issue. For example:
command.Parameters.AddWithValue("#now", DateTime.Now).SqlDbType =
SqlDbType.DateTime2;
Edit
#marc_s makes a good point with his comment:
You should read (and embrace!) Can we stop using AddWithValue() already?
To avoid these kind of issues from biting you, you could get into the habit of using the Add method on the parameters collection which takes the SqlDbType in some overloads and then set the Value property on that rather than using the AddWithValue method:
command.Parameters.Add("#now", SqlDbType.DateTime2).Value = DateTime.Now;
Maybe your database field is not storing your entire DateTime.Now value, because it's not precise enough. Why don't you simply compare your dates after you've formatted them as you like?
eg: (untested):
var databaseDate = d1.ToString("MM/dd/yyyy HH:mm:ss.fff");
var tempDate = d2.ToString("MM/dd/yyyy HH:mm:ss.fff");
Assert.AreEqual(databaseDate, tempDate);
I tested: using Linq To Entities My DateTime.Now is correctly saved to my datetime2(7) and equality test return True.
Are you sure you're passing your correct datetime value to the database? without truncating it?
I've been through a tough day facing this problem. In ent object, I have DiinputTanggal property as Date. In database, I have DiinputTanggal column as DateTime. When I try to insert the ent object into the database, I got the following error shown in the screenshot below. But when I debug, the property DiinputTanggal in ent object seems perfectly fine and nicely formatted. I have no idea where is my mistake.
Looking at your screenshot, it is probably the TaggalAktif property which is causing the overflow. .Net DateTimes default to DateTime.MinValue which cannot be represented in SQL DateTime.
You have several options
Initialize the DateTime to a value supported by Sql DateTime (in the range indicated by the error)
Change the DateTime to be nullable in both the class and database, (and ensure the property is initialized to null).
Use another Sql DataType to store the data e.g. DateTime2 or just Date if time isn't needed.
I am having a weird problem inserting a date value into my database. When debugging my program, i realized it inserts the first five rows into the database but on the sixth it throws an exception. I verified if it was a syntax error or if something changed along the way by checking the first 5 queries that could execute successfully with the sixth and they are both the same. If there is a problem with the type, it should throw the exception from the beginning. What do you think?
This is the error I get
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
That suggests you're specifying the value as a string, and performing a conversion. Don't do that. Avoid string conversions wherever you possibly can. (I suspect there's quite possibly a disconnect between the string format you're providing and the one that's being used for parsing. Removing the string conversion completely is the best fix for this.)
Instead, provide the value as a SqlParameter with a DateTime value.
I think your problem comes because of the difference between DateTime type in C# which start at 01-01-0001 and in SQL Server which is `January 1, 1753, through December 31, 9999', check this link, so I think (due to the error Jon explained) you are trying to insert a Date value which is less than 01-01-1753 into SQL Server. That's why the SQL Exception is shown.
On this project, I am tracking when something is created, edited, and finally processed. I have three DateTime fields setup for this. The code below is for when I create the record.
newsArchive.CreateDateTime = DateTime.Now;
newsArchive.ModifyDateTime = DateTime.MinValue;
newsArchive.SendDateTime = DateTime.MinValue;
naRepository.Add(newsArchive);
naRepository.Save();
I am getting "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM." when I try to add it. I know I can't send a NULL. How would you handle this?
I am using Linq2SQL to process this.
Why can't you send a null? AFAIK, Linq2SQL supports nullable value types; you just need the column in the DB to be nullable or L2S won't project the DAO with the nullable type.
You could use (DateTime)(SqlDateTime.MinValue) instead of DateTime.MinValue. This will use the minimum SQL-compatible datetime value, explicitly cast to DateTime.
Though both SqlDateTime and DateTime actually store time as a UInt64 "Ticks" value, the difference between the two types is that SQL Server uses a different epoch (value for zero) and a different resolution (what 1 "tick" represents in fractions of a second) than the CLR's DateTime.
Which version of SQL Server are you using? If you are using SQL 2008 (or 2008 R2) you can use the datetime2 data type which can support the full range of the .NET DateTime type.
Otherwise, use NULL (your model will need to have a property of type Nullable<DateTime>) or live within the limitations of the data type and DateTime.MinValue exceeds the limit of the SQL datetime type.
Create your own minimum date - a Date variable with value 1/1/1753 12:00:00 AM and use that instead of DateTime.MinValue
The datetime2 datatype allows dates as early as year 1. You can also use DateTime? to allow null dates in your .net code