I have an ASP.NET FX application hosted on an IIS server. On the server, and only on the server, not in Visual Studio IIS Express, when loading the 'Create' page I get this error:
Exception thrown: 'System.NullReferenceException' in System.Web.Mvc.dll
Usually 28 times. The errors below that occur when trying to create a new record (the POST method) but gives just as little information as the first error. I have VS Remote debugging configured for the server.
Create Get method
// GET: Statewides/Create
public ActionResult Create()
{
return View();
}
Create Post method
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,COUNTY,SignSys,RouteNoOrigImport,RouteNo,SuppDes,LocalName,DateNumber,Method,OriginalL,OriginalD,Projects,Documents,Comments,District,Duplicate_OK,RightofWay,ReservedRoute,ReservedBy,ReservedDate,CP_WorkCompleted,Work_Comments,CP_ProjectNo,ReservedCat,CurrentStatus,Add_User,Date_Added")] Statewide statewide)
{
if (ModelState.IsValid)
{
var userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
statewide.Add_User = userName;
statewide.Date_Added = DateTime.Today;
db.Statewides.Add(statewide);
try
{
db.SaveChanges();
}
catch (DbEntityValidationException ex)
{
foreach (var entityValidationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in entityValidationErrors.ValidationErrors)
{
Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
}
}
}
int lastAddedID = db.Statewides.Max(item => item.ID);
return RedirectToAction("Create", "Documents", new { statewideID = lastAddedID});
}
return View(statewide);
}
With the try catch block the code runs fine but no record is added to the database. Just nothing happens. The page simply navigates to the next page without adding a database record.
Without the try catch block simply using db.SaveChanges();
the app crashes and I get the errors:
Exception thrown: 'System.Data.Entity.Validation.DbEntityValidationException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Validation.DbEntityValidationException' in System.Web.Mvc.dll
Exception thrown: 'System.Data.Entity.Validation.DbEntityValidationException' in System.Web.Mvc.dll
I don't find these errors very helpful. I have no idea what the problem is. The errors don't point to any line or any particular variable or object so I have no idea where the problem is. I also don't understand why the try catch block simply ignores these errors.
What can I do to get a better error message?
EDIT: I tried to add the code that was recommended in the comments like this:
catch (DbEntityValidationException ex)
{
foreach (var entityValidationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in entityValidationErrors.ValidationErrors)
{
Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
if(ex.InnerException != null)
{
Response.Write(ex.InnerException.Message);
}
}
}
}
But there were no changes.
EDIT 2:
Create View
@model Roadway_History.Models.Statewide
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Statewide</h4>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.COUNTY, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("COUNTY", new List<SelectListItem>
{
new SelectListItem {Text = "BARBOUR", Value="BARBOUR" },
new SelectListItem {Text = "BERKELEY", Value="BERKELEY" },
new SelectListItem {Text = "BOONE", Value="BOONE" },
new SelectListItem {Text = "BRAXTON", Value="BRAXTON" },
new SelectListItem {Text = "BROOKE", Value="BROOKE" },
new SelectListItem {Text = "CABELL", Value="CABELL" },
new SelectListItem {Text = "CALHOUN", Value="CALHOUN" },
new SelectListItem {Text = "CLAY", Value="CLAY" },
new SelectListItem {Text = "DODDRIDGE", Value="DODDRIDGE" },
new SelectListItem {Text = "FAYETTE", Value="FAYETTE" },
new SelectListItem {Text = "GILMER", Value="GILMER" },
new SelectListItem {Text = "GRANT", Value="GRANT" },
new SelectListItem {Text = "GREENBRIER", Value="GREENBRIER" },
new SelectListItem {Text = "HAMPSHIRE", Value="HAMPSHIRE" },
new SelectListItem {Text = "HANCOCK", Value="HANCOCK'" },
new SelectListItem {Text = "HARDY", Value="HARDY" },
new SelectListItem {Text = "HARRISON", Value="HARRISON" },
new SelectListItem {Text = "JACKSON", Value="JACKSON" },
new SelectListItem {Text = "JEFFERSON", Value="JEFFERSON" },
new SelectListItem {Text = "KANAWHA", Value="KANAWHA" },
new SelectListItem {Text = "LEWIS", Value="LEWIS" },
new SelectListItem {Text = "LINCOLN", Value="LINCOLN" },
new SelectListItem {Text = "LOGAN", Value="LOGAN" },
new SelectListItem {Text = "MCDOWELL", Value="MCDOWELL" },
new SelectListItem {Text = "MARION", Value="MARION" },
new SelectListItem {Text = "MARSHALL", Value="MARSHALL" },
new SelectListItem {Text = "MASON", Value="MASON" },
new SelectListItem {Text = "MERCER", Value="MERCER" },
new SelectListItem {Text = "MINERAL", Value="MINERAL" },
new SelectListItem {Text = "MINGO", Value="MINGO" },
new SelectListItem {Text = "MONONGALIA", Value="MONONGALIA" },
new SelectListItem {Text = "MONROE", Value="MONROE" },
new SelectListItem {Text = "MORGAN", Value="MORGAN" },
new SelectListItem {Text = "NICHOLAS", Value="NICHOLAS" },
new SelectListItem {Text = "OHIO", Value="OHIO" },
new SelectListItem {Text = "PENDLETON", Value="PENDLETON" },
new SelectListItem {Text = "PLEASANTS", Value="PLEASANTS" },
new SelectListItem {Text = "POCAHONTAS", Value="POCAHONTAS" },
new SelectListItem {Text = "PRESTON", Value="PRESTON" },
new SelectListItem {Text = "PUTNAM", Value="PUTNAM" },
new SelectListItem {Text = "RALEIGH", Value="RALEIGH'" },
new SelectListItem {Text = "RANDOLPH", Value="RANDOLPH" },
new SelectListItem {Text = "RITCHIE", Value="RITCHIE" },
new SelectListItem {Text = "ROANE", Value="ROANE" },
new SelectListItem {Text = "SUMMERS", Value="SUMMERS" },
new SelectListItem {Text = "TAYLOR", Value="TAYLOR" },
new SelectListItem {Text = "TUCKER", Value="TUCKER" },
new SelectListItem {Text = "TYLER", Value="TYLER" },
new SelectListItem {Text = "UPSHUR", Value="UPSHUR" },
new SelectListItem {Text = "WAYNE", Value="WAYNE" },
new SelectListItem {Text = "WEBSTER", Value="WEBSTER" },
new SelectListItem {Text = "WETZEL", Value="WETZEL" },
new SelectListItem {Text = "WIRT", Value="WIRT" },
new SelectListItem {Text = "WOOD", Value="WOOD" },
new SelectListItem {Text = "WYOMING", Value="WYOMING" }
}, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SignSys, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("SignSys", new List<SelectListItem>
{
new SelectListItem {Text = "CO", Value="CO" },
new SelectListItem {Text = "US", Value="US" },
new SelectListItem {Text = "WV", Value="WV" },
new SelectListItem {Text = "DE", Value="DE" },
new SelectListItem {Text = "P&F", Value="P&F" },
new SelectListItem {Text = "HA", Value="HA" },
new SelectListItem {Text = "ZMISC", Value="ZMISC" },
new SelectListItem {Text = "I", Value="I" },
new SelectListItem {Text = "PR", Value="PR" },
new SelectListItem {Text = "MNS", Value="MNS" },
new SelectListItem {Text = "USFS", Value="USFS" }
}, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RouteNoOrigImport, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RouteNoOrigImport, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RouteNoOrigImport, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RouteNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RouteNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RouteNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SuppDes, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("SuppDes", new List<SelectListItem>
{
new SelectListItem {Text = "Not Applicable", Value="00" },
new SelectListItem {Text = "Alternate", Value="01" },
new SelectListItem {Text = "Wye", Value="02" },
new SelectListItem {Text = "Spur", Value="03" },
new SelectListItem {Text = "North", Value="04" },
new SelectListItem {Text = "South", Value="05" },
new SelectListItem {Text = "East", Value="06" },
new SelectListItem {Text = "West", Value="07" },
new SelectListItem {Text = "Business", Value="08" },
new SelectListItem {Text = "North Bound", Value="09" },
new SelectListItem {Text = "South Bound", Value="10" },
new SelectListItem {Text = "East Bound", Value="11" },
new SelectListItem {Text = "West Bound", Value="12" },
new SelectListItem {Text = "Truck Route", Value="13" },
new SelectListItem {Text = "Bypass", Value="14" },
new SelectListItem {Text = "Loop", Value="15" },
new SelectListItem {Text = "Toll", Value="16" },
new SelectListItem {Text = "Ramp", Value="17" },
new SelectListItem {Text = "Other", Value="18" },
new SelectListItem {Text = "City St. Non-State", Value="19" },
new SelectListItem {Text = "Road Under Construction", Value="20" },
new SelectListItem {Text = "Footbridge", Value="21" },
new SelectListItem {Text = "Historical Bridge", Value="22" },
new SelectListItem {Text = "Connector", Value="23" },
new SelectListItem {Text = "New/Proposed", Value="24" },
new SelectListItem {Text = "Crossover", Value="25" },
new SelectListItem {Text = "Emergency Crossover", Value="26" },
new SelectListItem {Text = "Left Turn Lane", Value="27" },
new SelectListItem {Text = "Right Turn Lane", Value="28" },
new SelectListItem {Text = "Rail Trail", Value="51" },
new SelectListItem {Text = "Abandoned", Value="99" }
}, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LocalName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LocalName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LocalName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DateNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Method, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Method, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Method, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OriginalL, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OriginalL, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OriginalL, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OriginalD, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OriginalD, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OriginalD, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Projects, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Projects, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Projects, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Documents, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Documents, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Documents, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Comments, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comments, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.District, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.District, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.District, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Duplicate_OK, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Duplicate_OK)
@Html.ValidationMessageFor(model => model.Duplicate_OK, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RightofWay, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RightofWay, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RightofWay, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ReservedRoute, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.ReservedRoute)
@Html.ValidationMessageFor(model => model.ReservedRoute, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ReservedBy, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReservedBy, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReservedBy, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ReservedDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReservedDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReservedDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CP_WorkCompleted, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.CP_WorkCompleted)
@Html.ValidationMessageFor(model => model.CP_WorkCompleted, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Work_Comments, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Work_Comments, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Work_Comments, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CP_ProjectNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CP_ProjectNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CP_ProjectNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ReservedCat, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReservedCat, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReservedCat, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Statewide Model
//------------------------------------------------------------------------------
// <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 Roadway_History.Models
{
using System;
using System.Collections.Generic;
public partial class Statewide
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Statewide()
{
this.Documents1 = new HashSet<Document>();
}
public int ID { get; set; }
public string COUNTY { get; set; }
public string SignSys { get; set; }
public string RouteNoOrigImport { get; set; }
public Nullable<int> RouteNo { get; set; }
public Nullable<int> SuppDes { get; set; }
public string LocalName { get; set; }
public Nullable<System.DateTime> DateNumber { get; set; }
public string Method { get; set; }
public string OriginalL { get; set; }
public string OriginalD { get; set; }
public string Projects { get; set; }
public string Documents { get; set; }
public string Comments { get; set; }
public Nullable<int> District { get; set; }
public bool Duplicate_OK { get; set; }
public string RightofWay { get; set; }
public bool ReservedRoute { get; set; }
public string ReservedBy { get; set; }
public Nullable<System.DateTime> ReservedDate { get; set; }
public bool CP_WorkCompleted { get; set; }
public string Work_Comments { get; set; }
public string CP_ProjectNo { get; set; }
public Nullable<int> ReservedCat { get; set; }
public string Add_User { get; set; }
public Nullable<System.DateTime> Date_Added { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Document> Documents1 { get; set; }
}
}
And here is the database table:
And here is a quick example of some data that works fine if I add it to the database directly through the Management Studio but does not work in the application