0
Good afternoon, you guys! I have a problem with doing a data insertion presented to my datagridview.
I have a table called Events, and it contains all my records, I created another table with the same fields, so that the events would be downloaded when the date is expired.
the problem is this... when I insert the data presented from the first table playing them to the table downloaded events, the data doubles, ie it is taking both the first id and the others. Note:(I am selecting the data from the datagridview line through a menu and clicking.) follows the image.
follows my code
private void BaixarDadosAgenda()
{
try
{
if (DGW_Agenda.Rows.Count > 1)
{
cg.con = new SqlConnection(cn.DBconn);
string cb = "INSERT INTO Eventos_Baixados VALUES (@Nome, @Endereco, @Quadra, @Lote, @Telefone, @Celular, @Data, @Hora, @Email, @Observacao, @Locacao, @Evento, @Estado)";
cg.con.Open();
cg.cmd = new SqlCommand(cb);
cg.cmd.Connection = cg.con;
for (int i = 0; i <= DGW_Agenda.Rows.Count - 1; i++)
{
DGW_Agenda.CurrentRow.Cells[0].Value.ToString();
DGW_Agenda.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
DGW_Agenda.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
DGW_Agenda.MultiSelect = false;
cg.cmd.Parameters.Clear();
cg.cmd.Parameters.AddWithValue("@Nome", DGW_Agenda.Rows[i].Cells["Nome"].Value);
cg.cmd.Parameters.AddWithValue("@Endereco", DGW_Agenda.Rows[i].Cells["Endereco"].Value);
cg.cmd.Parameters.AddWithValue("@Quadra", DGW_Agenda.Rows[i].Cells["Quadra"].Value);
cg.cmd.Parameters.AddWithValue("@Lote", DGW_Agenda.Rows[i].Cells["Lote"].Value);
cg.cmd.Parameters.AddWithValue("@Telefone", DGW_Agenda.Rows[i].Cells["Telefone"].Value);
cg.cmd.Parameters.AddWithValue("@Celular", DGW_Agenda.Rows[i].Cells["Celular"].Value);
cg.cmd.Parameters.AddWithValue("@Data", DGW_Agenda.Rows[i].Cells["Data"].Value);
cg.cmd.Parameters.AddWithValue("@Hora", DGW_Agenda.Rows[i].Cells["Hora"].Value);
cg.cmd.Parameters.AddWithValue("@Email", DGW_Agenda.Rows[i].Cells["Email"].Value);
cg.cmd.Parameters.AddWithValue("@Observacao", DGW_Agenda.Rows[i].Cells["Observacao"].Value);
cg.cmd.Parameters.AddWithValue("@Locacao", DGW_Agenda.Rows[i].Cells["Locacao"].Value);
cg.cmd.Parameters.AddWithValue("@Evento", DGW_Agenda.Rows[i].Cells["Evento"].Value);
cg.cmd.Parameters.AddWithValue("@Estado", DGW_Agenda.Rows[i].Cells["Estado"].Value);
cg.cmd.ExecuteNonQuery();
DGW_Agenda.Rows.Clear();
}
cg.con.Close();
st1 = Lbl_Usuario.Text;
st2 = "Nova Locacão Baixada '" + TB_Nome.Text;
cf.LogFunc(st1, DateTime.Now, st2);
LogEvento.GravarLog("'Locação Baixada com Sucesso! : = '" + TB_Nome.Text + "'Usuário'" + Lbl_Usuario.Text.ToString());
Reset();
BTN_Cadastrar.Enabled = false;
MessageBox.Show("Locação Baixada Com Sucesso !", "Locação", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void BT_BaixaAgenda_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Deseja dar baixa nessa Locação?", "Aviso do Sistema", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
BaixarDadosAgenda();
}
}
From now on I thank you for your strength!
You can select more than one line ?
– Joy Peter
No! the idea is to select one line at a time. I even did a test on my for my routine leaving it like this. for (int i = 0; i <= Dgw_agenda.SelectedRows.Count - 1; i++) But to no avail!
– Lucas_Silva
With the for has no way to work
– Joy Peter