Fill a Checked List Box with SQL Server values

Asked

Viewed 114 times

0

I am creating a form that I would like it to work as follows: First the user would select the type of exercise that would be played in the Checked list box using a Combo Box, then the bank would return all the values found that matched that type of exercise (For example the user would choose Back in Combobox and appear all exercises for back in the Checked List Box).

I know how to display the values in a Data Grid, but by aesthetic and by the final functioning of the form I prefer not to use Data Grid.

            Conexao.Conectar();
            string sql = @"select Exer_NM from Exercicios  
                          Where Exer_Tipo = @Exer_Tipo";

            SqlCommand cmd = new SqlCommand(sql, Conexao.conn);
            cmd.Parameters.AddWithValue("Exer_Tipo", cboTipo.Text);

            // Armazena o resultado do comando SELECT
            SqlDataReader dr = cmd.ExecuteReader();

            //Laço de repetição para retornar todos os valores
           // dr.Read() - Lê uma nova linha
            while (dr.Read())
            {
              //  Mostra no listBox os dados retornados
                clb.Items.Add(dr[1].ToString());


            }
            Conexao.Desconectar();

My biggest doubts are:

1 - In the code I tried to mount, if I am using the correct event (Combobox Selected Index Changed or should use Text Changed for example.

2 - If it is correct to assign the parameter "Exer_type" to the cboTipo.text.

3 - And if there is some gigantic mistake in my code.

(I am using Sql Server 2017, I posted the image code because I am not used to formatting site text yet, I apologize if this makes understanding difficult).

  • 1

    put the code in the question instead of an image

  • 2

    Even if you don’t know how to format the code on the site, insert it instead of the image, someone will format it for you.

  • Okay, I added the code. I used a code to add the values found in Select to a listbox.

1 answer

0

Select I use so @"select Exer_nm from Exercicios Where Exer_type = @Exer_type" (without simple quotes in the understood parameter?)

The method Cmd.parameters.Addwithvalue("@Excer_type", cboTipo.Text); You have to put the @ before the Excer_type Blz? You can use this same event or the Onvaluechenge or similar, but tbm vc can use the Validated event, which runs when the combobox loses focus

It would be good if you used clb.items.Clear() at the beginning of the event So that the listbox is lipo always before being added other items

Browser other questions tagged

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