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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:55:13+00:00 2026-05-16T06:55:13+00:00

I have a table structure like below : categoryID bigint , primary key ,

  • 0

I have a table structure like below :

categoryID bigint , primary key , not null
categoryName nvarchar(100) 
parentID bigint, not null

where as categoryID and parentID has an one-to-many relation to each other

and I want to create a nested categories with unlimited depth out of this table in my program.

I have a solution but it is not work so good and only returns the root please see the code :

    private static string createlist(string catid, string parent)
    {

        string sql = "SELECT categoryID , categoryName FROM category WHERE parentID = " + parent;
        SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=sanjab;Integrated Security=True");
        cn.Open();
        SqlCommand cmd = new SqlCommand(sql, cn);
        SqlDataReader sdr = cmd.ExecuteReader();

        while (sdr.Read())
        {
            if (catid != "")
                catid += ", ";
            catid += sdr[1].ToString();
            createlist(catid, sdr[0].ToString());


        }
        return catid;
    }

although the code is not very efficient cause it is opening a lots of connection at the same time but with the help of above code and little bit of tweaking I can manage 2-levels depth categories but more than that means lots of trouble for me.

is there any easier method or algorithm?

regards.

  • 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-16T06:55:14+00:00Added an answer on May 16, 2026 at 6:55 am

    Nested Sets are one well-regarded solution for the SQL end of this equation. It requires a bit more application logic, so you’ll want to abstract out the behavior to the extent that you can.

    They have some limitations if the elements have frequent inserts or deletes, but that shouldn’t apply in your case. The solution when you do have frequent inserts or deletes is usually to create numbering gaps among the children.

    http://www.sqlteam.com/article/more-trees-hierarchies-in-sql has references to an early Celko article on the topic, and some contemporary variations on the theme.

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

Sidebar

Related Questions

I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a mysql table structure like that: id int primary key name varchar
I have a mysql database with a table structure like below : Table Name
We have a table with tree structure like this: Id Desc ParentID === A
I have a table T (structure below) which initially contains all-NULL values in an
I have a database table that has a structure like the one shown below:
I have table structure like below: events (boxing, sparring etc) competitors (users who are
Assume that I have a table structure like seen below location|name ------------- NY|john London|neil
I have a table structure like the one shown below: <table id=result> <tr> <td
I have a table structure like the one below and I would like to

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.