Click here to Skip to main content
15,908,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i am new to programing.i was doing the project in that i want to display menus from database(mysql). In that database i use two tables such as tabs and tabpages. i use sitemap to show the data from database. in this i can get only the parentitems from table tabs,and children items is not showing in my menucontrol. can anybody help me?.
i am using C# asp.net.
my code below:
i am using the menu control in masterpage.
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateMenuControl();
        PopulateMenu();
    }


    private void CreateMenuControl()
    {

        Menu1.DataSource = GetSiteMapDataSource();
        Menu1.DataBind();
       
    }

    private SiteMapDataSource GetSiteMapDataSource()
    {
        XmlSiteMapProvider xmlSiteMap = new XmlSiteMapProvider();
        System.Collections.Specialized.NameValueCollection myCollection = new System.Collections.Specialized.NameValueCollection(1);
        myCollection.Add("siteMapFile", "Web.sitemap");
        xmlSiteMap.Initialize("provider", myCollection);
        xmlSiteMap.BuildSiteMap();
        SiteMapDataSource siteMap = new SiteMapDataSource();
        return siteMap;
    }
    

    private void PopulateMenu()
    {
        DataSet ds = GetDataSetForMenu();
        Menu menu = new Menu();

        foreach (DataRow parentItem in ds.Tables["tabs"].Rows)
        {
            MenuItem categoryItem = new MenuItem((string)parentItem["tabname"]);
            menu.Items.Add(categoryItem);

            foreach (DataRow childItem in parentItem.GetChildRows("tabpages"))
            {
                MenuItem childrenItem = new MenuItem((string)childItem["pagename"]);
                categoryItem.ChildItems.Add(childrenItem);
            }
        }
        Panel1.Controls.Add(menu);
        Panel1.DataBind();

    }

    private DataSet GetDataSetForMenu()
    {
        MySqlConnection myConnection = new MySqlConnection();
        string conn = @"Server=127.0.0.1;Database=find;user=root;password=Server123";
          myConnection.ConnectionString=conn;
          myConnection.Open();
          MySqlDataAdapter adtap = new MySqlDataAdapter("select * from tabs",myConnection);
          MySqlDataAdapter adtapage = new MySqlDataAdapter("select * from tabpages", myConnection);
          DataSet ds = new DataSet();
          adtap.Fill(ds,"tabs");
          adtapage.Fill(ds,"tabpages");
          ds.Relations.Add("tabpages", ds.Tables["tabs"].Columns["tabid"], ds.Tables["tabpages"].Columns["tabno"]);
          return ds;
    }


}
Posted
Updated 23-Aug-11 19:39pm
v3
Comments
Prerak Patel 24-Aug-11 1:17am    
Use code block for code segment.
parithi vr 24-Aug-11 1:40am    
ok.thanks
Victoria Rowe 13-Sep-11 3:24am    
Any reason you are not using the asp:menu control? I would have though the asp:menu control would pick up the child pages no problem if you have them in SiteMap file. Easy to link to this too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900