C
C#2y ago
yumo

❔ Help in sql client query Update

In the image as you see thats my datagridview and when i click on the button it will change all Acoes to "Validado", but i wanted just the row that the button is in , what and how can i do this? This is the statment i have
private void dataGridViewAcoesPendentes_CellClick_2(object sender, DataGridViewCellEventArgs e)
{
if (dataGridViewAcoesPendentes.Columns[e.ColumnIndex].HeaderText == "BtnEditar")
{
int id;
id = Convert.ToInt32(dataGridViewAcoesPendentes.Rows[e.RowIndex].Cells["acaoID"].Value);

con.Open();
try
{
SqlCommand cmd = new SqlCommand("UPDATE tbl_acoes SET estado = 'Validado'", con);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("Ação Validado");
SqlCommand cmd2 = new SqlCommand("SELECT * FROM tbl_acoes WHERE estado = 'pendente'", con);
DataTable dt = new DataTable();
dt.Load(cmd2.ExecuteReader());
dataGridViewAcoesPendentes.DataSource = dt;
con.Close();
}
else
{
MessageBox.Show("Ensaio nao validado!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
}
private void dataGridViewAcoesPendentes_CellClick_2(object sender, DataGridViewCellEventArgs e)
{
if (dataGridViewAcoesPendentes.Columns[e.ColumnIndex].HeaderText == "BtnEditar")
{
int id;
id = Convert.ToInt32(dataGridViewAcoesPendentes.Rows[e.RowIndex].Cells["acaoID"].Value);

con.Open();
try
{
SqlCommand cmd = new SqlCommand("UPDATE tbl_acoes SET estado = 'Validado'", con);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("Ação Validado");
SqlCommand cmd2 = new SqlCommand("SELECT * FROM tbl_acoes WHERE estado = 'pendente'", con);
DataTable dt = new DataTable();
dt.Load(cmd2.ExecuteReader());
dataGridViewAcoesPendentes.DataSource = dt;
con.Close();
}
else
{
MessageBox.Show("Ensaio nao validado!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
}
21 Replies
SG97
SG972y ago
is that id meant to represent the row's identifying acaoID if so, and it is the selected rows acaoID , you need to use it in the UPDATE statement
yumo
yumoOP2y ago
i would love to get that idProjetos i mean acaoID yes
SG97
SG972y ago
if you run that code, what is the value of id you're parsing it, but not using it
yumo
yumoOP2y ago
it changes all data there to Validado
SG97
SG972y ago
of course, as your statement is to update every row in tbl_acoes put a breakpoint (f9) on con.Open(); and run it again
yumo
yumoOP2y ago
yumo
yumoOP2y ago
says id 0
SG97
SG972y ago
and how are you binding you data to the datagrid
yumo
yumoOP2y ago
private void LoadAcoesPendentes()
{
SqlConnection con = new SqlConnection("Data Source=mydatasource");
SqlCommand cmd2 = new SqlCommand("SELECT * FROM tbl_acoes WHERE estado='Pendente'", con);
/*SqlCommand cmd2 = new SqlCommand("SELECT * FROM tbl_ensaios WHERE idProjetos =" + txtIdFerramenta.Text + "", con);*/
con.Open();
DataTable dt = new DataTable();
dt.Load(cmd2.ExecuteReader());
dataGridViewAcoesPendentes.DataSource = dt;
con.Close();
}
private void LoadAcoesPendentes()
{
SqlConnection con = new SqlConnection("Data Source=mydatasource");
SqlCommand cmd2 = new SqlCommand("SELECT * FROM tbl_acoes WHERE estado='Pendente'", con);
/*SqlCommand cmd2 = new SqlCommand("SELECT * FROM tbl_ensaios WHERE idProjetos =" + txtIdFerramenta.Text + "", con);*/
con.Open();
DataTable dt = new DataTable();
dt.Load(cmd2.ExecuteReader());
dataGridViewAcoesPendentes.DataSource = dt;
con.Close();
}
like this
SG97
SG972y ago
It's been a hot minute since I've used datatables, but if you breakpoint after the dt.Load.... what kind of values can you see but in essence, it should be easy to get the acaoID of the row that you clicked
yumo
yumoOP2y ago
but is it in the event? datacell click?
SG97
SG972y ago
yes, but the data in dataGridViewAcoesPendentes is what's being populated there so when you reference the datatable in your dataGridViewAcoesPendentes_CellClick_2 handler, you should be able to get the data inside the selected row that's why I wanted to see what's inside the dt in your LoadAcoesPendentes method
yumo
yumoOP2y ago
But i call it everywhere
SG97
SG972y ago
call what?
yumo
yumoOP2y ago
dataGridViewAcoesPendentes
SG97
SG972y ago
that's fine
yumo
yumoOP2y ago
Isn't this sql command that i need to add something?
SqlCommand cmd = new SqlCommand("UPDATE tbl_acoes SET estado = 'Validado'", con);
SqlCommand cmd = new SqlCommand("UPDATE tbl_acoes SET estado = 'Validado'", con);
SG97
SG972y ago
yes, you want to update a specific row not all the rows which is the row you clicked
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
yumo
yumoOP2y ago
Well, i have this code right now but the update sql is still the same and im gonna place here how my tables are so it can help you guys more to understand what i need from this. The SQL statement
private void dataGridViewAcoesPendentes_CellClick_2(object sender, DataGridViewCellEventArgs e)
{
if (dataGridViewAcoesPendentes.Columns[e.ColumnIndex].HeaderText == "BtnEditar")
{
int id;
id = Convert.ToInt32(dataGridViewAcoesPendentes.Rows[e.RowIndex].Cells["acaoID"].Value);

con.Open();
try
{
SqlCommand cmd = new SqlCommand("UPDATE tbl_acoes SET estado = 'Validado'", con);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("Ação Validado");
SqlCommand cmd2 = new SqlCommand("SELECT * FROM tbl_acoes WHERE estado = 'pendente'", con);
DataTable dt = new DataTable();
dt.Load(cmd2.ExecuteReader());
dataGridViewAcoesPendentes.DataSource = dt;
con.Close();
}
else
{
MessageBox.Show("Ação nao validada!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
}
private void dataGridViewAcoesPendentes_CellClick_2(object sender, DataGridViewCellEventArgs e)
{
if (dataGridViewAcoesPendentes.Columns[e.ColumnIndex].HeaderText == "BtnEditar")
{
int id;
id = Convert.ToInt32(dataGridViewAcoesPendentes.Rows[e.RowIndex].Cells["acaoID"].Value);

con.Open();
try
{
SqlCommand cmd = new SqlCommand("UPDATE tbl_acoes SET estado = 'Validado'", con);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("Ação Validado");
SqlCommand cmd2 = new SqlCommand("SELECT * FROM tbl_acoes WHERE estado = 'pendente'", con);
DataTable dt = new DataTable();
dt.Load(cmd2.ExecuteReader());
dataGridViewAcoesPendentes.DataSource = dt;
con.Close();
}
else
{
MessageBox.Show("Ação nao validada!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
}
Want results from more Discord servers?
Add your server