Good afternoon,
I am working on a restaurant system in C # using Windows Forms, which is the part of taking the order:
What I want to achieve in inserting this order in table form in SQL SERVER In the following table:
and how I want it to look when inserting it into SQL is as follows:
The code I am using is the following:
private void button9_Click(object sender, EventArgs e)
{
SqlCommand agregar = new SqlCommand("Insert into Ordenes2 values (@Pedido, @Mesa, @NombreP, @Costo,@Fecha, @Hora)", cadena);
try
{
cadena.Open();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
agregar.Parameters.AddWithValue("@Pedido", variables.orden);
agregar.Parameters.AddWithValue("@Mesa", label1.Text);
agregar.Parameters.AddWithValue("@NombreP",Convert.ToString(row.Cells[0].Value));
agregar.Parameters.AddWithValue("@Costo", Convert.ToSingle(row.Cells[1].Value));
label2.Text = DateTime.Now.ToString("yyyy-MM-dd");
agregar.Parameters.AddWithValue("@Fecha",label2.Text);
agregar.Parameters.AddWithValue("@Hora", label3.Text);
agregar.ExecuteNonQuery();
}
MessageBox.Show("Orden tomada exitosamente");
}
catch(Exception ex)
{
MessageBox.Show("Error al agregar " + ex);
}
finally
{
cadena.Close();
}
}
But I have a problem, where should I accommodate the ExecuteNonQuery? since it marks me: The name of the variables has already been declared. It's because of the cycle but I do not know where to put it so that it's not that error.
Greetings, thank you very much.