Click here to Skip to main content
15,896,118 members

binding menu control with Database in asp.net

suma2212 asked:

Open original thread
Hi i have 3 tables these are products table, catagory table, and sub catagory table. using these 3 tables i need to bind menu control with sql server database. in this when i click product name i have to display the catagoies and sub catagories and i have to display the content of catagories and sub catagories while clicking those sub catagories.

i am using this code

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;
using System.Data.SqlTypes;
using System.IO;

public partial class construction_materials : System.Web.UI.Page
{
string str = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

PopulateMenu();

}
}
DataSet GetMenuData()
{
SqlConnection con = new SqlConnection(str);
SqlDataAdapter dadCats = new SqlDataAdapter("SELECT catid, catname,productid FROM tbl_productcatagory where productid=1", con);
SqlDataAdapter dadProducts = new SqlDataAdapter("SELECT subcatid, catid, subcatagoryname FROM tbl_productsubcatagory ", con);
DataSet dst = new DataSet();
dadCats.Fill(dst, "tbl_productcatagory");
dadProducts.Fill(dst, "tbl_productsubcatagory");
dst.Relations.Add("Children",
dst.Tables["tbl_productcatagory"].Columns["catid"],
dst.Tables["tbl_productsubcatagory"].Columns["catid"]);
return dst;
}


public void PopulateMenu()
{
DataSet dst = GetMenuData();
foreach (DataRow masterRow in dst.Tables["tbl_productcatagory"].Rows)
{
MenuItem masterItem = new MenuItem((string)masterRow["catname"]);
Menu1.Items.Add(masterItem);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
MenuItem childItem = new MenuItem((string)childRow["subcatagoryname"], "", "", ("~/default.aspx?catid=" + (masterRow["catid"].ToString()) + "&subcatid=" + (childRow["subcatid"].ToString())), "_parent");
masterItem.ChildItems.Add(childItem);
}
}

}
}

But i am getting this error.

This constraint cannot be enabled as not all values have corresponding parent values.

can u please help me....
Tags: C# (C# 2.0), ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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