building a tree from database
<p>i'm new to asp.net and i'm trying to build a treeview from database for learning asp.net, and i download some code from internet and try to run it. but there is a few error and i don't really understand what the problem is</p>

<p>here is the table</p>

<pre><code>    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]
</code></pre>

<p>the code i've add some alteration</p>

<pre><code>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;

    }
}
</code></pre>

<p>Errors:</p>

<pre><code>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
</code></pre>

<p>Anyone can define the problems? i changed  parent_id to null is ok now (and check the root not by 0 not by null), but finally, how to display in webpages? </p>
