Click here to Skip to main content
6,306,412 members and growing! (16,648 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Menu Controls     Intermediate License: The Code Project Open License (CPOL)

Binding Menu Control With Database

By Ravi Selvaraj

Bind the menu control with list of Category and Sub Categories
Javascript, CSS, HTML, XHTML, .NET (.NET 2.0), ASP.NET, IIS (IIS 5.1), Visual Studio (VS2005), ADO.NET, WebForms, Dev
Posted:28 Jun 2008
Views:12,671
Bookmarked:34 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
4 votes for this article.
Popularity: 2.11 Rating: 3.50 out of 5

1
1 vote, 25.0%
2

3
2 votes, 50.0%
4
1 vote, 25.0%
5

Download source - 4.83 KB

Figure 1

BindMenu1.jpg
Figure 2

BindMenu2.jpg

Introduction

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 and 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

BindMenu3.jpg

Table Name - SubCategory

BindMenu4.jpg

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 =
WebConfigurationManager.ConnectionStrings("DatabaseConnectionString1").ConnectionString
Dim con As New SqlConnection(connectionString)
         

2) Create two DataAdapter 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
con.Open()
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"),
dsCat.Tables("SubCategory").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")
Menu1.Items.Add(mNode)

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")
Menu1.Items(count).ChildItems.Add(subCatItems)
Next
count = count + 1
Next
         

License

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

About the Author

Ravi Selvaraj


Member

Occupation: Web Developer
Company: Xypnoss Systems Pvt. Ltd.
Location: India India

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
QuestionBindMenu PinmemberMember 46794646:44 6 May '09  
Generalme 2 Pinmemberanhduongxanh17:54 2 Apr '09  
Questiongoing deeper Pinmemberykorotia23:18 29 Nov '08  
GeneralLayout PinmemberVelvet Saunders6:27 22 Oct '08  
GeneralRe: Layout PinmemberRavi Selvaraj0:17 26 Oct '08  
QuestionC# Code. Pinmembernirajan7:25 8 Jul '08  
AnswerRe: C# Code. PinmemberRavi Selvaraj8:11 8 Jul '08  
GeneralRe: C# Code. Pinmembernirajan15:39 14 Jul '08  
GeneralRe: C# Code. PinmemberRavi Selvaraj17:37 14 Jul '08  
GeneralRe: C# Code. Pinmemberdeepikadurge22:14 7 Apr '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Jun 2008
Editor:
Copyright 2008 by Ravi Selvaraj
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project