从数据库构建树

时间:2012-10-16 07:23:55

标签: c# asp.net

我是asp.net的新手,我正在尝试从数据库构建一个用于学习asp.net的树视图,我从互联网下载一些代码并尝试运行它。但有一些错误,我真的不明白问题是什么

这是表格

    CREATE TABLE [dbo].[node](
    [id] [int] NOT NULL,
    [title] [varchar](255) NULL,
    [parent_id] [int] NULL,
    [oid] [int] NULL,
    [display] [tinyint] NULL,
 CONSTRAINT [PK_note] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

代码我添加了一些更改

public partial class index : System.Web.UI.Page
{
    private static TreeView TreeView1 = new TreeView();

    protected void Page_Load(object sender, EventArgs e)
    {
        buildTree();
    }


    private void buildTree()
    {

        SqlConnection dbCon = new SqlConnection(ConfigurationManager.ConnectionStrings["earchConnectionString"].ConnectionString);
        dbCon.Open();
        string sql = "Select * from [node]";
        SqlDataAdapter adapter = new SqlDataAdapter(sql, dbCon);

        DataSet ds = new DataSet();

        adapter.Fill(ds);
        dbCon.Close();

        ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["id"], ds.Tables[0].Columns["parent_id"]);
        foreach (DataRow dbRow in ds.Tables[0].Rows)
        {
            if (dbRow["parent_id"]=="0")
            {
                TreeNode newNode = CreateNode(dbRow["title"].ToString(), dbRow["id"].ToString(), true);
                TreeView1.Nodes.Add(newNode);
                PopulateSubTree(dbRow, newNode);

            }
        }
    }

    private void PopulateSubTree(DataRow dbRow, TreeNode node)
    {

        foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
        {
            TreeNode childNode = CreateNode(childRow["title"].ToString(), childRow["id"].ToString(), true);

            node.ChildNodes.Add(childNode);
            PopulateSubTree(childRow, childNode);
        }
    }

    //
    private TreeNode CreateNode(string text, string id, bool expanded)
    {
        TreeNode node = new TreeNode(); ;
        node.Text = text;
        node.Value = id;
        node.Expanded = true;
        return node;

    }
}

错误:

This constraint cannot be enabled as not all values have corresponding parent values.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.

Source Error: 


Line 32:         dbCon.Close();
Line 33: 
Line 34:         ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["id"], ds.Tables[0].Columns["parent_id"]);
Line 35:         foreach (DataRow dbRow in ds.Tables[0].Rows)
Line 36:         {

Source File: c:\inetpub\web1\index.aspx.cs    Line: 34 

Stack Trace: 


[ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.]
   System.Data.ConstraintCollection.AddForeignKeyConstraint(ForeignKeyConstraint constraint) +1932628
   System.Data.ConstraintCollection.Add(Constraint constraint, Boolean addUniqueWhenAddingForeign) +468
   System.Data.DataSetRelationCollection.AddCore(DataRelation relation) +947
   System.Data.DataRelationCollection.Add(DataRelation relation) +169
   System.Data.DataRelationCollection.Add(String name, DataColumn parentColumn, DataColumn childColumn) +47
   index.buildTree() in c:\inetpub\web1\index.aspx.cs:34
   index.Page_Load(Object sender, EventArgs e) in c:\inetpub\web1\index.aspx.cs:17
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

任何人都可以定义问题吗?我将parent_id更改为null现在正常(并且检查根不是0而不是null),但最后,如何在网页中显示?

1 个答案:

答案 0 :(得分:0)

问题是有一些节点(第一级节点)没有父ID。当您尝试创建de relation时,内部代码默认创建一个froeing键。添加“false”作为第四个参数以避免这种情况。

ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["id"],.Tables[0].Columns["parent_id"], false);

PS:对不起我的英语,西班牙语发言人

相关问题