private void button1_Click(object sender, EventArgs e) { DBConnect edit = new DBConnect(); int barcode; if (!String.IsNullOrEmpty(IDfield.Text)) { barcode = int.Parse(IDfield.Text); int qty = (int)qtyField.Value; int x; int i; for (i = 0; i < copy.Count && barcode != copy[i].barCode; i++) { } //finds item in inventory copy if (i < copy.Count) { for (x = listView1.Items.Count - 1; x >= 0 && listView1.Items[x].SubItems[1].Text != barcode.ToString(); x--) { } //finds item in list view if (x >= 0) { int qtyDec = int.Parse(listView1.Items[x].SubItems[2].Text); //gets qty purchased double priceDec = Double.Parse(listView1.Items[x].SubItems[3].Text); //gets total price of purchased item String query = "INSERT INTO purchaselog(itemID, purchaseDate, qty, transactionID,remark) VALUES(" + copy[i].ID + ", curdate(), " + qty + ", " + data.transID + ",'RETURNED')"; String query2 = "UPDATE product SET product_qty = product_qty + '" + qty + "'WHERE bar_code = '" + barcode + "'"; if (qtyDec - qty > 0) { qtyDec -= qty; priceDec -= (copy[i].price * qty); listView1.Items[x].SubItems[2].Text = qtyDec.ToString(); listView1.Items[x].SubItems[3].Text = priceDec.ToString(); data.sum -= (double)(copy[i].price * qty); totalField.Text = data.sum.ToString(); edit.insertLog(query); edit.insertLog(query2); IDfield.Clear(); qtyField.Value = 1; itemNamefield.Clear(); } else if (qtyDec - qty == 0) { listView1.Items[x].Remove(); edit.insertLog(query); data.sum -= (copy[i].price * qty); totalField.Text = data.sum.ToString(); edit.insertLog(query); edit.insertLog(query2); IDfield.Clear(); qtyField.Value = 1; itemNamefield.Clear(); } else { MessageBox.Show("Invalid Amount"); } } else { MessageBox.Show("Item not purchased"); } } else { MessageBox.Show("Item not found"); } } else { MessageBox.Show("Please enter barcode/name"); } }