Reputation: 19
I have a program that allow customer to vote for the drinks. Order one drink equal to one vote. Order two drink equal to two votes and so on. Enter Order_ID to check the vote counts. Customer vote by selecting the radio buttons and click submit button. After submit, vote count should be -1. Message box will display vote count remained. (eg ordered 5 drinks, after one vote submit should be showing "vote count remained is 4" and stop program when vote count is 0).
My question is how to make the above happen? I have tried using loop but does not pause in between to let customer to resubmit again. Loop will countinously display MessageBox until vote count reached 0.
Second question, can I use groupbox? but how to write the code if apply groupbox.
Third question or I should not use loop at all?
private void btnSubmitVote_Click(object sender, EventArgs e)
{
for (int i = OrdersQty; OrdersQty > 0 ; OrdersQty-- )
{
if (radAmericano.Checked)
{
conn.Open();
MySqlCommand comm2a = new MySqlCommand(sql2a, conn);
MySqlDataReader reader2a = comm2a.ExecuteReader();
comm2c.ExecuteDataReader()
comm2a.Dispose();
conn.Close();
conn.Open();
MySqlCommand comm2b = new MySqlCommand(sql2b, conn);
comm2b.ExecuteNonQuery();
comm2b.Dispose();
conn.Close();
MessageBox.Show("You have " + OrdersQty + " vote left")
}
if (radCappuccino.Checked)
{
conn.Open();
MySqlCommand comm2c = new MySqlCommand(sql2c, conn);
MySqlDataReader reader2a = comm2a.ExecuteReader();
comm2c.ExecuteDataReader()
comm2c.Dispose();
conn.Close();
conn.Open();
MySqlCommand comm2d = new MySqlCommand(sql2d, conn);
comm2d.ExecuteNonQuery();
comm2d.Dispose();
conn.Close();
MessageBox.Show("You have " + OrdersQty + " vote left")
}
}
MessageBox.Show("You have " + OrdersQty + " vote left" + "Thank you")
this.Close();
}
Upvotes: -1
Views: 101
Reputation: 112682
You must remove the loop, because the user will only be able to activate radio buttons and do other inputs when the Button Click event handler is executed. As long as event handlers are running, the UI is frozen. Display a message when he is not finished. He will then make another selection and click the submit button again.
Aslo, you should use Parameterized Query for MySQL with C#. Like this, you will be able to reuse the same command for the different kinds of coffees. It would also simplify the code if you extracted the almost identical code to another method.
private bool InsertOrderItem(int orderId, int coffeeId)
{
const string insertSql = "INSERT INTO OrderItem (OrderId, CoffeId) VALUES (@oid, @cid)";
using var connection = new MySqlConnection(connectionString);
using var command = new MySqlCommand(insertSql, connection);
command.Parameters.AddWithValue("@oid", orderId);
command.Parameters.AddWithValue("@cid", coffeeId);
connection.Open();
return command.ExecuteNonQuery() > 0;
}
Then you can write something like this
private void btnSubmitVote_Click(object sender, EventArgs e)
{
if (radAmericano.Checked) {
if (InsertOrderItem(orderId, americanoCoffeeId)) {
OrdersQty--;
}
} else if (radCappuccino.Checked) {
if (InsertOrderItem(orderId, cappucinoCoffeeId)) {
OrdersQty--;
}
}
if (OrdersQty == 0) {
Close();
} else{
MessageBox.Show($"You have {OrdersQty} votes left\r\n\r\nThank you")
}
}
Upvotes: 0