简体   繁体   中英

How to make validation with for loop inside if statement

Hello this below code that deduct product quantity from the stock based on product quantity in grid view with id = gridagree

the issue is i want to deduct the products quantity with category =1 and put validation to check that all products quantity in gridagree if its bigger than the quantity in stock or not and if one product quantity bigger than the quantity in stoack the code should not executed

Category Id =gridagree.Rows[index].Cells[7].Text

Product Id = gridagree.Rows[index].Cells[3].Text)

Product Quantity in the grid =gridagree.Rows[index].Cells[5].Text

StoreClass s = new StoreClass();

if(//suggested code)
{

lblmsg.Text="Product quantity is bigger than current balance please change product quantity and try again"// here also can i determine to the user which product has the issue product with id=?
}

else
{
for (int index = 0; index < this.gridagree.Rows.Count; index++)
                    {
                        if (gridagree.Rows[index].Cells[7].Text == "1")
                        {
                            string qun = s.getprodqun(Convert.ToInt32(gridagree.Rows[index].Cells[3].Text));
                            s.subtract(Convert.ToInt32(gridagree.Rows[index].Cells[3].Text), Convert.ToInt32(qun) - Convert.ToInt32(gridagree.Rows[index].Cells[5].Text));
                        }

                    }
}

I am steering away from commenting on the code, and will just provide you with what I believe you're asking. With how you have things coded in your sample, you need another loop...

int categoryIdIndex = 7;
int productIdIndex = 3;
int quantityRequestedIndex = 5;

List<string> errors = new List<string>()
for (int index = 0; index < this.gridagree.Rows.Count; index++)
{
  Row row = gridagree.Rows[index];
  int categoryId = Convert.ToInt32(row.Cells[categoryIdIndex].Text);
  int productId = Convert.ToInt32(row.Cells[productIdIndex].Text);
  int quantityRequested = Convert.ToInt32(row.Cells[quantityRequestedIndex].Text);
  int availableQuantity = s.getprodqun(productId);

  if(quantityRequested > availableQuantity)
  {
    errors.Add(string.Format("Inventory contains insufficient items for product id {0} ", productId));
  }
}

if(errors.Count == 0)
{
  ... process the orders ...
}
else
{
  ... show the errors
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM