Hello Team,ly
Please I keep getting this generic error, Object reference not set to an instance of an object, frequently in my code, and also the profit and tax is not calculating it value,kindly assist.

public ActionResult SaveSales(SalesModel ObjSalesModel)
{
if (ObjSalesModel == null || ObjSalesModel.tblSales == null)
{
return Json(new { result = false, message = "Invalid data received." }, JsonRequestBehavior.AllowGet);
}
try
{
decimal Tax = 0;
decimal CostPrice = 0;
decimal SellingPrice = 0;
decimal vat = Tax;
decimal costPrice = CostPrice;
decimal sellingPrice = SellingPrice;
decimal Profit = sellingPrice - costPrice;
decimal Vatpercent = (vat / 100) * sellingPrice;
tblSale ObjSales = new tblSale();
ObjSales.OrderDate = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
ObjSales.TransactionNo = string.Format("{0:ddMMMyyyyHHmmss}", DateTime.Now);
ObjSales.Amount = ObjSalesModel.tblSales.Amount;
ObjSales.Discount = ObjSalesModel.tblSales.Discount;
ObjSales.Tax = Vatpercent;
ObjSales.GrandTotal = ObjSalesModel.tblSales.GrandTotal;
ObjSales.Profit = Profit;
//ObjSales.UserID = ObjSalesModel.UserID;
objASPNETPHARMACYDBEntities.tblSales.Add(ObjSales);
objASPNETPHARMACYDBEntities.SaveChanges();
foreach (var sales in ObjSalesModel.SalesItems)
{
tblSalesItem ObjSalesItem = new tblSalesItem();
ObjSalesItem.StockID = sales.StockID;
ObjSalesItem.SalesID = ObjSales.SalesID; // Use the newly generated SalesID
ObjSalesItem.Qty = sales.Qty;
ObjSalesItem.Rate = sales.Rate;
ObjSalesItem.Amount = sales.Amount;
objASPNETPHARMACYDBEntities.tblSalesItems.Add(ObjSalesItem);
objASPNETPHARMACYDBEntities.SaveChanges();
var ObjtblStock = objASPNETPHARMACYDBEntities.tblStocks.Where(x => x.StockID == sales.StockID).FirstOrDefault();
if (ObjtblStock != null)
{
decimal itemcostPrice = Convert.ToDecimal(ObjtblStock.CostPrice);
decimal salesPrice = Convert.ToDecimal(ObjtblStock.SellingPrice);
decimal profit = salesPrice - itemcostPrice;
decimal vatAmount = (vat / 100) * sellingPrice;
// Update the sales item with calculated values
sales.Amount = Convert.ToDecimal(sellingPrice * sales.Qty);
ObjSales.Profit = profit * sales.Qty;
ObjSales.Tax = vatAmount * sales.Qty;
ObjtblStock.Qty = ObjtblStock.Qty - sales.Qty;
objASPNETPHARMACYDBEntities.Entry(ObjtblStock).State = EntityState.Modified;
objASPNETPHARMACYDBEntities.SaveChanges();
}
}
return Json(new { result = true, message = "Sales successfully saved!", TransactionNo = ObjSales.TransactionNo }, JsonRequestBehavior.AllowGet);
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
Exception raise = dbEx;
foreach (var vailidationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in vailidationErrors.ValidationErrors)
{
string message = string.Format("{0}:{1}",
vailidationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
raise = new InvalidOperationException(message, raise);
}
}
throw raise;
//return Json(new { result = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
Emmmanuel FIADUFEPosted Feb 10, 2025, 8:39 AM
Yes Amit,
After updating the code, when input data into the client side and save it, it didn't trigger any action so my assumption is that SalesItems table is not receiving any data from the loop, and only sales record got save in the sales table, I have severally updated the SalesID and the StockID in the SalesItems table but it isn't working.
Amit MohantyPosted Feb 10, 2025, 5:36 AM
You’re checking if ObjSalesModel.tblSales is null, but you may also want to check whether ObjSalesModel.SalesItems is null or empty before trying to loop through it.
The above code also checks if SalesItems is null or if it contains no elements before entering the loop.
Emmmanuel FIADUFEPosted Feb 9, 2025, 11:30 PM
Hello team,
Please any help will be highly appreciated.
Thanks
Emmmanuel FIADUFEPosted Feb 8, 2025, 2:52 PM
The error occures on the Loop