Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ to SQL INSERT Failing
    text
    copied!<p>I am experiencing a very frustrating issue when trying to insert a new record using LINQ to SQL. If I step through this code sometimes it inserts the new record but most of the time it doesn't. When it fails I seeing the following error.</p> <blockquote> <p>Cannot insert the value NULL into column 'Name', table 'EquipmentManufacturer'; column does not allow nulls. INSERT fails. The statement has been terminated.</p> </blockquote> <p>This error is complaining about the 'Name' field being null but that should not be the case. When I debug and step through this collection["Name"] has the value I entered on the form.</p> <p>Here is the table create statement.</p> <pre><code>CREATE TABLE [EquipmentManufacturer] ( [EquipmentManufacturerID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NOT NULL, CONSTRAINT [PK_EquipmentManufacturer] PRIMARY KEY CLUSTERED ( [EquipmentManufacturerID] ASC ) ON [PRIMARY] ) ON [PRIMARY] </code></pre> <p>Here is the ASP.NET MVC Controller and Create Action where I am trying to add a new record.</p> <pre><code>public partial class EquipmentManufacturerController : Controller { private IRepository&lt;EquipmentManufacturer&gt; reposManu; // POST: /EquipmentManufacturer/Create [AcceptVerbs(HttpVerbs.Post)] public virtual ActionResult Create(FormCollection collection) { EquipmentManufacturer entity = reposManu.New(); try { //HACK: Something screwy is going on here the entity oject doesn't always get updated correctly //UpdateModel(entity); entity.Name = collection["Name"]; reposManu.Insert(entity); reposManu.SubmitChanges(); return RedirectToAction("Details", new { id = entity.EquipmentManufacturerID }); } catch (RulesException ex) { ex.AddModelStateErrors(ModelState, "EquipmentManufacturer"); return ModelState.IsValid ? RedirectToAction("Create") : (ActionResult)View(); } } } </code></pre> <p>Here is the Create.aspx view.</p> <pre><code>&lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;h2&gt;Create&lt;/h2&gt; &lt;%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %&gt; &lt;% using (Html.BeginForm()) {%&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;p&gt; &lt;label for="Name"&gt;Name:&lt;/label&gt; &lt;%= Html.TextBox("Name") %&gt; &lt;%= Html.ValidationMessage("Name") %&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; &lt;%= Html.ClientSideValidation&lt;EquipmentManufacturer&gt;() %&gt; &lt;div&gt; &lt;%=Html.ActionLink("Back to List", "Index") %&gt; &lt;/div&gt; &lt;/asp:Content&gt; </code></pre> <p>Here is the Repository implementation I am using.</p> <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class { public IDataContext DC { get; set; } public Repository(IDataContext dataContext) { DC = dataContext; } /// &lt;summary&gt; /// Return all instances of type T. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public IEnumerable&lt;T&gt; All() { return GetTable; } /// &lt;summary&gt; /// Return all instances of type T that match the expression exp. /// &lt;/summary&gt; /// &lt;param name="exp"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public IQueryable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; exp) { return GetTable.Where&lt;T&gt;(exp); } /// &lt;summary&gt;See IRepository&lt;/summary&gt; /// &lt;param name="exp"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public T Single(Expression&lt;Func&lt;T, bool&gt;&gt; exp) { return GetTable.SingleOrDefault(exp); } /// &lt;summary&gt;See IRepository&lt;/summary&gt; /// &lt;param name="exp"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public T First(Expression&lt;Func&lt;T, bool&gt;&gt; exp) { return GetTable.First(exp); } /// &lt;summary&gt;See IRepository&lt;/summary&gt; /// &lt;param name="entity"&gt;&lt;/param&gt; public virtual void Delete(T entity) { DC.Context.GetTable&lt;T&gt;().DeleteOnSubmit(entity); } /// &lt;summary&gt; /// Create a new instance of type T. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public virtual T New() { T entity = Activator.CreateInstance&lt;T&gt;(); GetTable.InsertOnSubmit(entity); return entity; } /// &lt;summary&gt; /// Adds an insance T. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public virtual void Insert(T entity) { GetTable.InsertOnSubmit(entity); } /// &lt;summary&gt; /// Update entity. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public virtual void Update(T entity) { DC.Context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, entity); } /// &lt;summary&gt;See IRepository&lt;/summary&gt; public void SubmitChanges() { DC.SubmitChanges(); } private string PrimaryKeyName { get { return TableMetadata.RowType.IdentityMembers[0].Name; } } private System.Data.Linq.Table&lt;T&gt; GetTable { get { return DC.Context.GetTable&lt;T&gt;(); } } private System.Data.Linq.Mapping.MetaTable TableMetadata { get { return DC.Context.Mapping.GetTable(typeof(T)); } } private System.Data.Linq.Mapping.MetaType ClassMetadata { get { return DC.Context.Mapping.GetMetaType(typeof(T)); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload