Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi !

I want to Bind a Menu like a DropDoenList from DataBase.It does not support.
<asp:Menu ID="Menu1" runat="server"></asp:Menu>

C#
Menu1.DataSource = dt(DataTable);
                Menu1.DataBind();


Please correct the Code.
Posted
Comments
LebneizTech 15-May-12 6:46am    
Sir I am using C# & code is in VB. Please change it if possible. Also I have change a part but not complete.

void BindMenu()
{
string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter dadCategories = new SqlDataAdapter("SELECT CatId,CatName FROM Category order by CatName", con);
SqlDataAdapter dadSubCat = new SqlDataAdapter("SELECT SubCatId,CatId,SubCatName FROM SubCategory order by SubCatName", con);
DataSet dsCat = new DataSet();
con.Open();
dadCategories.Fill(dsCat, "Category");
dadSubCat.Fill(dsCat, "SubCategory");
//End Using

//Add a DataRelation
//dsCat.Relations.Add("Children", dsCat.Tables("Category")("CatId"), dsCat.Tables("SubCategory")("CatId"));
//dsCat.Relations.Add(dsCat.Tables("Category").("CatId"), dsCat.Tables("SubCategory")("CatId"));
//dsCat.Relations.Add();
// ' Add the Category nodes
// Dim count As Integer = 0
// 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)

// ' Get matching Sub Category
// 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
//End Sub
}
preet88 16-May-12 0:17am    
void BindMenu(){
String connectionString = WebConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
SqlConnection con= new SqlConnection(connectionString);
SqlDataAdapter dadCategories = new SqlDataAdapter("SELECT CatId,CatName FROM Category order by CatName", con);
SqlDataAdapter dadSubCat= new SqlDataAdapter("SELECT SubCatId,CatId,SubCatName FROM SubCategory order by SubCatName", con);

//' Add the DataTables to the DataSet
DataSet dsCat =new DataSet();

con.Open();
dadCategories.Fill(dsCat, "Category");
dadSubCat.Fill(dsCat, "SubCategory");


//' Add a DataRelation
dsCat.Relations.Add("Children", dsCat.Tables["Category"].Columns["CatId"], dsCat.Tables["SubCategory"].Columns["CatId"]);
//' Add the Category nodes
int count = 0;
foreach( DataRow categoryRow in dsCat.Tables["Category"].Rows)
{

var mNode = new MenuItem();//here you can check for overloaded menuitem constructors which suits you best i'm takin default constructor
Menu1.Items.Add(mNode);

//' Get matching Sub Category
DataRow []subCatRows = categoryRow.GetChildRows("Children");
foreach(DataRow row in subCatRows)
{
String subCatName= row["SubCatName"].ToString();
var subCatItems =new MenuItem(); // again here also look for best overloaded constructor
Menu1.Items[count].ChildItems.Add(subCatItems);
}
count = count + 1;
}
}
i suppose this will help to sort your problem...
thanks
LebneizTech 16-May-12 6:05am    
Thank You Sir,
But it is working after some changes.It is ...
void BindMenu()
{
string constr = ConfigurationManager.AppSettings["CS"];
//String connectionString = WebConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter dadCategories = new SqlDataAdapter("SELECT CatCode,CatName FROM Category order by CatName", con);
SqlDataAdapter dadSubCat= new SqlDataAdapter("SELECT SCatCode,CatCode,SCatName FROM SubCategory order by SCatName", con);
//' Add the DataTables to the DataSet
DataSet dsCat =new DataSet();
con.Open();
dadCategories.Fill(dsCat, "Category");
dadSubCat.Fill(dsCat, "SubCategory");
//' Add a DataRelation
dsCat.Relations.Add("Children", dsCat.Tables["Category"].Columns["CatCode"],
dsCat.Tables["SubCategory"].Columns["CatCode"]);
//' Add the Category nodes
int count = 0;
foreach( DataRow categoryRow in dsCat.Tables["Category"].Rows)
{
MenuItem mNode = new MenuItem(dsCat.Tables["Category"].Rows[count][1].ToString());
//here you can check for overloaded menuitem constructors which suits you best i'm takin default constructor
//mNode.Text = "AA";
Menu1.Items.Add(mNode);

//' Get matching Sub Category
DataRow []subCatRows = categoryRow.GetChildRows("Children");
foreach(DataRow row in subCatRows)
{
String subCatName = row["SCatName"].ToString();
MenuItem subCatItems = new MenuItem(subCatName);
//MenuItem subCatItems = new MenuItem(subCatRows.Tables["Category"].Rows[count][1].ToString());
// again here also look for best overloaded constructor
Menu1.Items[count].ChildItems.Add(subCatItems);
}
count = count + 1;
}
}
preet88 16-May-12 6:09am    
my pleasure feel free to ask queries
thanks for accepting solution.

i hope that following link may help you to solve your problem

Binding Menu Control With Database[^]


Here is a similar post hope you'll also get an idea from this.

how to bind menu with sqlserver database using asp.net[^]

Thanks
 
Share this answer
 
Comments
LebneizTech 15-May-12 6:27am    
Sir I am using C# & code is in VB. Please change it if possible. Also I have change a part but not complete.

string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter dadCategories = new SqlDataAdapter("SELECT CatId,CatName FROM Category order by CatName", con);
SqlDataAdapter dadSubCat = new SqlDataAdapter("SELECT SubCatId,CatId,SubCatName FROM SubCategory order by SubCatName", con);
DataSet dsCat = new DataSet();
con.Open();
dadCategories.Fill(dsCat, "Category");
dadSubCat.Fill(dsCat, "SubCategory");
//End Using

//Add a DataRelation
dsCat.Relations.Add("Children", dsCat.Tables("Category")("CatId"), dsCat.Tables("SubCategory")("CatId"));
// ' Add the Category nodes
// Dim count As Integer = 0
// 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)

// ' Get matching Sub Category
// 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
//End Sub
preet88 16-May-12 0:18am    
void BindMenu(){
String connectionString = WebConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
SqlConnection con= new SqlConnection(connectionString);
SqlDataAdapter dadCategories = new SqlDataAdapter("SELECT CatId,CatName FROM Category order by CatName", con);
SqlDataAdapter dadSubCat= new SqlDataAdapter("SELECT SubCatId,CatId,SubCatName FROM SubCategory order by SubCatName", con);

//' Add the DataTables to the DataSet
DataSet dsCat =new DataSet();

con.Open();
dadCategories.Fill(dsCat, "Category");
dadSubCat.Fill(dsCat, "SubCategory");


//' Add a DataRelation
dsCat.Relations.Add("Children", dsCat.Tables["Category"].Columns["CatId"], dsCat.Tables["SubCategory"].Columns["CatId"]);
//' Add the Category nodes
int count = 0;
foreach( DataRow categoryRow in dsCat.Tables["Category"].Rows)
{

var mNode = new MenuItem();//here you can check for overloaded menuitem constructors which suits you best i'm takin default constructor
Menu1.Items.Add(mNode);

//' Get matching Sub Category
DataRow []subCatRows = categoryRow.GetChildRows("Children");
foreach(DataRow row in subCatRows)
{
String subCatName= row["SubCatName"].ToString();
var subCatItems =new MenuItem(); // again here also look for best overloaded constructor
Menu1.Items[count].ChildItems.Add(subCatItems);
}
count = count + 1;
}
}

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900