在选定的下拉列表项目上运行SQL,并在文本框ASP.NET中显示SQL结果



我一直在尝试运行SQL查询,如果用户从下拉列表中选择项目并将SQL结果放入文本框中。

下拉菜单显示移动模型名称。单击模型应检索和在文本框中显示。

我已经设置了下拉列表以从表"产品"

加载模型
 DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select Price from Products WHERE Model='" +DropDownList1.SelectedIndexChanged+ "'", con);
 da.Fill(dt);
        TextBox1.Text = dt.Rows[0]["Price"].ToString();

,但是即使我单击下拉菜单

,文本框中也没有显示任何内容

如果您使用的是ASP.NET,则必须将下拉列表的AutoPostback设置为true。然后在dropdownlistrongelectedIndexchanged

中执行您的代码

您的SQL命令无效。应该是

SqlDataAdapter da = new SqlDataAdapter("select Price from Products WHERE
Model='" +DropDownList1.SelectedText+ "'", con);

DropDownList1.SelectedValue 

DropDownList1.SelectedText

不应该是

DropDownList1.SelectedIndexChanged

最新更新