I have a JavaScript function that looks like this
<script type="text/javascript">
function add() {
var token = $("input[name='__RequestVerificationToken']", "#__AjaxAntiForgeryForm").val();
var parts = [], partsqty = [];
// Get all part ids and quantities and store them in arrays
var partssel = document.getElementsByClassName("Part-select");
var partsqtysel = document.getElementById('Part-qty').value;
for (i = 0; i < partssel.length; i++) {
parts[i] = partssel[i].options[partssel[i].selectedIndex].value;
partsqty[i] = partsqtysel;
if (partsqty[i] < 0) {
alert("Quantities can't be negative!");
return;
}
}
alert("I am an alert box too!");
$.ajax({
type: "POST",
url: "@IGT.baseUrl/JODetailsAjax/AddUnits",
traditional: true,
data: {
__RequestVerificationToken: token,
jo_id: @Model.Id,
addPart_id: parts,
addPart_qty: partsqty
},
success: function (data) {
if (data.success === "False") {
var errorMessage = data.Message;
alert("Error: " + errorMessage);
return;
}
if(data.success === "True"){
location.href = "../JobOrders/Details?id=@Model.Id";
}
},
error: function (jqXHR, status, error) {
alert("Error:" + error);
}
});
}
</script>
Which calls my 'AddUnits' function that looks like this
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult AddUnits(int jo_id, int[] addPart_id, float[] addPart_qty)
{
bool canCreate = true;
string errors = "";
for (int i = 0; i < addPart_id.Length; i++)
{
foreach (JODetails jod in db.JobOrders.Find(jo_id).JODetails)
{
if (i < addPart_id.Length && addPart_qty[i] != 0 && jod.PartID == addPart_id[i])
{
jod.part_qty += addPart_qty[i];
addPart_id[i] = 0;
addPart_qty[i] = 0;
}
}
db.SaveChanges();
if (i < addPart_qty.Length && addPart_qty[i] != 0)
{
JODetails jODetails = new JODetails
{
JobOrderID = jo_id
};
// Only add a unit to the JODetails object if it's not null and has an id and quanitity specified
if (i < addPart_id.Length && addPart_id[i] != 0 && addPart_qty[i] != 0)
{
jODetails.PartID = addPart_id[i];
jODetails.part_qty = addPart_qty[i];
}
JobOrder JO = db.JobOrders.Find(jODetails.JobOrderID);
JODetails jobOrderDetails = db.JODetails.Add(jODetails);
jobOrderDetails.JobOrder = JO;
Part JO_Part = db.Parts.Find(jODetails.PartID);
if (JO_Part != null)
{
jODetails.part_qty = jODetails.part_qty == null ? 0 : jODetails.part_qty;
float qtyOrdered = jODetails.part_qty == null ? 0 : (float)jODetails.part_qty;
jobOrderDetails.dynamicPart_qty = qtyOrdered;
}
if (!canCreate)
{
var errorMessage = string.Join(",", errors);
var stock = new { success = "False", Message = errorMessage };
return Json(stock, JsonRequestBehavior.AllowGet);
}
db.JODetails.Add(jODetails);
}
}
db.SaveChanges(); // error on this line
JobOrder jobOrder = db.JobOrders.Find(jo_id);
bool hasParts = false;
foreach (JODetails jod in jobOrder.JODetails)
{
if (jod.Parts != null && jod.part_qty > 0)
hasParts = true;
}
ViewBag.hasParts = hasParts;
var result = new { success = "True" };
return Json(result);
}
But everytime it runs through the method theres an error at the db.SaveChanges();
which says
SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
As you can see nothing in my code is even referencing a date so I'm not sure why this problem is occurring? I have searched for answers on the internet but everybody seems to have been dealing with dates when they experienced the issue.
Any help is appreciated.