Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:50:05+00:00 2026-06-12T22:50:05+00:00

i’m new to asp.net and i’m trying to build a treeview from database for

  • 0

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

here is the table

    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]

the code i’ve add some alteration

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;

    }
}

Errors:

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:50:07+00:00Added an answer on June 12, 2026 at 10:50 pm

    the problem is that there are some node (first level nodes) that have no parent id. When you try to create de relation, the inner code create a froeing key by default. Add “false” as 4th parameter to avoid that.

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

    PS: sorry for my English, Spanish speaker

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.