当我要删除部门名"bb",此时部门bb里并无员工
public void DataList_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
string str="select count(*) as count,users.dep_id,dep_name from users,dep where users.dep_id=dep.dep_id group by users.dep_id,dep_name";
com=new SqlCommand(str,conn);
conn.Open();
SqlDataReader dr=com.ExecuteReader();
while(dr.Read())
{
int count = Convert.ToInt32(dr["count"]); //count=5 ???
string depname=dr["dep_name"].ToString();
if(count==0)
{string str1="delete * from dep where dep_name=depname";
com1=new SqlCommand(str1,conn);
com1.ExecuteNonQuery();}
else
{
Response.Write("<script>javascript:alert('该部门里还有员工,不允许删除!!!');window.location='dep_manage.aspx'</script>"); //跳到这一步
}
}
dr.Close();
conn.Close();
ListBind();
}
private void ListBind()
{
string strsql;
strsql="select dep_id,dep_name,leader_name,zhize from dep";
ds=new DataSet();
da=new SqlDataAdapter(strsql,conn);
conn.Open();
da.Fill(ds,"Dep");
DataList1.DataSource=ds.Tables[0].DefaultView;
DataList1.DataBind();
conn.Close();
}
[此贴子已经被作者于2007-6-2 13:25:49编辑过]