Click here to Skip to main content
13,248,562 members (74,933 online)
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 28 Jun 2008

Binding Menu Control With Database

, 28 Jun 2008
Rate this:
Please Sign up or sign in to vote.
Bind the menu control with list of Category and Sub Categories
Figure 1


Figure 2



In this article, I will show how to bind the menu control with the database. Suppose you have a situation to display the different Category and its Sub-Categories. When we click the Category/Sub Category (See Figure 1) to view the detail information about Category/Sub Category (See Figure 2), then this bit of code will be useful. This code prevents a post back to the server while selecting Sub Category.

Using the Code

Database Detail

Table Name - Category


Table Name - SubCategory


There are two tables (Category, SubCategory) which are related to each other.
Here’s the method called BindMenu which binds the menu control during the page load.

  1. Create the connection string to establish connection with the database:
    Dim connectionString As String =
    Dim con As New SqlConnection(connectionString)
  2. Create two DataAdapters for both the tables:
    Dim dadCategories As New SqlDataAdapter("SELECT CatId,CatName FROM Category
    order by CatName", con)
    Dim dadSubCat As New SqlDataAdapter("SELECT SubCatId,CatId,SubCatName FROM
    SubCategory order by SubCatName", con)
  3. Create a DataSet and fill the dataset with both the tables:
    Dim dsCat As New DataSet()
    Using con
    dadCategories.Fill(dsCat, "Category")
    dadSubCat.Fill(dsCat, "SubCategory")
    End Using
  4. Relate both the tables and name the relation as Children:
    dsCat.Relations.Add("Children", dsCat.Tables("Category").Columns("CatId"),
  5. Loop through each category of data and its related Sub category of data. At each loop, we create menu items and associate with the menu control.
    For Each categoryRow As DataRow In dsCat.Tables("Category").Rows
    Dim mNode As New MenuItem(CType(categoryRow("CatName"), String), "", "",
    "~/DetailView.aspx?CatID=" + CType(categoryRow("CatId"), String), "_parent")
    Dim subCatRows() As DataRow = categoryRow.GetChildRows("Children")
    For Each row As DataRow In subCatRows
    Dim subCatName As String = CType(row("SubCatName"), String)
    Dim subCatItems As New MenuItem(subCatName, "", "",
    "~/DetailView.aspx?CatID=" + CType(row("CatId"), String) + "&SubCatID=" +
    CType(row("SubCatId"), String), "_parent")
    count = count + 1


  • 28th June, 2009: Initial post


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


About the Author

Ravi Selvaraj
Web Developer Syntax Solutions Inc.
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionAdding SubMenu to SubMenu Pin
bhagyap11-Jan-12 1:32
memberbhagyap11-Jan-12 1:32 
General5-level menu Pin
amit121212419-Dec-10 23:52
memberamit121212419-Dec-10 23:52 
QuestionHow find the CatID Pin
Ajeet kumar verma25-Feb-10 2:15
memberAjeet kumar verma25-Feb-10 2:15 
QuestionBindMenu Pin
Member 46794646-May-09 6:44
memberMember 46794646-May-09 6:44 
AnswerRe: BindMenu Pin
Ajeet kumar verma25-Feb-10 2:19
memberAjeet kumar verma25-Feb-10 2:19 
Generalme 2 Pin
anhduongxanh2-Apr-09 17:54
memberanhduongxanh2-Apr-09 17:54 
Questiongoing deeper Pin
ykorotia29-Nov-08 23:18
memberykorotia29-Nov-08 23:18 
GeneralLayout Pin
Velvet Saunders22-Oct-08 6:27
memberVelvet Saunders22-Oct-08 6:27 
GeneralRe: Layout Pin
Ravi Selvaraj26-Oct-08 0:17
memberRavi Selvaraj26-Oct-08 0:17 
QuestionC# Code. Pin
nirajan8-Jul-08 7:25
membernirajan8-Jul-08 7:25 
AnswerRe: C# Code. Pin
Ravi Selvaraj8-Jul-08 8:11
memberRavi Selvaraj8-Jul-08 8:11 
GeneralRe: C# Code. Pin
nirajan14-Jul-08 15:39
membernirajan14-Jul-08 15:39 
GeneralRe: C# Code. Pin
Ravi Selvaraj14-Jul-08 17:37
memberRavi Selvaraj14-Jul-08 17:37 
GeneralRe: C# Code. Pin
suma221210-Dec-12 20:12
membersuma221210-Dec-12 20:12 
GeneralRe: C# Code. Pin
deepikadurge7-Apr-09 22:14
memberdeepikadurge7-Apr-09 22:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 28 Jun 2008
Article Copyright 2008 by Ravi Selvaraj
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid