您的位置:首页 > 编程语言 > ASP

关于asp.net中两个DropDownList之间数据库调用传递选项

2011-09-19 20:14 363 查看
private void fillProvince()

{

string strCnn = "data source=.;initial catalog=liaotian;integrated security=true";

SqlConnection sqlCnn = new SqlConnection(strCnn);

SqlCommand sqlCmm = new SqlCommand();

sqlCmm.Connection = sqlCnn;

sqlCmm.CommandText = "SELECT * FROM bumen1";

SqlDataReader reader = null;

try

{

sqlCnn.Open();

reader = sqlCmm.ExecuteReader();

ListItem li;

while (reader.Read())

{

li = new ListItem(reader["部门"].ToString(), reader["id"].ToString());

this.DropDownList1.Items.Add(li);

}

}

catch

{

this.Response.Write("数据库错误");

}

finally

{

reader.Close();

sqlCnn.Close();

}

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

string strCnn = "data source=.;initial catalog=liaotian;integrated security=true";

SqlConnection sqlCnn = new SqlConnection(strCnn);

SqlCommand sqlCmm = new SqlCommand();

sqlCmm.Connection = sqlCnn;

sqlCmm.CommandText = "SELECT id,职位 FROM zhiwei WHERE
pid=@id";

SqlParameter param = new SqlParameter("@id", System.Data.SqlDbType.Int);

param.Value = this.DropDownList1.SelectedValue;

sqlCmm.Parameters.Add(param);

SqlDataReader reader = null;

try

{

sqlCnn.Open();

reader = sqlCmm.ExecuteReader();

this.DropDownList2.DataSource = reader;

this.DropDownList2.DataTextField = "职位";

this.DropDownList2.DataValueField = "id";

this.DropDownList2.Items.Clear();

this.DropDownList2.Items.Add(new ListItem("--请选择职位--", "0"));

this.DropDownList2.DataBind();

}

catch { throw; }

finally

{

reader.Close();

sqlCnn.Close();

}

记住加主键
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: