导航
  • 主页
  • ASP
  • .NET Framework
  • Web Services
  • VB
  • VC
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • LINQ
  • 问答

treeview动态生成节点...当数据在一百多条以上就会出现异常.究竟哪里出了问题...

lanye_purple 2006-11-18 11:41:29
我是在web下面引用的第三方控件treeview .运行时数据只能保持在一百条以内..
代码如下:

private void Page_Load(object sender, System.EventArgs e)
{
TreeView1.Nodes.Clear(); //清除TreeView1的节点
InitRootNodeDataTable(id);//生成节点
InitTreeRootNode(TreeView1.Nodes,id.ToString());
}


#region 树 HW

#region 将DataAdapter 转为 DataTable
/// <summary>
/// 将DataReader 转为 DataTable
/// </summary>
/// <param name="DataReader">DataReader</param>
public DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
{
DataTable datatable = new DataTable();
DataTable schemaTable = dataReader.GetSchemaTable();
//动态添加列
foreach(DataRow myRow in schemaTable.Rows)
{
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
//myDataColumn.DataType = myRow[0].GetType();

myDataColumn.ColumnName = myRow[0].ToString();
datatable.Columns.Add(myDataColumn);
}
//添加数据
while(dataReader.Read())
{
DataRow myDataRow = datatable.NewRow();
for(int i=0;i<schemaTable.Rows.Count;i++)
{
myDataRow[i] = dataReader[i].ToString();
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
return datatable;
}
#endregion

#region InitRootNodeDataTable
/// <summary>
/// 初始化 RootNode DataTable
/// </summary>
private void InitRootNodeDataTable(int ClassParentID)
{
Database data = new Database();
SqlDataReader dataReader = null;
SqlParameter[] prams = {
data.MakeInParam("@PositionID",SqlDbType.Int , 20, ClassParentID)
};
try
{
data.RunProc("sp_SF_GetRootPosition",prams, out dataReader);
}
catch(Exception ex)
{
Response.Write(ex.ToString());
//UDS.Components.Error.Log(ex.ToString());
}
dataTbl1 = ConvertDataReaderToDataTable(dataReader);
dataTbl1.TableName = "TreeView";

}
#endregion

#region InitChildNodeDataTable
/// <summary>
/// 初始化 ChildNode DataTable
/// </summary>
private void InitChildNodeDataTable(int ClassParentID)
{
Database data = new Database();
SqlDataReader dataReader = null;
SqlParameter[] prams = {
data.MakeInParam("@PositionID",SqlDbType.Int , 20, ClassParentID)
};
try
{
data.RunProc("sp_SF_GetSubPosition", prams,out dataReader);
}
catch(Exception ex)
{
Response.Write(ex.ToString());
//UDS.Components.Error.Log(ex.ToString());
}
dataTbl2 = ConvertDataReaderToDataTable(dataReader);
dataTbl2.TableName = "TreeView";
}
#endregion

#region InitStaffNodeDataTable
/// <summary>
/// 初始化 SatffNode DataTable
/// </summary>
private void InitStaffNodeDataTable(int ClassParentID)
{
Database data = new Database();
SqlDataReader dataReader = null;
SqlParameter[] prams = {
data.MakeInParam("@PositionId",SqlDbType.Int , 20, ClassParentID)
};
try
{
data.RunProc("sp_SF_GetStaffIDPosition", prams,out dataReader);
}
catch(Exception ex)
{
Response.Write(ex.ToString());
//UDS.Components.Error.Log(ex.ToString());
}
dataTbl3 = ConvertDataReaderToDataTable(dataReader);
dataTbl3.TableName = "TreeView";
}
#endregion

#region InitTreeRootNode
/// <summary>
/// 初始化TreeView 的 RootNode
/// </summary>
private void InitTreeRootNode(TreeNodeCollection TNC,string classParentID)
{
DataView dataView = new DataView();
dataView = dataTbl1.Copy().DefaultView;
//dataView.RowFilter = "Super_Position_ID = Position_ID";
dataView.RowFilter="Position_ID="+classParentID+"";
foreach(DataRowView drv in dataView)
{
TreeNode tn = new TreeNode();
tn.ID = drv["Position_ID"].ToString();
string message=drv["Position_Name"].ToString()+"("+drv["Position_Mark"].ToString().Trim()+")";
tn.Text = "<span onmouseover=javascript:title='"+message+"'>"+message+"</span>";
tn.ImageUrl = GetIcon("8");
//tn.NavigateUrl = "PositionInformation.aspx?PositionID=" + tn.ID;
tn.Target = "main";
TNC.Add(tn);
InitStaffNodeDataTable(Int32.Parse(tn.ID.ToString()));
InitTreeStaffNode(tn.Nodes,tn.ID);
InitChildNodeDataTable(Int32.Parse(tn.ID.ToString()));
InitTreeChildNode(tn.Nodes,tn.ID);



}

dataTbl1 = null;
dataTbl2 = null;
dataTbl3 = null;
}
#endregion

#region InitTreeChildNode
/// <summary>
/// 初始化TreeView 的 ChildNode
/// </summary>
private void InitTreeChildNode(TreeNodeCollection TNC,string classParentID)
{
DataView dataView = new DataView();
dataView = dataTbl2.Copy().DefaultView ;
dataView.RowFilter = "Super_Position_ID = " + classParentID + "";
foreach(DataRowView drv in dataView)
{
TreeNode tn = new TreeNode();
tn.ID = drv["Position_ID"].ToString();
string message=drv["Position_Name"].ToString()+"("+drv["Position_Mark"].ToString().Trim()+")";
tn.Text = "<span onmouseover=javascript:title='"+message+"'>"+message+"</span>";
tn.ImageUrl = GetIcon("9");
//tn.NavigateUrl = "PositionInformation.aspx?PositionID=" + tn.ID;
tn.Target = "main";
TNC.Add(tn);
InitStaffNodeDataTable(Int32.Parse(tn.ID.ToString()));
InitTreeStaffNode(tn.Nodes,tn.ID);
InitChildNodeDataTable(Int32.Parse(tn.ID.ToString()));
InitTreeChildNode(tn.Nodes,tn.ID);


}
}
#endregion

#region InitTreeStaffNode
/// <summary>
/// 初始化TreeView 的StaffNode
/// </summary>
private void InitTreeStaffNode(TreeNodeCollection TNC,string classParentID)
{
DataView dataView =new DataView();
dataView =dataTbl3.Copy().DefaultView;
dataView.RowFilter="Position_ID="+classParentID+"";
foreach(DataRowView drv in dataView)
{
TreeNode tn=new TreeNode();
tn.ID =drv["Staff_ID"].ToString();
string message=UDS.Components.Staff.GetRealNameByStaffId(drv["Staff_ID"].ToString().Trim()).ToString();
tn.Text ="<span onmouseover=javascript:title='"+message+"'>"+message+"</span>";
//根据用户身份显示不同的图标
string mark=drv["MarkId"].ToString();
switch(mark)
{
case"1": tn.ImageUrl=GetIcon("5");
break;
case"2": tn.ImageUrl=GetIcon("6");
break;
case"3": tn.ImageUrl=GetIcon("7");
break;
}

//tn.NavigateUrl="StaffInformation.aspx?StaffID=" + tn.ID;
tn.Target="main";
TNC.Add(tn);

}
}
#endregion

在数据在一百条以上,程序就会出现如下错误:
“/eform3.0”应用程序中的服务器错误。
--------------------------------------------------------------------------------

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 55: {
行 56: DataTable datatable = new DataTable();
行 57: DataTable schemaTable = dataReader.GetSchemaTable();
行 58: //动态添加列
行 59: foreach(DataRow myRow in schemaTable.Rows)


源文件: F:\WebeformSys\back\eform3.0\Inc\ControlAddressBookTreeView.ascx.cs 行: 57
...全文
185 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanye_apurple 2007-03-28
up
回复
lanye_purple 2006-11-18
100条件 只有我大概估计的..数据再多点就不行了..
回复
孟子E章 2006-11-18
代码比较长:可能的问题dataReader在某个地方设置为null了
回复
whu305 2006-11-18
帮顶一下
好复杂 为什么是100不是其他的。。。
回复
lanye_purple 2006-11-18
然后是 初始化TreeView 的 RootNode
private void InitTreeRootNode(TreeNodeCollection TNC,string classParentID)
{
//根结点
DataView dataView = new DataView();
dataView = dataTbl1.Copy().DefaultView;
dataView.RowFilter="Position_ID="+classParentID+"";
foreach(DataRowView drv in dataView)
{
TreeNode tn = new TreeNode();
tn.ID = drv["Position_ID"].ToString();
string message=drv["Position_Name"].ToString()+"("+drv["Position_Mark"].ToString().Trim()+")";
tn.Text = "<span onmouseover=javascript:title='"+message+"'>"+message+"</span>";
tn.ImageUrl = GetIcon("8");
tn.Target = "main";
TNC.Add(tn);
InitStaffNodeDataTable(Int32.Parse(tn.ID.ToString()));
InitTreeStaffNode(tn.Nodes,tn.ID);
InitChildNodeDataTable(Int32.Parse(tn.ID.ToString()));
InitTreeChildNode(tn.Nodes,tn.ID);

}
dataTbl1 = null;
dataTbl2 = null;
dataTbl3 = null;
}
private void InitTreeChildNode(TreeNodeCollection TNC,string classParentID)
{
//部门
........
}
private void InitTreeStaffNode(TreeNodeCollection TNC,string classParentID)
{
//人员
........
}
回复
lanye_purple 2006-11-18
下面主要是将将DataAdapter 转为 DataTable 我有三个存储过程..分别是根结点,部门,人员.
private void Page_Load(object sender, System.EventArgs e)
{
TreeView1.Nodes.Clear(); //清除TreeView1的节点
InitRootNodeDataTable(id);//生成节点
InitTreeRootNode(TreeView1.Nodes,id.ToString());
}

private void InitRootNodeDataTable(int ClassParentID)
{
Database data = new Database();
SqlDataReader dataReader = null;
SqlParameter[] prams = {
data.MakeInParam("@PositionID",SqlDbType.Int , 20, ClassParentID)
};
try
{
data.RunProc("sp_SF_GetRootPosition",prams, out dataReader);
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
dataTbl1 = ConvertDataReaderToDataTable(dataReader);
dataTbl1.TableName = "TreeView";

}

private void InitChildNodeDataTable(int ClassParentID)
{
Database data = new Database();
SqlDataReader dataReader = null;
SqlParameter[] prams = {
data.MakeInParam("@PositionID",SqlDbType.Int , 20, ClassParentID)
};
try
{
data.RunProc("sp_SF_GetSubPosition", prams,out dataReader);
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
dataTbl2 = ConvertDataReaderToDataTable(dataReader);
dataTbl2.TableName = "TreeView";
}
#endregion

private void InitStaffNodeDataTable(int ClassParentID)
{
Database data = new Database();
SqlDataReader dataReader = null;
SqlParameter[] prams = {
data.MakeInParam("@PositionId",SqlDbType.Int , 20, ClassParentID)
};
try
{
data.RunProc("sp_SF_GetStaffIDPosition", prams,out dataReader);
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
dataTbl3 = ConvertDataReaderToDataTable(dataReader);
dataTbl3.TableName = "TreeView";
}
回复
lanye_purple 2006-11-18
呵呵..代码是有点长..但也没有那么难理解..我分细一下..
回复
代码太乱啊,如孟子所说,有可能字段内那些字段为空了
最好跟踪一下
回复
发动态
发帖子
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
社区公告
暂无公告