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 KumarPosted Dec 12, 2024, 2:34 AM
Hello Emmanuel,
Please use this query,
Emmmanuel FIADUFEPosted Dec 12, 2024, 9:49 AM
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.
Emmmanuel FIADUFEPosted Dec 12, 2024, 9:21 AM
Thank you all team,
and especially Jignesh Kumar , it is working now
Emmmanuel FIADUFEPosted Dec 11, 2024, 5:01 PM
Hello team, please any additional help will be highly appreciated.
Emmmanuel FIADUFEPosted Dec 11, 2024, 12:14 PM
Hello Sangeetha S
Sangeetha SPosted Dec 11, 2024, 11:53 AM
Hi,
For removing duplicate entry keep the particular column as primary key and try
Emmmanuel FIADUFEPosted Dec 11, 2024, 11:32 AM
Hello Sangeetha S
It still duplicate after remodifying the code.
Sangeetha SPosted Dec 11, 2024, 10:11 AM