In this article we will learn how to bind data from Database to a MenuControl
Our Menu will be like above
Let’s describe our Table:

Name:MenuContents
MenuItemId int
MenuItemDesc varchar(50)
ParentId varchar(50)
Path varchar(50)


Our Table Data will be like above:
Here Path is very important.According to this path data is added to the Menu

Now retrieve data from data base. The storedProcedure will be like this:
StoredProcedure:

ALTER PROCEDURE getMenuItems

AS

BEGIN
SELECT * FROM MenuContents
END
RETURN



Create Menu:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MenuControl.aspx.cs" Inherits="WebApplication1.MenuControl" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">.subMenu
{
BORDER-RIGHT: black thin solid;
BORDER-TOP: black thin solid;
BORDER-LEFT: black thin solid;
BORDER-BOTTOM: black thin solid
}
.Static
{
BORDER-RIGHT: #336699 1px solid;
BORDER-TOP: #336699 1px solid;
BORDER-LEFT: #336699 1px solid;
BORDER-BOTTOM: #336699 1px solid;
BACKGROUND-COLOR: Maroon;
TEXT-DECORATION: none
}
.Hoover {
BORDER-RIGHT: #336699 1px solid;
BORDER-TOP: #336699 1px solid;
BORDER-LEFT: #336699 1px solid;
BORDER-BOTTOM: #336699 1px solid;
BACKGROUND-COLOR: #5286bb;
TEXT-DECORATION: none
}
.Style1 {
FONT-SIZE: 12px;
COLOR: #ffffff;
FONT-FAMILY: sans-serif, Tahoma, Verdana, Geneva, Arial, Helvetica;
TEXT-DECORATION: none
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" Font-Bold="True" Font-Names="Times New Roman" Height="46px" Width="492px" Font-Size="Medium" ForeColor="White">
<StaticHoverStyle CssClass="Hoover" />
<DynamicHoverStyle CssClass="Hoover" />
<StaticMenuStyle CssClass="Static" />
<DynamicMenuStyle CssClass="Static" />
<DynamicMenuItemStyle CssClass="Static" />
</asp:Menu>
</form>
</body>
</html>


In Code behind get the data from database
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace WebApplication1
{
public partial class MenuControl : System.Web.UI.Page
{
protected string connectionstr = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection(connectionstr);
DataSet ds = new DataSet();
try
{
SqlDataAdapter myadapter = new SqlDataAdapter("getMenuItems", con);
myadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
myadapter.Fill(ds);
}
catch (SqlException ex)
{
throw ex;
}
finally
{
con.Close();
}

DataTable dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
string ParentId = dr["ParentId"].ToString();
string path = dr["path"].ToString();
string Description = dr["MenuItemDesc"].ToString();
MenuItem mchild = new MenuItem(Description, ParentId);

if (ParentId == path)
{
Menu1.Items.Add(mchild);
}
else
{
Menu1.FindItem(path).ChildItems.Add(mchild);
}


}

}

}
}