protected void saveButton_Click(object sender, EventArgs e) { outputLabel.Text = null; string tempStockOutQuantity = stockOutTextBox.Text; int tempCompanyId = Convert.ToInt32(companyDropDownList.SelectedValue); int tempItemId = Convert.ToInt32(itemDropDownList.SelectedValue); outputLabel.Text = stockOutManager.SetValidation(tempStockOutQuantity, tempCompanyId, tempItemId); if (stockOutManager.PassValidation()) { if (ViewState["stockOutListVS"] == null) { StockOut stockOut = new StockOut(); stockOut.CompanyId = Convert.ToInt32(companyDropDownList.SelectedValue); stockOut.CompanyName = companyDropDownList.SelectedItem.Text; stockOut.ItemName = itemDropDownList.SelectedItem.Text; stockOut.ItemId = Convert.ToInt32(itemDropDownList.SelectedValue); stockOut.StockOutQuantity = Convert.ToInt32(stockOutTextBox.Text); int tempStockOut = stockOut.StockOutQuantity; int checkVS = 0; checkVS = stockOutManager.CheckBefore(tempStockOut, stockOut.ItemName, stockOut.CompanyId); if (checkVS > 0) { outputLabel.Text = "Quantity of " + stockOut.ItemName + " is insufficent"; } else { List <StockOut> stockOutList = new List <StockOut>(); stockOutList.Add(stockOut); ViewState["stockOutListVS"] = stockOutList; } } else { List <StockOut> stockOut = (List <StockOut>)ViewState["stockOutListVS"]; StockOut newStockOut = new StockOut(); int check = 0; newStockOut.CompanyName = companyDropDownList.SelectedItem.Text; newStockOut.CompanyId = Convert.ToInt32(companyDropDownList.SelectedValue); newStockOut.ItemName = itemDropDownList.SelectedItem.Text; newStockOut.ItemId = Convert.ToInt32(itemDropDownList.SelectedValue); newStockOut.StockOutQuantity = Convert.ToInt32(stockOutTextBox.Text); foreach (StockOut aStockOut in stockOut) { if (aStockOut.CompanyName == newStockOut.CompanyName && aStockOut.ItemName == newStockOut.ItemName) { check = 1; int q = aStockOut.StockOutQuantity; int tempStockOut = q + newStockOut.StockOutQuantity; int checkVS = 0; checkVS = stockOutManager.CheckBefore(tempStockOut, aStockOut.ItemName, aStockOut.CompanyId); if (checkVS > 0) { outputLabel.Text = "Quantity of " + aStockOut.ItemName + " is insufficent"; } else { aStockOut.StockOutQuantity = q + newStockOut.StockOutQuantity; newStockOut.StockOutQuantity = aStockOut.StockOutQuantity; } } } if (check == 0) { int tempStockOut = newStockOut.StockOutQuantity; int checkVS = 0; checkVS = stockOutManager.CheckBefore(tempStockOut, newStockOut.ItemName, newStockOut.CompanyId); if (checkVS > 0) { outputLabel.Text = "Quantity of " + newStockOut.ItemName + " is insufficent"; } else { stockOut.Add(newStockOut); ViewState["stockOutListVS"] = stockOut; } } } } List <StockOut> aNewstockOut = (List <StockOut>)ViewState["stockOutListVS"]; stockOutGridView.DataSource = (List <StockOut>)ViewState["stockOutListVS"]; stockOutGridView.DataBind(); }