asked on
Runtime error
ASP.NET MVC VS 2019
Can you please help me with this error?
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using AdventureDB.Models;
namespace AdventureDB.Controllers
{
public class SchedulingController : Controller
{
private SchedulingContext db = new SchedulingContext();
// GET: Scheduling
public ActionResult Index()
{
return View(db.Training_Program.ToList());
}
// GET: Scheduling/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Training_Program training_Program = db.Training_Program.Find(id);
if (training_Program == null)
{
return HttpNotFound();
}
return View(training_Program);
}
// GET: Scheduling/Create
public ActionResult Create()
{
int lastCourseTypeId = db.Training_Program.Max(t => t.CourseTypeID);
int nextCourseTypeId = lastCourseTypeId + 1;
Training_Program training_Program = new Training_Program
{
CourseTypeID = nextCourseTypeId
};
return View(training_Program);
}
// POST: Scheduling/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CourseTypeID, Course_Type, ClassSize, Training_Prog, Descr, Start_Day, End_Day")] Training_Program training_Program)
{
if (ModelState.IsValid)
{
db.Training_Program.Add(training_Program);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(training_Program);
}
// GET: Scheduling/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Training_Program training_Program = db.Training_Program.Find(id);
if (training_Program == null)
{
return HttpNotFound();
}
return View(training_Program);
}
// POST: Scheduling/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "CourseTypeID,Course_Type, ClassSize, Training_Prog,Descr,Start_Day,End_Day")] Training_Program training_Program)
{
if (ModelState.IsValid)
{
db.Entry(training_Program).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(training_Program);
}
// GET: Scheduling/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Training_Program training_Program = db.Training_Program.Find(id);
if (training_Program == null)
{
return HttpNotFound();
}
return View(training_Program);
}
// POST: Scheduling/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Training_Program training_Program = db.Training_Program.Find(id);
db.Training_Program.Remove(training_Program);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Training_Program.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace AdventureDB.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Training_Program
{
public int CourseTypeID { get; set; }
public Nullable<int> Course_Type { get; set; }
public Nullable<int> ClassSize { get; set; }
public string Training_Prog { get; set; }
public string Descr { get; set; }
[DisplayFormat(DataFormatString = "{0:MMM d, yyyy}")]
public DateTime Start_Day { get; set; }
[DisplayFormat(DataFormatString = "{0:MMM d, yyyy}")]
public DateTime End_Day { get; set; }
}
}
Despite Kyle's solution, I want to mention that when you call the extension method ToList, it takes the enumerable and transforms it into a list by evaluating each element in the query. So, when you made the call to ToList, it threw the exception on the first element because it queried it and wasn't satisfied by the default value it was set with, which comes from your datasource, which is your database, which is null. So, declaring your End_Day datetime property nullable will solve your issue.
Here's the remark from Microsoft:
If my answer satisfies your issue, consider marking Kyle's answer as the solution and mine as assisted.