How do you use the UPDATE command more than once? [duplicate]

1

I can not use the UPDATE command more than once.

For example, when I perform the update at id 43, that's fine. But when I try to finish 44, it does not finish, but the 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 even does, however in the msm ID, it is fixed in the first ID I made the update.

Images

  

link    link    link

    
asked by anonymous 23.05.2018 / 16:03

1 answer

1

Forget your DataGrid_CellClick is an event other than your Btnfnz_Click . If you debug your code you will see that they run at different times and maybe DataGrid_CellClick is not invoked when you click the ...

Capture the row index through 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.
}
    
23.05.2018 / 18:07