Using Arraylist to update the information in c# "checkedlistbox"

Asked

Viewed 141 times

1

Fala galera to with a difficulty in picking my strings to update my information. My scenario is this: i have a checkedlistbox where I have my strings in total are 9. In the input part of the data I can do normally, but I don’t know how to pick the selected string and update NOTE:MY STRINGS ARE FIXED BOTH FOR INSERTION BOTH FOR UPDATE. if I put my checkedlistbox.text, it only takes one string, even if I select more than one, I’ve tried checkedlistbox.setitems, Checkeditems and so on. and in vdd I need to pick up more than one selected string and send it to my bank.

this is my code where I have my fields to update, I last parameters because I believe it would be easier.

public bool Update(ArrayList p_arrUpdate)
        {
        vsql = "UPDATE alunos SET nome = @nome, idade = @idade, endereco = @endereco, quadra_lote = @quadra_lote, telefone = @telefone, email = @email, cidade = @cidade, uf = @uf, nome_pai = @nome_pai, nome_mae = @nome_mae, situacao = @situacao, atividade = @atividade  WHERE id_aluno = @id_aluno";

            SqlCommand objcmd = null;

            if (this.conectar())

                try // ele responsavel por tentar executar o array, se der alguma coisa errada e vai cair no finally
                {

                    objcmd = new SqlCommand(vsql, objCon);
                    objcmd.Parameters.Add(new SqlParameter("@id_aluno", p_arrUpdate[0]));
                    objcmd.Parameters.Add(new SqlParameter("@nome", p_arrUpdate[1]));
                    objcmd.Parameters.Add(new SqlParameter("@idade", p_arrUpdate[2]));
                    objcmd.Parameters.Add(new SqlParameter("@endereco", p_arrUpdate[3]));
                    objcmd.Parameters.Add(new SqlParameter("@quadra_lote", p_arrUpdate[4]));
                    objcmd.Parameters.Add(new SqlParameter("@telefone", p_arrUpdate[5]));
                    objcmd.Parameters.Add(new SqlParameter("@email", p_arrUpdate[6]));
                    objcmd.Parameters.Add(new SqlParameter("@cidade", p_arrUpdate[7]));
                    objcmd.Parameters.Add(new SqlParameter("@uf", p_arrUpdate[8]));
                    objcmd.Parameters.Add(new SqlParameter("@nome_pai", p_arrUpdate[9]));
                    objcmd.Parameters.Add(new SqlParameter("@nome_mae", p_arrUpdate[10]));
                    objcmd.Parameters.Add(new SqlParameter("@situacao", p_arrUpdate[11]));
                    objcmd.Parameters.Add(new SqlParameter("@atividade", p_arrUpdate[12]));


                objcmd.ExecuteNonQuery();
                    return true;

                }
                catch (SqlException sqlerr)
                {

                    throw sqlerr;
                }
                finally
                {

                    this.desconectar();
                }
            else
            {
                return false;
            }
        }

this is my code where in the button save the changes he makes the Arraylist.

private void button1_Click(object sender, EventArgs e)
    {
        SysDBADM obj = new SysDBADM();
        ArrayList arr = new ArrayList();

        try
        {
            arr.Add(TB_cod.Text);
            arr.Add(TB_nome.Text);
            arr.Add(TB_idade.Text);
            arr.Add(TB_endereco.Text);
            arr.Add(TB_quadra_lote.Text);
            arr.Add(MD_telefoneCel.Text);
            arr.Add(TB_email.Text);
            arr.Add(TB_cidade.Text);
            arr.Add(TB_uf.Text);
            arr.Add(TB_nomepai.Text);
            arr.Add(TB_nomemae.Text);
            arr.Add(CB_ativo.Text);
            arr.Add(CLB_atividade.Text + ","); // esse é meu checkedlistbox

            if (obj.Update(arr))
                {

                    TB_cod.Clear();
                    TB_nome.Clear();
                    TB_idade.Clear();
                    TB_endereco.Clear();
                    TB_quadra_lote.Clear();
                    MD_telefoneFixo.Clear();
                    MD_telefoneCel.Clear();
                    TB_email.Clear();
                    TB_cidade.Clear();
                    TB_uf.Clear();
                    TB_nomepai.Clear();
                    TB_nomemae.Clear();
                    CB_ativo.Text = "";


                    while (CLB_atividade.CheckedItems.Count > 0)
                {
                    CLB_atividade.SetItemChecked(CLB_atividade.CheckedIndices[0],false);
                }
                   MessageBox.Show("Cadastro Atualizado.", "Mensagem do Sistema", MessageBoxButtons.OK, MessageBoxIcon.Information);


                }

                else
                {
                    MessageBox.Show("Erro ao Atualizar!", "Mesagem do Sistema", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
           }

        catch (Exception erro)
        {
            MessageBox.Show(erro + "Erro Ocorrido");
        }

    }

this is the image where I change the registered record. inserir a descrição da imagem aqui

1 answer

0

Replace your while for this foreach

foreach (var item in CLB_atividade.CheckedItems)
{
  //item tem o valor checado                
}

Use of the event Itemcheck Evento ItemCheck

  • good morning @Lexandre Cavaloti, so my broadcaster is being in updating the information as selected, ie the information is already being recorded when I enter the data, what I need and update the information for example: in my example my image has selected option 2 that was made at the time of my Sert, what I need to do is to update, ie if I click another activity or remove and click the button save change , this information that update you.

  • I understand, IE, you need to know the items that were cleared as well. This can be done using the event Itemcheck checkbox. I added in the reply an image that contains the values passed for this event, which are: Checked or "unknown" item, item index, old value and new value. You can use this event to control which items should be removed and which items should be placed on the list in the BD.

  • I understood your explanation, I searched the microsoft documentation. I just don’t know how to implement this within my code is the first time I use the checkedlistbox, I don’t have much experience with c#, so I’m asking for help. more let lah solve this problem. within my arr.add(checkedlistbox.itemCheck); I must do this ?

  • As they are in the database, the activity data, which should insert or update/delete?

  • the way it is being inserted into the database is my table called Students, in the active column. I use my checkedlistbox to take the Collections strings, and concatenate according to the item selected at the time of registration, and in my update form like this in the image I use the same table students to do the update more in my code I can update the information of my textbox, and combobox, more do not know how to do with the checkedlistbox

  • that is, all activities concatenated?

  • yes and stored in my column activities

  • So always update the activities that are selected, regardless of whether there is a change or not. This solves the right problem?

  • yes the activities are fixed in my checkedlistbox, what I need is to update the information that was entered at the time of registration, that is when the user selects 3 activities and at the time of registration at the time I edit it will show me the 3 activities that is what he is doing, more I need to edit or if I need to take a registration activity that was carried out will have to upgrade to 2 activities just understood

  • only update everything. For being a concatenated line with all selected activities. Example: Activities: Pilates; Zumba . UPDATE: Activities = Zumba; Yoga

Show 5 more comments

Browser other questions tagged

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