trying to do UL list by loops in a generalized way

Hierarchy layout

Table MenuH
Menu       Sub
      1      1
      2      2
      3      3
      3      4
      3      5
      5      6

Table Menu
ID     description
1      home
2      about us
3      contact
4      contact 1
5      contact 2
6      contact under 5

trying to do a heirarchal menu like above
the results to be,

   <li>about us</li>
            <li>contact 1</li>
            <li>contact 2
                      <li>contract 5</li>

trying to write a few loops, in c#, and using MS SQL to do the above but not getting anywhere, could some one help?  thanks a lot
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
Recursion is your friend ;-)

I added an <a href> but maybe you have another way to handle clicking on menu choices.
        // global/class var...
        SqlConnection oConn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=eeQ_28266899;Trusted_Connection=SSPI;");

        protected string GetMenu(int prnt = 0) {
            string ret = string.Empty;
            string sSQL = "SELECT, Menu.description FROM MenuH INNER JOIN Menu On = MenuH.Sub WHERE ";
            string sWhere = (prnt == 0) ? "Menu = Sub" : string.Format("Menu = {0} AND Sub <> Menu", prnt);
            using (DataTable dt = new DataTable()) {
                using (SqlCommand cmd = new SqlCommand(sSQL + sWhere, oConn)) {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd)) {
                if (dt.Rows.Count > 0) {
                    ret += "<ul>";
                    foreach (DataRow r in dt.Rows) {
                        ret += string.Format(
                            "<li><a href='menu.aspx?id={1}'>{0}</a>{2}</li>",
                    ret += "</ul>";
            return ret;

Open in new window

And then simply add to your page:
<%= GetMenu() %>

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
goodkAuthor Commented:
Detailed, Correct and to the point solution.  Thanks a lot  robert_schutt:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.