

Navigator : Home > Tutorials > Controls Tutorials > ...
SQL Database Driven Menu in ASP.NET (C#)
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
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
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
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.
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!