This tutorial will show you how to binding a Menu Control to SQL Server database using C#. By this sample, you can create a database driven menu in ASP.Net.
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;
}
|
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
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);
}
}
}
|
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
PopulateMenu();
}
|
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!