Hello Team, I use the below method/function to save data and as well to update that, saving data work fine but whem I tried updating the data I get duplication of data, and this happen in the tblQuantity. I have the following table tblCategory, tblProduct, and tblQuantity. can anyone help me out.
public ActionResult SaveItem(ItemViewModel itemModel)
{
var result = false;
try
{
decimal vat = itemModel.Vat;
decimal costPrice = itemModel.CostPrice;
decimal sellingPrice =itemModel.ItemPrice;
decimal Profit = sellingPrice - costPrice;
decimal Vatpercent = (vat / 100) * sellingPrice;
tblCategory objCategory = new tblCategory();
if (itemModel.CategoryId > 0)
{
objCategory = objRestaurantDBEntities.tblCategories.FirstOrDefault(x => x.CategoryId == itemModel.CategoryId);
}
else
{
objCategory.CategoryName = itemModel.CategoryName;
objRestaurantDBEntities.tblCategories.Add(objCategory);
objRestaurantDBEntities.SaveChanges();
// Assign the generated CategoryId after save
itemModel.CategoryId = objCategory.CategoryId;
}
tblItem objItem = new tblItem();
if (itemModel.ItemId > 0)
{
objItem = objRestaurantDBEntities.tblItems.FirstOrDefault(x => x.ItemId == itemModel.ItemId);
}
objItem.PCode = itemModel.PCode;
objItem.ItemName = itemModel.ItemName;
objItem.CostPrice = itemModel.CostPrice;
objItem.ItemPrice = itemModel.ItemPrice;
objItem.Profit = Profit;
objItem.Vat = Vatpercent;
objItem.CategoryId = itemModel.CategoryId;
objItem.Active = itemModel.Active;
if (itemModel.ItemId <= 0)
{
objRestaurantDBEntities.tblItems.Add(objItem);
}
tblQuantity objQnty = new tblQuantity();
if (itemModel.QuantityId > 0)
{
objQnty = objRestaurantDBEntities.tblQuantities.FirstOrDefault(x => x.QuantityId == itemModel.QuantityId);
}
objQnty.Quantity = itemModel.Quantity;
objQnty.ItemId = itemModel.ItemId;
if (itemModel.QuantityId <= 0)
{
objRestaurantDBEntities.tblQuantities.Add(objQnty);
//objRestaurantDBEntities.SaveChanges();
//itemModel.QuantityId = objQnty.QuantityId;
}
objRestaurantDBEntities.SaveChanges();
result = true;
}
catch (Exception ex)
{
// Log the exception or handle it appropriately
// Logging ex: Logger.Log(ex);
// Return a meaningful error message
return Json(new { success = false, message = "An error occurred while saving the item." });
}
return Json(new { success = result, message = "Item saved successfully." });
}
function UpdateforProduct(ItemId) {
debugger;
var itemModel = new Object();
var objQnty = new Object();
itemModel.PCode = $("#pCode").val();
itemModel.CategoryId = $("#categoryId").val();
itemModel.QuantityId = $("#qtyId").val();
itemModel.Quantity = $("#quantity").val();
itemModel.ItemId = $("#itemId").val();
itemModel.ItemName = $("#itemName").val();
itemModel.CostPrice = $("#txtCostPrice").val();
itemModel.ItemPrice = $("#itemPrice").val();
itemModel.Vat = $("#vat").val();
itemModel.Active = $("#status").val();
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'JSON',
type: 'POST',
url: '@Url.Action("SaveItem", "Home")',
data: JSON.stringify({
itemModel: itemModel
}),
success: function (response) {
if (response.message) {
//alert(response.message = "Success")
toastr.success(response.message = "Product successfully updated!");
DataTable.ajax.reload();
$("#exampleModal").modal('hide');
}
},
error: function (msg) {
toastr.danger(msg.responseText);
}
});
}

Jignesh KumarRank #91 · 22.9K reputationPosted Dec 11, 2024, 9:34 PM EST
Hello Emmanuel,
Please use this query,
var result = false; try { decimal vat = itemModel.Vat; decimal costPrice = itemModel.CostPrice; decimal sellingPrice = itemModel.ItemPrice; decimal Profit = sellingPrice - costPrice; decimal Vatpercent = (vat / 100) * sellingPrice; // Category tblCategory objCategory = itemModel.CategoryId > 0 ? objRestaurantDBEntities.tblCategories.FirstOrDefault(x => x.CategoryId == itemModel.CategoryId) : objRestaurantDBEntities.tblCategories.FirstOrDefault(x => x.CategoryName == itemModel.CategoryName) ?? new tblCategory { CategoryName = itemModel.CategoryName }; if (objCategory.CategoryId == 0) { objRestaurantDBEntities.tblCategories.Add(objCategory); objRestaurantDBEntities.SaveChanges(); } itemModel.CategoryId = objCategory.CategoryId; // Item tblItem objItem = itemModel.ItemId > 0 ? objRestaurantDBEntities.tblItems.FirstOrDefault(x => x.ItemId == itemModel.ItemId) : objRestaurantDBEntities.tblItems.FirstOrDefault(x => x.PCode == itemModel.PCode || x.ItemName == itemModel.ItemName) ?? new tblItem(); objItem.PCode = itemModel.PCode; objItem.ItemName = itemModel.ItemName; objItem.CostPrice = costPrice; objItem.ItemPrice = sellingPrice; objItem.Profit = Profit; objItem.Vat = Vatpercent; objItem.CategoryId = itemModel.CategoryId; objItem.Active = itemModel.Active; if (objItem.ItemId == 0) { objRestaurantDBEntities.tblItems.Add(objItem); } // Quantity tblQuantity objQnty = itemModel.QuantityId > 0 ? objRestaurantDBEntities.tblQuantities.FirstOrDefault(x => x.QuantityId == itemModel.QuantityId) : objRestaurantDBEntities.tblQuantities.FirstOrDefault(x => x.ItemId == objItem.ItemId) ?? new tblQuantity(); objQnty.Quantity = itemModel.Quantity; objQnty.ItemId = objItem.ItemId; if (objQnty.QuantityId == 0) { objRestaurantDBEntities.tblQuantities.Add(objQnty); } objRestaurantDBEntities.SaveChanges(); result = true; } catch (Exception ex) { // Log the exception or handle it appropriately return Json(new { success = false, message = "An error occurred while saving the item." }); } return Json(new { success = result, message = "Item saved successfully." });Sangeetha SRank #212 · 11.4K reputationPosted Dec 11, 2024, 5:11 AM EST
public ActionResult SaveItem(ItemViewModel itemModel) { var result = false; try { decimal vat = itemModel.Vat; decimal costPrice = itemModel.CostPrice; decimal sellingPrice = itemModel.ItemPrice; decimal Profit = sellingPrice - costPrice; decimal Vatpercent = (vat \/ 100) * sellingPrice; \/\/ Handling category tblCategory objCategory = new tblCategory(); if (itemModel.CategoryId > 0) { objCategory = objRestaurantDBEntities.tblCategories.FirstOrDefault(x => x.CategoryId == itemModel.CategoryId); } else { objCategory.CategoryName = itemModel.CategoryName; objRestaurantDBEntities.tblCategories.Add(objCategory); objRestaurantDBEntities.SaveChanges(); itemModel.CategoryId = objCategory.CategoryId; \/\/ Assign the generated CategoryId after save } \/\/ Handling item tblItem objItem = new tblItem(); if (itemModel.ItemId > 0) { objItem = objRestaurantDBEntities.tblItems.FirstOrDefault(x => x.ItemId == itemModel.ItemId); } objItem.PCode = itemModel.PCode; objItem.ItemName = itemModel.ItemName; objItem.CostPrice = itemModel.CostPrice; objItem.ItemPrice = itemModel.ItemPrice; objItem.Profit = Profit; objItem.Vat = Vatpercent; objItem.CategoryId = itemModel.CategoryId; objItem.Active = itemModel.Active; if (itemModel.ItemId 0) { objQnty = objRestaurantDBEntities.tblQuantities.FirstOrDefault(x => x.QuantityId == itemModel.QuantityId); if (objQnty != null) { \/\/ Update existing quantity objQnty.Quantity = itemModel.Quantity; objQnty.ItemId = itemModel.ItemId; } } else { \/\/ Create new quantity if QuantityId is not set objQnty = new tblQuantity { Quantity = itemModel.Quantity, ItemId = itemModel.ItemId }; objRestaurantDBEntities.tblQuantities.Add(objQnty); } objRestaurantDBEntities.SaveChanges(); result = true; } catch (Exception ex) { \/\/ Log the exception or handle it appropriately return Json(new { success = false, message = \"An error occurred while saving the item.\" }); } return Json(new { success = result, message = \"Item saved successfully.\" }); }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 11, 2024, 6:32 AM EST
Hello Sangeetha S
It still duplicate after remodifying the code.
Sangeetha SRank #212 · 11.4K reputationPosted Dec 11, 2024, 6:53 AM EST
Hi,
For removing duplicate entry keep the particular column as primary key and try
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 11, 2024, 7:14 AM EST
Hello Sangeetha S
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 11, 2024, 12:01 PM EST
Hello team, please any additional help will be highly appreciated.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 12, 2024, 4:21 AM EST
Thank you all team,
and especially Jignesh Kumar , it is working now
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 12, 2024, 4:49 AM EST
Hello Kumar
But one issue just occured now after applying your code, the product status have active and inactive with the color red and green respectively, but after remodifying the code now the status color remains constant, whether I select active or inactive the color doesn't change.