How to fix The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
You did not set a value for SolvedDate
, so the default value which is 01\01\01
will be sent to the database, which causes the error.
If you want to have SolvedDate
with null
value, you should define it as Nullable
date(DateTime?
) because DateTime
is a struct
and cannot be set to null
.
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated
The datetime column "DateAdded" is marked Not Nullable in sql.
Option 1 (change controller):
[HttpPost]
public ActionResult Create(Movie movie)
{
//setdate
movie.DateAdded = Datetime.Now;
_context.Movies.Add(movie);
_context.SaveChanges();
return RedirectToAction("Index", "Movies");
}
Option 2 (change model)
public class Movie
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
public DateTime DateAdded { get; set; } = Datetime.Now;
[Display(Name = "Release Date")]
public DateTime ReleaseDate { get; set; }
[Display(Name = "Number in Stock")]
public byte NumberInStock { get; set; }
public Genre Genre { get; set; }
[Display(Name = "Genre")]
[Required]
public byte GenreId { get; set; }
}
The conversion of a datetime2 data type to a datetime data type Error
The error is because you haven't actually set those values correctly, make sure you set them depending on your applications locale. (e.g. dd/mm/yyyy for en-GB
, mm/dd/yyyy for en-US
).
It also looks like your database has been set up to use a datetime2
column and not a datetime
column.
You can either:
A) Modify the database to change the type from datetime2
to datetime
B) Change the types in your Model to be datetime2
, by doing:
[Column(TypeName = "DateTime2")]
public DateTime StartTime { get; set; }
[Column(TypeName = "DateTime2")]
public DateTime EndTime { get; set; }
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
The issue is that you're using ApplyPropertyChanges
with a model object that has only been populated with data in the form (headline, story, and image). ApplyPropertyChanges
applies changes to all properties of the object, including your uninitialized DateTime
, which is set to 0001-01-01, which is outside of the range of SQL Server's DATETIME
.
Rather than using ApplyPropertyChanges
, I'd suggest retrieving the object being modified, change the specific fields your form edits, then saving the object with those modifications; that way, only changed fields are modified. Alternately, you can place hidden inputs in your page with the other fields populated, but that wouldn't be very friendly with concurrent edits.
Update:
Here's an untested sample of just updating some fields of your object (this is assuming you're using LINQ to SQL):
var story = _db.ArticleSet.First(a => a.storyId == ArticleToEdit.storyId);
story.headline = ArticleToEdit.headline;
story.story = ArticleToEdit.story;
story.image = ArticleToEdit.image;
story.modifiedDate = DateTime.Now;
_db.SubmitChanges();
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range?
DateTime
has the range: January 1, 1753, through December 31, 9999
DateTime2
has the range: 0001-01-01 through 9999-12-31
So if you are entering a date before 1753 you would get this error when the field in the table is of type DateTime
.
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value exception
datetime2
coveres a larger range than datetime
.
datetime2:
Date range 0001-01-01 through 9999-12-31
datetime:
Date range January 1, 1753, through December 31, 9999
Date="12-03-19"
probably converts to year 12 (or 19). The solution could be using DateTime
for the Date
property:
public class TimeOutJustification
{
public DateTime Date { get; set; }
...
}
Related Topics
Why Can't I Use an Alias in a Delete Statement
How to Insert Unicode Text to SQL Server from Query Window
Generate Default Values in a Cte Upsert Using Postgresql 9.3
For JSON Path Returns Less Number of Rows on Azure SQL
How to Change Db Schema to Dbo
SQL Server 2005 - Export Table Programmatically (Run a .SQL File to Rebuild It)
Function with SQL Query Has No Destination for Result Data
"This SQLtransaction Has Completed; It Is No Longer Usable."... Configuration Error
Postgresql Tables Exists, But Getting "Relation Does Not Exist" When Querying
SQL Populate Table with Random Data
How to Start Auto Increment from a Specific Point
SQL Server - Lack of Natural Join/X Join Y Using(Field)
How to Represent a Data Tree in SQL