0
I am wanting to save more than one line of my datagridview in the database, but it saves only the first line and after the error saying that the parameters have already been set, wanted to know where I am missing
MySqlConnection conn;
conn = new MySqlConnection(conexao);
conn.Open();
cmd = new MySqlCommand("INSERT INTO TesteCauaVendas VALUES(@Nome, @Cor, @Especie, @Quantidade, @Vendedor, @Data, @Cliente)", conn);
try
{
for (int i = 0; i < dtCompra.Rows.Count - 1; i++)
{
cmd.Parameters.AddWithValue("@Nome", dtCompra.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@Cor", dtCompra.Rows[i].Cells[1].Value);
cmd.Parameters.AddWithValue("@Especie", dtCompra.Rows[i].Cells[2].Value);
cmd.Parameters.AddWithValue("@Quantidade", dtCompra.Rows[i].Cells[3].Value);
cmd.Parameters.AddWithValue("@Vendedor", dtCompra.Rows[i].Cells[4].Value);
cmd.Parameters.AddWithValue("@Data", dtCompra.Rows[i].Cells[5].Value);
cmd.Parameters.AddWithValue("@Cliente", dtCompra.Rows[i].Cells[6].Value);
MessageBox.Show("Venda Realizada com Sucesso");
}
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
Thank you very much, it worked! It was missing a bit of organization and logic kkk.
– user247153