I'm encountering an error when trying to update a record in ASP.NET Core.
Microsoft.Data.SqlClient.SqlException: 'The parameterized query '(@EmployeeID int,@FirstName nvarchar(4000),@LastName nvarchar(40' expects the parameter '@FirstName', which was not supplied.'
public class Employee
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public int? ReportsTo { get; set; }
}
public void Update(Employee employee)
{
string str = Configuration.GetConnectionString("dbConnection");
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = str;
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "UPDATE Employees SET LastName=@LastName, FirstName=@FirstName,City=@City WHERE EmployeeID=@EmployeeID";
cmd.Parameters.AddWithValue("@EmployeeID", employee.EmployeeID);
cmd.Parameters.AddWithValue("@FirstName", employee.FirstName);
cmd.Parameters.AddWithValue("@LastName", employee.LastName);
cmd.Parameters.AddWithValue("@City", employee.City);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
public ActionResult UpdateEmployee(int? id)
{
return View(GetEmployees().Find(e => e.EmployeeID == id));
}
[HttpPost]
public JsonResult UpdateEmployee(Employee emp)
{
Update(emp);
return Json("Records updated successfully.");
}
@model CrudWithADONetAjaxjQuery.Models.Employee
@{
ViewBag.Title = "Update Employee";
}
<h2>Update Employee</h2>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script>
$(document).ready(function () {
$("#btnUpdate").click(function () {
var employee =
{
EmployeeID: $("#hfEmployeeID").val(),
FirstName: $("#FirstName").val(),
LastName: $("#LastName").val(),
City: $("#City").val()
};
$.ajax({
type: "POST", URL: "/Home/UpdateEmployee", dataType: "json", contentType: "application/json",
data: JSON.stringify({ emp: employee }),
success: function (response) { window.location.href = "/Home/AddEmployee"; },
error: function (response) { alert(response.responseText); }
});
});
});
</script>
<div class="form-horizontal">
<h4></h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.EmployeeID, new { @id = "hfEmployeeID" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="btnUpdate" value="Update" class="btn btn-default" />
</div>
</div>
</div>