【发布时间】:2020-06-27 08:29:01
【问题描述】:
我正在尝试从两个下拉列表中获取文本以进行查询,然后将该执行查询的值插入到一堆下拉列表中。 Visual Studio 没有显示任何类型的错误,但下拉列表不会更新。如果aspx页面上的下拉列表与codeBehind不匹配,请不要介意,有很多。
前端部分
<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>
代码隐藏
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);
}
【问题讨论】:
-
这段代码有几处非常错误:你容易被SQL注入,使用查询参数而不是字符串连接;你没有清理资源,我希望你是。您的
for循环过于复杂。您可以简单地将数据表设置为每个下拉列表的DataSource,将DataMember设置为列名并在它们上调用DataBind(),除非您要附加到以前的值。
标签: c# asp.net sql-server ado.net