Wednesday, September 2, 2015

Store update, insert, or delete statement affected an unexpected number of rows (0)

As I was creating a simple Create, Update, Delete (CRUD) application in ASP.NET MVC 5 and executing the controller on Edit, I encountered an error called "Store update, insert, or delete statement affected an unexpected number of rows (0)".
After debugging and looking at the stack trace, I found out that the value of the primary key is zero.
  Department: "Education"<br>  
   Designation: "Dean"<br>  
   EmployeeName: "JE"<br>  
   EmployeeID: 0<br>  
   Salary: 25500<br>  
The fix for this issue is to add a hidden field to the edit view referencing to the primary key which in this case is the EmployeeID.
 @Html.HiddenFor(model => model.EmployeeID)  
And now, the model passed to the context now has a value for the EmployeeID field.
  Department: "Education"<br>  
   Designation: "Dean"<br>  
   EmployeeName: "JE"<br>  
   EmployeeID: 2<br>  
   Salary: 25500<br>  
Cheers! :)

0 comments:

Post a Comment