我按如下代码绑定数据,
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection CN = new SqlConnection();
try
{
//初始化连接字符串
CN.ConnectionString="server=localhost;database=Menu;User ID=sa;password=";
CN.Open();
//添加命令,从数据库中得到数据
SqlCommand sqlCmd= new SqlCommand();
sqlCmd.Connection = CN;
sqlCmd.CommandText = "select * from Mmm";
sqlCmd.CommandType = CommandType.Text ;
SqlDataAdapter adp = new SqlDataAdapter(sqlCmd);
adp.Fill(ds);
}
catch (Exception ex)
{
throw (ex);
}
finally
{
CN.Close();
}
//调用递归函数,完成树形结构的生成
AddTree(11, (TreeNode)null);
} // 递归添加树的节点
public void AddTree(int ParentID,TreeNode pNode)
{
DataView dvTree = new DataView(ds.Tables[0]);
//过滤no,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID;
foreach(DataRowView Row in dvTree)
{
if(pNode == null)
{
TreeNode Node=new TreeNode();
Node.Text=Row["MenuName"].ToString();
Node.Text=Row["MenuID"].ToString();
pNode.Nodes.Add(Node);
AddTree(Int32.Parse(Row["MenuID"].ToString()),Node); //再次递归
}
else
{
TreeNode Node = pNode.Nodes.Add(Row["MenuName"].ToString());
AddTree(Int32.Parse(Row["MenuID"].ToString()),Node);//再次递归
}
}
}
可以编译,但调试出现下面错误;Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error:
Line 65: Node.Text=Row["MenuName"].ToString();
Line 66: Node.Text=Row["MenuID"].ToString();
Line 67: pNode.Nodes.Add(Node);
Line 68: AddTree(Int32.Parse(Row["MenuID"].ToString()),Node); //再次递归
Line 69: }
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection CN = new SqlConnection();
try
{
//初始化连接字符串
CN.ConnectionString="server=localhost;database=Menu;User ID=sa;password=";
CN.Open();
//添加命令,从数据库中得到数据
SqlCommand sqlCmd= new SqlCommand();
sqlCmd.Connection = CN;
sqlCmd.CommandText = "select * from Mmm";
sqlCmd.CommandType = CommandType.Text ;
SqlDataAdapter adp = new SqlDataAdapter(sqlCmd);
adp.Fill(ds);
}
catch (Exception ex)
{
throw (ex);
}
finally
{
CN.Close();
}
//调用递归函数,完成树形结构的生成
AddTree(11, (TreeNode)null);
} // 递归添加树的节点
public void AddTree(int ParentID,TreeNode pNode)
{
DataView dvTree = new DataView(ds.Tables[0]);
//过滤no,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID;
foreach(DataRowView Row in dvTree)
{
if(pNode == null)
{
TreeNode Node=new TreeNode();
Node.Text=Row["MenuName"].ToString();
Node.Text=Row["MenuID"].ToString();
pNode.Nodes.Add(Node);
AddTree(Int32.Parse(Row["MenuID"].ToString()),Node); //再次递归
}
else
{
TreeNode Node = pNode.Nodes.Add(Row["MenuName"].ToString());
AddTree(Int32.Parse(Row["MenuID"].ToString()),Node);//再次递归
}
}
}
可以编译,但调试出现下面错误;Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error:
Line 65: Node.Text=Row["MenuName"].ToString();
Line 66: Node.Text=Row["MenuID"].ToString();
Line 67: pNode.Nodes.Add(Node);
Line 68: AddTree(Int32.Parse(Row["MenuID"].ToString()),Node); //再次递归
Line 69: }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货