Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 ASP.NET
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 10-Dec-12 3:12am
Comments
Vani Kulkarni at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
 
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.
  Permalink  
Comments
suma2212 at 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 at 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)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100