I am currently implementing a goto
statement in my code but upon debugging I notice that the labeled statement is also executed even I am not calling the goto
statement. Is my code correct about implementing to goto
statement? Thanks for any help.
Below is my code structure and the "InsertAdd" labeled statement is also executed even the flow goes to else statement. Am I missing some codes or logic? Thanks.
I don't want to repeat my code in every if statement that's why I use goto
. If you could also suggest other method is also much appreciated.
if (id == -107) goto InsertAdd;
else if (totalAllTk + qty <= 24) goto InsertAdd;
else statusChk = "FULL";
InsertAdd:
if (itemExists)
{
Cart exists = (from item in db.Carts
where item.ProductId == id && item.SessionId == sessionId && item.SizeId == id select item).FirstOrDefault();
newQuantity = exists.Quantity + qty;
exists.Quantity = newQuantity;
db.SaveChanges();
}
else
{
Cart newItem = new Cart()
{
ProductId = id,
CreatedBy = userID,
SessionId = sessionId,
Quantity = qty,
SizeId = id,
Size = tkType,
CreatedOn = DateTime.Now.ToUniversalTime()
};
db.Carts.Add(newItem);
db.SaveChanges();
newQuantity = qty;
}