Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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....
Posted
Comments
Vani Kulkarni 10-Dec-12 8:19am    
Verify the data in both the tables. Check all the rows in Sub Category table has CatId. If there any one row missing CatId then this error occurs.

1 solution

Verify the data in both the tables. Check all the rows in Sub Category table has CatId. If there any one row missing CatId then this error occurs.
The solutions is to set constraint in DataRelation class to false

C#
dst.Relations.Add("Children",
dst.Tables["tbl_productcatagory"].Columns["catid"],
dst.Tables["tbl_productsubcatagory"].Columns["catid"], false);


By default, when you create a relationship, it enforces foreign key constraints, by setting to false, you are telling it that you dont want to enforce the relationship.
 
Share this answer
 
Comments
suma2212 11-Dec-12 0:04am    
I am getting the error from this code

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;
}


in this without productid=1 it is not showing any error when i use product id i am getting error from this code. but i need to provide product id otherwise it displays all product catagories

dst.Relations.Add("Children",
dst.Tables["tbl_productcatagory"].Columns["catid"],
dst.Tables["tbl_productsubcatagory"].Columns["catid"]);
suma2212 11-Dec-12 0:20am    
Hi Vani Kulkarni,

I tried this code
dst.Relations.Add("Children",
dst.Tables["tbl_productcatagory"].Columns["catid"],
dst.Tables["tbl_productsubcatagory"].Columns["catid"], false);

But i am not getting the child values...

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