update of lines followed according to items selected in listview

Asked

Viewed 124 times

0

we have a table 1, which has building and rooms, building A, sala01, room 02.... , we need to put people in these rooms. These people come from another table(2), which fills the listview. This Lvi has checkboxes and is multiselect. How will you do it? Select people by checkboxes and click a button to place each person in a room in sequence. If you select Person 1, Person 4, Person 7, you place Person 1 in Room 1, Person 4 in Room 2, Person 7 in Room 3, right on the bench for display on the grid.... I’m doing it like this:

 private void btnAlocar_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView2.CheckedItems)
            {
                if (item.Checked == true)
                {
                    int j = listView2.CheckedItems.Count;
                    try
                    {
                        SqlConnection con = new SqlConnection(conexaoString);

                        con.Open();

                        for (int i = 0; i < (j); i++)
                        {
                            var cmd = new SqlCommand(@"UPDATE salas SET
                            nomePessoa=@nomePessoa, cargo=@cargo, idPessoa=@idPessoa WHERE idSala=@idSala", con);

                            cmd.Parameters.AddWithValue("@idSala", Convert.ToInt32(dgvSalas.CurrentRow.Cells["idSala"].Value.ToString()));

                            cmd.Parameters.AddWithValue("@nomePessoa", item.SubItems[0].Text);
                            cmd.Parameters.AddWithValue("@idPessoa", Convert.ToInt32(item.SubItems[3].Text));

                            cmd.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Erro ao alocar Pessoa: {ex.Message}");
                    }
                    finally
                    {
                        SqlConnection con = new SqlConnection(conexaoString);
                        this.salasTableAdapter.Fill(this.bdDataSet.salas);

                        con.Close();
                        con.Dispose();
                    }
                }
            }

        }

it does the insertion, but from one person, I’m not getting it to pass to the next line and insert the other selected, where I’m missing, I can’t find the error....

1 answer

0

On the line

cmd.Parameters.AddWithValue("@idSala", Convert.ToInt32(dgvSalas.CurrentRow.Cells["idSala"].Value.ToString()));

Where do you update dgvSalas.CurrentRow? 'Cause it looks to me like you’re overwriting the user in that room with every UPDATE...

  • I’m using this as a starting line. I should start inserting this line into the person column. I did update pq the table is filled in several steps, and at that point already has data, so it has to be update...

  • o que eu não consigo é fazer ele andar as linhas, tentei assim:&#xA;&#xA;cmd.Parameters.AddWithValue("@idSala", Convert.ToInt32(dgvSalas.CurrentRow.Cells["idSala"].Value.ToString())+i);

  • pq wanted him to go to the next idsala, with the +i, then put another name but where he wanted...

  • I tried to write so, if 3 names were selected, update in the name column, in the idsala line where the cursor is, inserting each name checked, row by row....

  • I got it. I didn’t need the is, only the foreach already solved....

Browser other questions tagged

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