internal static void DB_updateCashRegister(SomerenModel.CashRegisterList list) { SqlConnection connection = openConnectionDB(); // we generate the date on the server // we only support ordering one of the same drink at a time string sqlQuery = @"INSERT INTO StudentBarService (drink_id, student_id, date, drink_sold) VALUES (@drink_id, @student_id, GETDATE(), 1)"; SqlCommand command = new SqlCommand(sqlQuery, connection); // insert the drinks one by one foreach (SomerenModel.CashRegister record in list.getList()) { // clear from previous query command.Parameters.Clear(); // add student and drink ids command.Parameters.AddWithValue("@drink_id", record.getDrinkId()); command.Parameters.AddWithValue("@student_id", record.getStudentId()); // execute the query command.ExecuteNonQuery(); } // make sure to close the connection connection.Close(); }
private static void Handle_CheckoutButtonClicked(object sender, EventArgs eventArgs) { // find the button Button checkoutButton = (Button)sender; // find the other controls Control[] otherControls = (Control[])checkoutButton.Tag; // find the two lists ListView studentsListView = (ListView)otherControls[0]; ListView barServiceListView = (ListView)otherControls[1]; // we only support one student per order if (studentsListView.CheckedItems.Count != 1) { MessageBox.Show("Please select exactly one student.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // get the selected student int studentId = int.Parse(studentsListView.CheckedItems[0].Text); // get the selected drinks List <int> drinkIds = new List <int>(); foreach (ListViewItem checkedDrink in barServiceListView.CheckedItems) { int drinkId = int.Parse(checkedDrink.Text); drinkIds.Add(drinkId); } SomerenModel.CashRegisterList cashLines = new SomerenModel.CashRegisterList(); // add each of the drinks to the list foreach (int drinkId in drinkIds) { SomerenModel.CashRegister cashLine = new SomerenModel.CashRegister(); cashLine.setDrinkId(drinkId); cashLine.setStudentId(studentId); cashLines.addList(cashLine); } // invoke database query to store the stuff SomerenDB.DB_updateCashRegister(cashLines); // reset the view foreach (ListViewItem student in studentsListView.Items) { student.Checked = false; } foreach (ListViewItem drink in barServiceListView.Items) { drink.Checked = false; } }