Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to create a menu and the menu items should come from the database.
Posted

Create a method of GetMenuData to read data of menu from database to dataset.
DataSet GetMenuData()
{
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter dadCats = new SqlDataAdapter("SELECT * FROM Categories", con);
SqlDataAdapter dadProducts = new SqlDataAdapter("SELECT * FROM Products", con);
DataSet dst = new DataSet();
dadCats.Fill(dst, "Categories");
dadProducts.Fill(dst, "Products");
dst.Relations.Add("Children",
dst.Tables["Categories"].Columns["CategoryID"],
dst.Tables["Products"].Columns["CategoryID"]);
return dst;
}

Fill data to menu control
public void PopulateMenu()
{
DataSet dst = GetMenuData();
foreach (DataRow masterRow in dst.Tables["Categories"].Rows)
{
MenuItem masterItem = new MenuItem((string)masterRow["CategoryName"]);
Menu1.Items.Add(masterItem);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
MenuItem childItem = new MenuItem((string)childRow["ProductName"]);
masterItem.ChildItems.Add(childItem);
}
}
}

Page Load
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
PopulateMenu();
}


Get an alternative here with source code:http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c15661[^]
 
Share this answer
 
Comments
NandaKumer 20-Jan-12 14:08pm    
good one
 
Share this answer
 

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