C
C#

help

❔ Help in sql client query Update

Yyumo2/10/2023
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();
}
}
}
SSG972/10/2023
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
Yyumo2/10/2023
i would love to get that idProjetos i mean acaoID yes
SSG972/10/2023
if you run that code, what is the value of id you're parsing it, but not using it
Yyumo2/10/2023
it changes all data there to Validado
SSG972/10/2023
of course, as your statement is to update every row in tbl_acoes put a breakpoint (f9) on con.Open(); and run it again
Yyumo2/10/2023
Yyumo2/10/2023
says id 0
SSG972/10/2023
and how are you binding you data to the datagrid
Yyumo2/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();
}
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
SSG972/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 but in essence, it should be easy to get the acaoID of the row that you clicked
Yyumo2/10/2023
but is it in the event? datacell click?
SSG972/10/2023
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
Yyumo2/10/2023
But i call it everywhere
SSG972/10/2023
call what?
Yyumo2/10/2023
dataGridViewAcoesPendentes
SSG972/10/2023
that's fine
Yyumo2/10/2023
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);
SSG972/10/2023
yes, you want to update a specific row not all the rows which is the row you clicked
AAccord2/11/2023
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Yyumo2/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();
}
}
}
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();
}
}
}

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ Trying to return the result of Fan Out Fan In Azure FunctionSo I've been trying to build my Azure function with a Fan-Out Fan-In method. But when the function c❔ What is this Blazor Wasm with Server template?What is this Blazor Wasm with Server template?❔ how do i make "dotnet new console" workpls help❔ How can i make this work on windows formsim trying to make this work on windows forms but idk what im doing wrong❔ using data received from a Post to determine what property to setBelow will be attached text of two classes, one called PostReceiveStruct and the other UserAccount. ❔ Saving Images to the database on azureFor my web app i need to save images. my app is hosted on azure back end is asp.net core. when an im❔ broken c# (its a large code explained)```css using System.Runtime.InteropServices; using System.Text; public class Program { [DllImpo✅ Unit tests not appearingHello! For once I'm doing a little project in the console rather than in Unity, meaning for unit tes❔ replace report viewer net core WinFormsHello people, I need to make a ticket and print it, how can I do it? Report viewer in net core versi❔ MVC - data is being passed to view, but not displayedMy app worked fine earlier and somehow I broke it, and I do not know how. My model is full of data,❔ Understanding DelegatesHi there. I'm a bit new to delegates. There are a couple things that I would like to straighten out.✅ Youtzbe Eventhi, does anyone know an addon for youtube? that "tracks" a channel and returns an event when the cha❔ P/Invoked DeviceIoControl() works in C++ but does nothing in C#.Code is attached. I have scoured this for hours upon hours and cross checked the pointers with the ❔ WPFHey guys. I want to run Class.cs in xaml.cs. Is that possible? (I tried to run a method that in th❔ EF Core questionData annotations vs fluent api? What are pros and cons? a pro for fluent api I see is that I can dec❔ asp.net core How to send array to javascript?how do we send a clean array to javascript?❔ how to add parallel processing?I have following function: ```cs [DeliveryHandler( Name = ReportsAPI.Commands.ReportsGenerate )] pub❔ Could not populate a data tableI am practicing EF Core with the help of a reference book. I have downloaded a .sql file from the bo❔ Unity Saving userinput data for next sceneHi, i'm trying to have people enter multiple names into a different inputfield and trying to use it ❔ how to give the Label give better SetLeft() positionThe labels get random position, but the 6 Label sometimes is on the edge, what it should not be. Th