C#
C#

help

Root Question Message

slash
slash2/10/2023
❔ 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();
                }
            }
        }
sn0wgirl_97
sn0wgirl_972/10/2023
is that id meant to represent the row's identifying acaoID
sn0wgirl_97
sn0wgirl_972/10/2023
if so, and it is the selected rows acaoID , you need to use it in the UPDATE statement
slash
slash2/10/2023
i would love to get that idProjetos
slash
slash2/10/2023
i mean
slash
slash2/10/2023
acaoID yes
sn0wgirl_97
sn0wgirl_972/10/2023
if you run that code, what is the value of id
sn0wgirl_97
sn0wgirl_972/10/2023
you're parsing it, but not using it
slash
slash2/10/2023
slash
slash2/10/2023
it changes all data there to Validado
sn0wgirl_97
sn0wgirl_972/10/2023
of course, as your statement is to update every row in tbl_acoes
sn0wgirl_97
sn0wgirl_972/10/2023
put a breakpoint (f9) on con.Open();
sn0wgirl_97
sn0wgirl_972/10/2023
and run it again
slash
slash2/10/2023
slash
slash2/10/2023
says id 0
sn0wgirl_97
sn0wgirl_972/10/2023
and how are you binding you data to the datagrid
slash
slash2/10/2023
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();
        }
slash
slash2/10/2023
like this
sn0wgirl_97
sn0wgirl_972/10/2023
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
sn0wgirl_97
sn0wgirl_972/10/2023
but in essence, it should be easy to get the acaoID of the row that you clicked
slash
slash2/10/2023
but is it in the event?
slash
slash2/10/2023
datacell click?
sn0wgirl_97
sn0wgirl_972/10/2023
yes, but the data in dataGridViewAcoesPendentes is what's being populated there
sn0wgirl_97
sn0wgirl_972/10/2023
so when you reference the datatable in your dataGridViewAcoesPendentes_CellClick_2 handler, you should be able to get the data inside the selected row
sn0wgirl_97
sn0wgirl_972/10/2023
that's why I wanted to see what's inside the dt in your LoadAcoesPendentes method
slash
slash2/10/2023
But i call it everywhere
sn0wgirl_97
sn0wgirl_972/10/2023
call what?
slash
slash2/10/2023
dataGridViewAcoesPendentes
sn0wgirl_97
sn0wgirl_972/10/2023
that's fine
slash
slash2/10/2023
Isn't this sql command that i need to add something?


SqlCommand cmd = new SqlCommand("UPDATE tbl_acoes SET estado = 'Validado'", con);
sn0wgirl_97
sn0wgirl_972/10/2023
yes, you want to update a specific row
sn0wgirl_97
sn0wgirl_972/10/2023
not all the rows
sn0wgirl_97
sn0wgirl_972/10/2023
which is the row you clicked
slash
slash2/13/2023
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();
                }
            }
        }
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy