protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (LastADEntities ctx = new LastADEntities()) { //RetrievalListSimple is a method that consolidates the quantity of the items to collect from the warehouse // and will display this data in a gridview if (Session["a"] != null) { list2 = (ArrayList)Session["a"]; GridView1.DataSource = Business.RetrievalListSimple(list2); GridView1.DataBind(); } int quantityneed; String id; ItemList items; for (int i = 0; i < this.GridView1.Rows.Count; i++) { TextBox txt = (TextBox)this.GridView1.Rows[i].Cells[5].FindControl("TextBox1"); if (txt != null) { Control ctrl = GridView1.Rows[i].Cells[2].FindControl("Label7"); Label lab = ctrl as Label; id = lab.Text; items = Business.FindItemByID(id).First(); quantityneed = Business.GetTotalQtyNeededForSelectedItem(id, list2); //if we have more stock than requested quantity, we will retrieve the requested quantity //else we will only retrieve the amount we have in the store if (items.Stock > quantityneed) { txt.Text = quantityneed.ToString(); } else { txt.Text = items.Stock.ToString(); } } } } } }