0

I Have the following code in my Model

public int Id { get; set; }

    [Required]
    [StringLength(255)]
    public string Name { get; set; }

    public DateTime DateAdded { get; set; }

    [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; }

This is my controller coder error point shows

[HttpPost]
    public ActionResult Create(Movie movie)
    {
        _context.Movies.Add(movie);
        _context.SaveChanges();

        return RedirectToAction("Index", "Movies");
    }

And this is the view

<div class="form-group">
    @Html.LabelFor(m => m.Movie.ReleaseDate)
    @Html.TextBoxFor(m => m.Movie.ReleaseDate, "{0:d MMM yyyy}", new { @class = "form-control" })
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Movie.GenreId)
    @Html.DropDownListFor(m => m.Movie.GenreId, new SelectList(Model.Genres, "Id", "Name"), "Select Movie Genre", new { @class = "form-control" })
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Movie.NumberInStock)
    @Html.TextBoxFor(m => m.Movie.NumberInStock, new { @class = "form-control" })
</div>

<button type="submit" class="btn btn-primary">Save</button>

When I click submit button, I get an error:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.

Any idea about this problem? I'm assuming it's a datetime issue, but I cannot get the problem. I'm a beginner at this.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Cross
  • 15
  • 4
  • 4
    Possible duplicate of [Conversion of a datetime2 data type to a datetime data type results out-of-range value](https://stackoverflow.com/questions/1331779/conversion-of-a-datetime2-data-type-to-a-datetime-data-type-results-out-of-range) – Sonal Borkar Dec 24 '18 at 21:01
  • Make sure you [use `datetime2` rather than `datetime`](https://stackoverflow.com/questions/1334143/datetime2-vs-datetime-in-sql-server); and if it's nullable, make sure the column is nullable. – Richardissimo Dec 24 '18 at 23:22
  • its already nullable. But why i nedd datetime2? @Richardissimo – Cross Dec 25 '18 at 06:01
  • I gave a link with the explanation of why to use datetime2 rather than datetime...did you read it? Datetime2 has a wider range than datetime, preventing this out of range issue (alonng with several other good reasons.) – Richardissimo Dec 26 '18 at 04:01

2 Answers2

0

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; }
}
Solarcloud
  • 555
  • 1
  • 7
  • 15
  • Thanks a lot. You save my day. Is this make the column nullable? – Cross Dec 25 '18 at 06:28
  • You could also make DateAdded nullable in the database however I don't think that would be wise in this case. Option 1 and 2 do not make the column in the database nullable. Option 1 just populates the DateAdded to Now before you SaveChanges, and Option 2 sets the default date to Datetime.Now in code when you instantiate Movie (instead of NULL). You could also set a default date to GETDATE() in sql as another option. Just want to give you options so you can decide what works best in your case! I've been stumped by this in the past and feel your pain. – Solarcloud Dec 26 '18 at 20:25
0

Simply change your DateAdded column with datetime2 instead of datatime like this.See Image

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 13 '22 at 20:32