我有一个下拉列表,它绑定数据库表中的数据,我可以显示它,但是当我想选择下拉列表值时显示数据时,它什么也不能显示
这是我的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = GetData();
DropDownList1.DataTextField = "DEPT_NAME";
DropDownList1.DataValueField = "DEPT_NO";
DropDownList1.DataBind();
}
}
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = DropDownList1.SelectedValue;
using (SqlConnection conn = new SqlConnection(@"Data Source=server;Initial Catalog=DB;User ID=id;Password=pw"))
{
using (SqlCommand cmd = new SqlCommand("SELECT TEL_NO,EMP_NAME,EMP_POSITION FROM tel WHERE DEPT_NO =" +value, conn))
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
this.GridView1.Visible = true;
GridView1.DataSource = dr;
GridView1.DataBind();
dr.Close();
conn.Close();
}
}
}
DataTable GetData()
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(@"Data Source=server;Initial Catalog=DB;User ID=id;Password=pw"))
{
using (SqlCommand cmd = new SqlCommand("SELECT 'Please Select' AS DEPT_NAME , '000' AS DEPT_NO UNION SELECT DEPT_NAME , DEPT_NO FROM dept ORDER BY DEPT_NO ASC", conn))
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
}
}
return dt;
}
using (SqlCommand cmd = new SqlCommand("SELECT TEL_NO,EMP_NAME,EMP_POSITION FROM tel WHERE DEPT_NO = @value", conn))
cmd.Parameter.AddWithValue("@value",value);
"SELECT TEL_NO,EMP_NAME,EMP_POSITION FROM tel WHERE DEPT_NO =" +value
如果DEPT_NO数据类型为数字,则查询()将有效
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句