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();
}
记住加主键