-2

I'm trying to get the text from two drop down list to make a query then insert the values of that executed query inside bunch of drop down list. Visual studio doesn't show me any kind of error, but the drop down list do not update. Don't mind if the drop down list on the aspx page do not match with the codeBehind,there are a lot of them.

Front-End Part

<div class="column has-text-centered  ">
<div class="box height-20   ">
    <div class="spacing-5">

        <asp:DropDownList ID="hor_lun_c1" CssClass="ddl-width-200 has-text-justified " runat="server">
        </asp:DropDownList>

    </div>

    <div class="spacing-5">

        <asp:DropDownList ID="ddl_lun_c1_nom_mat" CssClass="ddl-width-200 has-text-justified " runat="server">
        </asp:DropDownList>
    </div>

    <div class="spacing-5">

        <asp:DropDownList ID="ddl_lun_c1_nom_prof" CssClass="ddl-width-200 has-text-justified " runat="server">
        </asp:DropDownList>
    </div>
    <div class="spacing-5">
        <asp:DropDownList ID="ddl_lun_c1_nom_salle" CssClass="ddl-width-200 has-text-justified " runat="server"></asp:DropDownList>
    </div>
    <div class="spacing-5">

        <asp:DropDownList ID="ddl_lun_c1_ratt_ou_exam" CssClass="ddl-width-200 has-text-justified " runat="server"></asp:DropDownList>
    </div>

</div>

codeBehind

string nom_na = ddl_na1.SelectedValue;

string fil = ddl_fil.SelectedValue;
MySqlConnection conn = new MySqlConnection(con);
conn.Open();

MySqlCommand charger_li_prof_cmd = conn.CreateCommand();
charger_li_prof_cmd.CommandType = CommandType.Text;
charger_li_prof_cmd.CommandText = "SELECT Professeur.abrv_nom_prof FROM Professeur,Niv_academique,Filiere " +
    "WHERE Niv_academique.nom_na = '" + nom_na + "' AND Filiere.nom_fil = '" + fil + "'  ";

charger_li_prof_cmd.ExecuteNonQuery();

MySqlDataAdapter da_li_prof = new MySqlDataAdapter(charger_li_prof_cmd);
DataTable dt_li_prof = new DataTable();
da_li_prof.Fill(dt_li_prof);
for (int i = 0; i < dt_li_prof.Rows.Count; i++)
{
    string listprof = dt_li_prof.Rows[i].ItemArray[0].ToString();


    ddl_lun_c1_nom_prof.Items.Add(listprof);


    ddl_lun_c2_nom_prof.Items.Add(listprof);


    ddl_lun_c3_nom_prof.Items.Add(listprof);


    ddl_lun_c4_nom_prof.Items.Add(listprof);


    ddl_mar_c1_nom_prof.Items.Add(listprof);


    ddl_mar_c2_nom_prof.Items.Add(listprof);


    ddl_mar_c3_nom_prof.Items.Add(listprof);


    ddl_mar_c4_nom_prof.Items.Add(listprof);


    ddl_mer_c1_nom_prof.Items.Add(listprof);


    ddl_mer_c2_nom_prof.Items.Add(listprof);


    ddl_mer_c3_nom_prof.Items.Add(listprof);


    ddl_mer_c4_nom_prof.Items.Add(listprof);


    ddl_jeu_c1_nom_prof.Items.Add(listprof);


    ddl_jeu_c2_nom_prof.Items.Add(listprof);


    ddl_jeu_c3_nom_prof.Items.Add(listprof);


    ddl_jeu_c4_nom_prof.Items.Add(listprof);


    ddl_ven_c1_nom_prof.Items.Add(listprof);


    ddl_ven_c2_nom_prof.Items.Add(listprof);


    ddl_ven_c3_nom_prof.Items.Add(listprof);


    ddl_ven_c4_nom_prof.Items.Add(listprof);


    ddl_sam_c1_nom_prof.Items.Add(listprof);


    ddl_sam_c2_nom_prof.Items.Add(listprof);


    ddl_sam_c3_nom_prof.Items.Add(listprof);


    ddl_sam_c4_nom_prof.Items.Add(listprof);
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 1
    A couple of things *extremely wrong* with this code: You are prone to SQL injection, use query parameters rather than string concatenation; you are not cleaning up the resources, I hope you are. Your `for` loop is overly complicated. You can simply set the datatable as the `DataSource` of each drop down list, set `DataMember` to the column name and call `DataBind()` on them unless you are appending to previous values. – Tanveer Badar Mar 16 '20 at 08:19

1 Answers1

1

You can bind a DataTable directly to a DropDownList without having to add each item individually.

ddl_lun_c1_nom_prof.DataSource = dt_li_prof;
ddl_lun_c1_nom_prof.DataValueField = "itemID";
ddl_lun_c1_nom_prof.DataTextField = "field01";
ddl_lun_c1_nom_prof.DataBind();

Also, use parameters in your query: Why do we always prefer using parameters in SQL statements?

VDWWD
  • 35,079
  • 22
  • 62
  • 79