private void CreateQuerry() { HelperClass hc = new HelperClass(connectionString); List <OneProduct> list = hc.GetProductList(); OneProduct product = list[dataGridViewProducts.CurrentRow.Index]; int id = product.Id; bool isInDb = false; int amountAdd = Convert.ToInt32(maskedTextBoxAmount.Text); int amountInDb = 0; int tempId = 0; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand("SELECT Id, Amount FROM dbo.Products", connection); var reader = command.ExecuteReader(); while (reader.Read()) { tempId = reader.GetInt32(0); if (tempId == id) { isInDb = true; amountInDb = reader.GetInt32(1); break; } } string str; if (isInDb) { int amountTotal = amountInDb + amountAdd; int sum = amountTotal * product.Price; str = "UPDATE dbo.Products SET Amount = " + amountTotal.ToString() + ", Sum = " + sum.ToString() + " WHERE Id = " + tempId.ToString(); } else { str = "INSERT INTO dbo.Products VALUES ('" + product.Id + "', '" + product.Name + "', '" + product.Category + "', '" + product.Measuring + "', '" + product.Price + "', '" + amountAdd + "', '" + amountAdd * product.Price + "')"; } commands.Add(str); dataGridViewItems.Rows.Add(product.Id, product.Name, product.Category, product.Measuring, product.Price, amountAdd, Convert.ToInt32(maskedTextBoxAmount.Text) * product.Price); stringToSend += product.Name + "(x" + amountAdd + "), "; totalSum += product.Price * amountAdd; } }
private void CreateQuerry() { HelperClass hc = new HelperClass(connectionString); List <OneProduct> list = hc.GetProductList(); OneProduct product = new OneProduct(); int currId = Convert.ToInt32(dataGridViewProducts.CurrentRow.Cells[1].Value); foreach (var i in list) { if (i.Id == currId) { product = i; } } int newAmount = Convert.ToInt32(dataGridViewProducts.CurrentRow.Cells[3].Value) - Convert.ToInt32(maskedTextBoxAmount.Text); int sum = Convert.ToInt32(dataGridViewProducts.CurrentRow.Cells[2].Value) * newAmount; string str = ""; int amountAdd = Convert.ToInt32(maskedTextBoxAmount.Text); if (newAmount == 0) { str = "DELETE FROM dbo.Products WHERE Id = " + currId.ToString(); } else { str = "UPDATE dbo.Products SET Amount = " + newAmount + ", Sum = " + sum + "WHERE Id = " + currId.ToString(); } commands.Add(str); dataGridViewItems.Rows.Add(product.Id, product.Name, product.Category, product.Measuring, product.Price, amountAdd, Convert.ToInt32(maskedTextBoxAmount.Text) * product.Price); str = maskedTextBoxAmount.Text.Trim(' '); stringToSend += Convert.ToString(dataGridViewProducts.CurrentRow.Cells[0].Value) + "(x" + str + "), "; totalSum += Convert.ToInt32(maskedTextBoxAmount.Text) * Convert.ToInt32(dataGridViewProducts.CurrentRow.Cells[2].Value); }