private void btnAdd_Click(object sender, EventArgs e) { int q; if (String.IsNullOrWhiteSpace(txtQuantity.Text)) { MessageBox.Show("Please enter quantity first!", "Error"); } else if (!(Int32.TryParse(txtQuantity.Text, out q))) { MessageBox.Show("Please enter valid quantity!", "Error!"); } else { int CheckID = Convert.ToInt32(dgvProducts.SelectedRows[0].Cells[0].Value); foreach (DataRow row in CartTable.Rows) { if (Convert.ToInt32(row[0]) == CheckID) { int Pid = Convert.ToInt32(dgvProducts.SelectedRows[0].Cells[0].Value); int Rotquantity = Convert.ToInt32(txtQuantity.Text); int AvailableQuantity = (int)(dgvProducts.SelectedRows[0].Cells[5].Value); if (Rotquantity > AvailableQuantity) { if (MessageBox.Show(GeneralMethods.GetProductNameByID(Pid) + " is not available in the desired quantity. Do you wish to add the complete quantity avaialable i.e " + AvailableQuantity + " to the cart?", "Oops", MessageBoxButtons.YesNo) == DialogResult.Yes) { Rotquantity = AvailableQuantity; //float a = Convert.ToSingle(dgvProducts.SelectedRows[0].Cells[4].Value); ////float b = Convert.ToInt32(txtQuantity.Text); //float rottotalamount = a * Rotquantity; //string pname = ((GeneralMethods.GetProductByID(Pid)).Rows[0][2]).ToString(); //CartTable.Rows.Add(Pid, pname, Rotquantity, rottotalamount); int Amount = Convert.ToInt32(dgvProducts.SelectedRows[0].Cells[4].Value); //int Quantity = Convert.ToInt32(dgvProducts.SelectedRows[0].Cells[5].Value); int Quantity = AvailableQuantity; float Price = Amount; //int Profit = GeneralMethods.GetProfitRate(CheckID); //float SellingPrice = ((Price / 100) * Profit) + Price; row[2] = Quantity; row[3] = Price * Quantity; //dgvProducts.SelectedRows[0].Cells[5].Value = (Convert.ToInt32(dgvProducts.SelectedRows[0].Cells[5].Value)) - (Convert.ToInt32(txtQuantity.Text)); return; //break; } } else { int Amount = Convert.ToInt32(dgvProducts.SelectedRows[0].Cells[4].Value); int Quantity = Convert.ToInt32(dgvProducts.SelectedRows[0].Cells[5].Value); //int Quantity = AvailableQuantity; float Price = Amount; //int Profit = GeneralMethods.GetProfitRate(CheckID); //float SellingPrice = ((Price / 100) * Profit) + Price; row[2] = txtQuantity.Text; row[3] = Price * Convert.ToInt32(txtQuantity.Text); //dgvProducts.SelectedRows[0].Cells[5].Value = (Convert.ToInt32(dgvProducts.SelectedRows[0].Cells[5].Value)) - (Convert.ToInt32(txtQuantity.Text)); return; } } } int pid = (int)(dgvProducts.SelectedRows[0].Cells[0].Value), rotquantity = Convert.ToInt32(txtQuantity.Text); int availableQuantity = (int)(dgvProducts.SelectedRows[0].Cells[5].Value); if (rotquantity > availableQuantity) { if (availableQuantity == 0) { MessageBox.Show("Sorry! The selected product is Out of Stock..\nPlease Try again later..", "Sorry!"); } else if (MessageBox.Show(GeneralMethods.GetProductNameByID(pid) + " is not available in the desired quantity. Do you wish to add the complete quantity avaialable i.e " + availableQuantity + " to the cart?", "Oops", MessageBoxButtons.YesNo) == DialogResult.Yes) { rotquantity = availableQuantity; float a = Convert.ToSingle(dgvProducts.SelectedRows[0].Cells[4].Value); //float b = Convert.ToInt32(txtQuantity.Text); float rottotalamount = a * rotquantity; string pname = ((GeneralMethods.GetProductByID(pid)).Rows[0][2]).ToString(); CartTable.Rows.Add(pid, pname, rotquantity, rottotalamount); } } else { float a = Convert.ToSingle(dgvProducts.SelectedRows[0].Cells[4].Value); //float b = Convert.ToInt32(txtQuantity.Text); float rottotalamount = a * rotquantity; string pname = ((GeneralMethods.GetProductByID(pid)).Rows[0][2]).ToString(); CartTable.Rows.Add(pid, pname, rotquantity, rottotalamount); //dgvProducts.SelectedRows[0].Cells[5].Value = (Convert.ToInt32(dgvProducts.SelectedRows[0].Cells[5].Value)) - (Convert.ToInt32(txtQuantity.Text)); } } }