How do you use the UPDATE command more than once?

Asked

Viewed 105 times

1

I cannot use the UPDATE command more than once.

For example, when I do the update on id 43, sure. But when I try to finish 44, it does not finish, but 43 that gets the update..

Code:

    Int att,index;

    Private void DataGrid_CellClick(object sender, DataGridViewCellEventsArgs e) {

       if(e.RowIndex >= 0)
       {

         Index = e.RowIndex;                                               
         att = Convert.ToInt32(DataGrid.Rows[Index].Cells[0].Value);
       }
    }

 //quando clico numa célula, Att recebe o número que está //dentro dela, no caso correspondente ao ID na database tb. E index recebe o índice da linha.

    private void Btnfnz_Click(objetc sender, EventArgs e)
    {
         If(index >=0)
         {
              var linha = DataGrid1.Rows[index];
              cn.Connection = Conexão.conectar();
              cn.CommandText = "UPDATE Atendimento SET HorarioOut = ' " + Txthr.Text + " ' WHERE ID=@att";
              cn.Parameters.AddWithValue("@att",att);
              cn.ExecuteNonQuery();
              cn.Connection = Conexão.desconectar();
         }
         else 
         {
               MessageBox.Show("Lista vazia!");
         }
         Refreshatt(); //Carrega o datagrid com dados do banco dados, onde horarioOut estiver em branco.
    }

Everything works fine, but when I try to do a new update, it does, but in msm ID, it is fixed in the first ID I did the update.

Imagery

https://drive.google.com/file/d/1xyqp6rA4Rk3AvneFaUlqk6AVvjQjbtSi/view?usp=drivesdk https://drive.google.com/file/d/190WFQ0cxGWYJw2aO66xBNvOsNftY7CAk/view?usp=drivesdk https://drive.google.com/file/d/1-XS3NeZm7cU0_4Pi7r2fY4dCNNAjCKtf/view?usp=drivesdk

  • you have asked this question before the problem is in the value assignment of the variable att...

  • And remove your previous question.

  • You tested my answer?

1 answer

1

Forget your DataGrid_CellClick is a different event than yours Btnfnz_Click. If you debug your code you will see that they run at different times and maybe the DataGrid_CellClick or be invoked when you click the button...

Capture the Row content via its own button in the click event.

private void Btnfnz_Click(objetc sender, EventArgs e)
{
     GridViewRow gridViewRow = (GridViewRow)(sender as Control).Parent.Parent;
     int index = gridViewRow.RowIndex;
     int att = Convert.ToInt32(DataGrid.Rows[index].Cells[0].Value);

     if(index >=0)
     {
          var linha = DataGrid1.Rows[index];
          cn.Connection = Conexão.conectar();
          cn.CommandText = "UPDATE Atendimento SET HorarioOut = ' " + Txthr.Text + " ' WHERE ID=@att";
          cn.Parameters.AddWithValue("@att",att);
          cn.ExecuteNonQuery();
          cn.Connection = Conexão.desconectar();
     }
     else 
     {
           MessageBox.Show("Lista vazia!");
     }
     Refreshatt(); //Carrega o datagrid com dados do banco dados, onde horarioOut estiver em branco.
}
  • It didn’t work, Gridviewrow doesn’t appear as a valid code here... What I need is for the fnz button to pick the line that was selected and update it. As I said, the first time I try everything works, but when I click on another line, the update happens in the first line, I already tested the cellclick and it returns the ID corresponding to the right line, I don’t think the problem is in the variable att.

  • It is a web application or windows Forms?

  • If it is windows Forms the correct type is DataGridViewRow

  • I’m gonna test it, vlww

  • Error: "Cannot convert "System.Windows.Forms.Control" to "System.Windows.Forms.Datagrodviewrow" "

  • Include your grid’s Markup in the question.

  • In place of which name?

  • I tested it, but it went on the same way... The first time he updates the line I selected, but when I select another line I update. It performs the update always on the same line... and not on the one I selected.

  • is missing information, as I said in your other question include the code of the Markup

  • Sorry, what is Markup?

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.