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 6633801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:51:20+00:00 2026-05-25T22:51:20+00:00

i want to add the parent and child nodes from sql server to treeview.

  • 0

i want to add the parent and child nodes from sql server to treeview. i implemented some code. but i gets error “Index was out of range”

below is the code i am using to fill parent and child nodes.

protected void GetParentNodes()
    {
        SqlDataAdapter adap = new SqlDataAdapter("select id, name from crossarticle_category where parentid=-1", con);
        DataTable dt = new DataTable();
        adap.Fill(dt);
        int index = -1;
        foreach (DataRow d in dt.Rows)
        {
            SqlDataAdapter adapInner = new SqlDataAdapter("select id, name from crossarticle_category where parentid=" + Convert.ToInt32(d["id"].ToString()) + "", con);
            DataTable dtInner = new DataTable();
            adapInner.Fill(dtInner);
            index++;
            TreeNode n = new TreeNode();
            n.Value = d["id"].ToString();
            n.Text = d["name"].ToString();
            foreach (DataRow r in dtInner.Rows)
            {
                if (dtInner.Rows.Count > 0)
                {
                    TreeNode inner = new TreeNode();
                    inner.Value = r["id"].ToString();
                    inner.Text = r["name"].ToString();
                    tree1.Nodes[index].ChildNodes.Add(inner);
                }
            }
            tree1.Nodes.Add(n);
        }
    }

can anyone help me rectify the issue in this code.. this code has been created by myself.

  • 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-05-25T22:51:20+00:00Added an answer on May 25, 2026 at 10:51 pm

    Looks like you are trying to add a child node before you add the parent. Try adding the parent first and then the child nodes; like so:

    ///...
    
    n.Value = d["id"].ToString();
    n.Text = d["name"].ToString();
    
    tree1.Nodes.Add(n);
    

    And then

    foreach (DataRow r in dtInner.Rows)
    {
        if (dtInner.Rows.Count > 0)
        {
            TreeNode inner = new TreeNode();
            inner.Value = r["id"].ToString();
            inner.Text = r["name"].ToString();
            tree1.Nodes[index].ChildNodes.Add(inner); //node at pos index should exist now
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add textbox in treeview as child of one of the parent
I have a website built with ASP.NET (3.5) and want add some level of
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
I want to know why if I have a parent and child relation and
So i got this treeview where i want to display some people with as
i want to add the check box to the child node of a certain
HI, I have a parent - child relationship and want to query putting a
I have a WinForm TreeView Control that displays the Parent Child relationship of CaseNotes(I
I have an object-oriented parent-child tree in PHP that I want to clone. The
I want to add additional stuff to the parent class. I can't modify the

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.