日期:2014-05-18  浏览次数:20577 次

TreeView数据绑定问题????
错误提示: 阅读器关闭时Read的尝试无效.
  代码:void BinData()
{
System.Data.IDataReader dr=cdb.SelectDep1();
while(dr.Read())
{
TreeNode node=new TreeNode();
node.Text=dr["DepName"].ToString();
System.Data.IDataReader dr2=cdb.SelectDep2((int)dr["DepID"]);
while(dr2.Read())
{
TreeNode node2=new TreeNode();
node2.Text=dr2["DepName"].ToString();
node2.NavigateUrl="admin_Dep.aspx?DepID="+dr2["DepID"].ToString();
node.Nodes.Add(node2);

}
TreeView1.Nodes.Add(node);
}
}



CS 文件代码: 
  1 .. public SqlDataReader SelectDep1()
{
SqlConnection myconnection=new SqlConnection(connstr);
string cmdtext="select * from DepB where DepNum=1";
SqlCommand mycommand=new SqlCommand(cmdtext,myconnection);
myconnection.Open();
SqlDataReader recm=mycommand.ExecuteReader();

myconnection.Close();
return recm;
}

  2 .. public SqlDataReader SelectDep2(int MYDepNumClass)
{
SqlConnection myconnection=new SqlConnection(connstr);
string cmdtext="select * from DepB where DepNumClass=MYDepNUmClass and DepNum=2";
SqlCommand mycommand=new SqlCommand(cmdtext,myconnection);
myconnection.Open();
SqlDataReader recm=mycommand.ExecuteReader();

myconnection.Close();
return recm;
}


  请各位帮忙看看错误 谢谢了  


------解决方案--------------------
帮你顶下!


------解决方案--------------------
myconnection.Close(); //删除这句
SqlDataReader recm=mycommand.ExecuteReader(); 
改为
SqlDataReader recm=mycommand.ExecuteReader(CommandBehavior.CloseConnection); 

while(dr2.Read())

}
之后把dr2.Close()
------解决方案--------------------
string cmdtext="select * from DepB where DepNum=2 and DepNumClass="+你的变量;
------解决方案--------------------
string cmdtext="select * from DepB where DepNum=2 and DepNumClass=" + MYDepNumClass.ToString();