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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:26:48+00:00 2026-05-14T01:26:48+00:00

I’ve data which looks something like this. | id | name | depth |

  • 0

I’ve data which looks something like this.

| id  | name                 | depth | itemId |
+-----+----------------------+-------+-------+
|   0 |  ELECTRONICS         |     0 |  NULL |
|   1 | TELEVISIONS          |     1 |  NULL |
| 400 | Tube                 |     2 |  NULL |
| 432 | LCD                  |     3 |  1653 |
| 422 | Plasma               |     3 |  1633 |
| 416 | Portable electronics |     3 |  1595 |
| 401 | MP3 Player           |     3 |  1249 |
| 191 | Flash                |     2 |  NULL |
| 555 | CD Players           |     3 |  2198 |
| 407 | 2 Way Radio          |     3 |  1284 |
| 388 | I've a problem with  |     3 |  1181 |
| 302 | What is your bill pa |     3 |   543 |
| 203 | Where can I find my  |     3 |   299 |
| 201 | I would like to make |     3 |   288 |
| 200 | Do you have any job  |     3 |   284 |
| 192 | About Us             |     3 |  NULL |
| 199 | What can you tell me |     4 |   280 |
| 198 | Do you help pr       |     4 |   276 |
| 197 | would someone help co|     4 |   272 |
| 196 | can you help ch      |     4 |   268 |
| 195 | What awards has Veri |     4 |   264 |
| 194 | What's the latest ne |     4 |   260 |
| 193 | Can you tell me more |     4 |   256 |
| 180 | Site Help            |     2 |  NULL |
| 421 | Where are the        |     3 |  1629 |
| 311 | How can I access My  |     3 |   557 |
| 280 | Why isn't the page a |     3 |   512 |

To convert the above data into unordered list based on depth, I’m using the following code

int lastDepth = -1;
int numUL = 0;

StringBuilder output = new StringBuilder();


foreach (DataRow row in ds.Tables[0].Rows)
{

    int currentDepth = Convert.ToInt32(row["Depth"]);

    if (lastDepth < currentDepth)
    {
        if (currentDepth == 0)
        {
            output.Append("<ul class=\"simpleTree\">");
            output.AppendFormat("<li class=\"root\"><span><a href=\"#\" title=\"root\">root</a></span><ul><li class=\"open\" ><span><a href=\"#\" title={1}>{0}</a></span>", row["name"],row["id"]);
        }
        else
        {
            output.Append("<ul>");
            if(currentDepth==1)
            output.AppendFormat("<li><span>{0}</span>", row["name"]);
            else
                output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
        }
        numUL++;
    }
    else if (lastDepth > currentDepth)
    {
        output.Append("</li></ul></li>");
        if(currentDepth==1)
        output.AppendFormat("<li><span>{0}</span>", row["name"]);
        else
            output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
        numUL--;
    }
    else if (lastDepth > -1)
    {
        output.Append("</li>");
        output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"],row["id"]);
    }


    lastDepth = currentDepth;
}

for (int i = 1; i <= numUL+1; i++)
{
    output.Append("</li></ul>");
}

myliteral.text=output.ToString();

But the resulting unordered list doesnt seem to be forming properly(using which i am constructing a tree).For example “Site Help” with id ‘180’ is supposed to appear as a direct child of “Televisions” with id ‘1’,is appearing as a direct child of ‘Flash’ with id ‘191’ using my code.so in addition to considering depth,I’ve decided to consider itemid as well in order to get the treeview properly.Those rows of the table with itemId not equal to null are not supposed to have a child node(i.e.,they are the leaf nodes in the tree) and all the other nodes can have child nodes.

Please could someone help me in constructing a proper unordered list based on my depth,itemid columns?

Update:

The ‘itemid’ column refers to the id of an item which is present in another table.This column just helps in identifying if an item has any sub items(i.e., ‘name’ in my data in this case has any other unordered lists under it).

  • 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-14T01:26:48+00:00Added an answer on May 14, 2026 at 1:26 am

    It looks like the initial code wasn’t taking account of the number of levels back up you needed to go from the “Can you tell me more” node to the “Site Help” node.

    int lastDepth = -1;
            int numUL = 0;
    
            StringBuilder output = new StringBuilder();
    
            foreach (DataRow row in ds.Tables[0].Rows)
            {
    
                int currentDepth = Convert.ToInt32(row["Depth"]);
    
                if (lastDepth < currentDepth)
                {
                    if (currentDepth == 0)
                    {
                        output.Append("<ul class=\"simpleTree\">");
                        output.AppendFormat("<li class=\"root\"><span><a href=\"#\" title=\"root\">root</a></span><ul><li class=\"open\" ><span><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
                    }
                    else
                    {
                        output.Append("<ul>");
                        if (currentDepth == 1)
                            output.AppendFormat("<li><span>{0}</span>", row["name"]);
                        else
                            output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
                    }
                    numUL++;
                }
                else if (lastDepth > currentDepth)
                {
                    //output.Append("</li></ul></li>");
                    output.Append("</li>");
                    for (int i = lastDepth; i > currentDepth; i--)
                    {
                        output.Append("</ul></li>");
                    }
    
                    if (currentDepth == 1)
                        output.AppendFormat("<li><span>{0}</span>", row["name"]);
                    else
                        output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
                    numUL--;
                }
                else if (lastDepth > -1)
                {
                    output.Append("</li>");
                    output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
                }
    
    
                lastDepth = currentDepth;
            }
    
            for (int i = 1; i <= numUL + 1; i++)
            {
                output.Append("</li></ul>");
            }
    
            myliteral.Text = output.ToString();
    

    Stuart.

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

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer use the following code and pass the window handle to… May 14, 2026 at 6:37 pm
  • Editorial Team
    Editorial Team added an answer Check the following: Check if you your data-access library to… May 14, 2026 at 6:37 pm
  • Editorial Team
    Editorial Team added an answer If your acceleration vector is a = (x, y, z),… May 14, 2026 at 6:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.