This is a migrated thread and some comments may be shown as answers.

TreeView Bind Error:"This constraint cannot be enabled as not all values have corresponding parent values."

4 Answers 476 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Yngwie
Top achievements
Rank 1
Yngwie asked on 12 May 2009, 08:57 AM
Hi everyone.
I've a problem with my TreeView controls. the problem is;

When I try to bind data from DB to RadTreeView, it gives this error : "This constraint cannot be enabled as not all values have corresponding parent values."

C# Code :

private void ForTree()
{
line 1:String Query = "SELECT * FROM dbo.FESTIVAL WHERE flag=0";
line 2:myDataTable3 = dtClass.GetQuery(Query);
        RadTreeView3.DataSource = myDataTable3;
        RadTreeView3.DataFieldID = "fest_id";
        RadTreeView3.DataValueField = "fest_id";
        RadTreeView3.DataFieldParentID = "cat_id";
        RadTreeView3.DataTextField = "title";
line 8:RadTreeView3.DataBind();   // This is the line where error occurs.


line 2 is connects to DB and fill the datatable from another class.

when I write the query  :"SELECT * FROM dbo.FESTIVAL", there was no problem but when I add it "WHERE flag=0" clause to filter some data at "line 1" it gives me error. And interesting point is When I change the Flag value to "1" (flag=1) it works fine !!,  it makes me crazy.

I search all the topics in "Telerik Forums" and there were some problems as mine, but my table is not spereated and all the Parent_id's values are "NULL"

so what can I do at this situation?

Database DataTable:

cat_id is a child_id. title column has some words will placed on node.

dbo.Festival
Fest_id   Cat_id  flag   title
1             NULL      0   
2             NULL      1
3             NULL      0    
4             NULL      1
5             NULL      0
6             NULL      1
7             NULL      0
8             NULL      1
9             NULL      0
10           NULL     1
11           NULL     0
12           NULL     1
13           NULL     0
14           NULL     1
15           NULL     0    
16           NULL     1
17           NULL     0    
18           NULL     1
19           NULL     0
20           NULL     1
21           1          0
22           1          1
23           1          0
24           1          1
25           2          0    
26           2         1
27           2         0
28           2         1
29           3         0
30           3         1
31          4          0
32           4          1
33           7          0    
34           7          1
35            8         0
36            8         1
37            9          0
38            9          1
39          10          0

...it goes until 180 records.

4 Answers, 1 is accepted

Sort by
0
Yngwie
Top achievements
Rank 1
answered on 12 May 2009, 12:44 PM
Is it my fault or RadTreeView's bug?
0
Veselin Vasilev
Telerik team
answered on 12 May 2009, 02:41 PM
Hi Yngwie ,

When you execute the select query:

SELECT * FROM dbo.FESTIVAL WHERE flag=0

there are some records that does not have parents, here is an example:

Fest_id   Cat_id  flag   title
1             NULL      0   
3             NULL      0   

...
25           2          0

Note that the record with fest_id = 25 has a parent (cat_id) = 2, but this record is not here because its flag is not equal to 0.

I hope this explains everything.

All the best,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Yngwie
Top achievements
Rank 1
answered on 12 May 2009, 03:06 PM
Yes you're right ;)
That was awesome, Thanks ;)
0
Vishnu
Top achievements
Rank 2
answered on 14 Mar 2015, 11:54 AM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Grid.Exam
{
    public partial class StudentDetail : System.Web.UI.Page
    {
    
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadDropDownTree1.DataFieldID = "ID";
                RadDropDownTree1.DataFieldParentID = "ParentID";
                RadDropDownTree1.DataValueField = "Value";
                RadDropDownTree1.DataTextField = "Text";
                RadDropDownTree1.DataSource = GetData();
                RadDropDownTree1.DataBind();

            }
            }
        public DataTable GetData()
        {



            string str = "select ParentID=null,Stateid as Value,StateName as Text from State ";
            str += "Union all ";
            str += "select StateId as ParentID,DistrictId as Value,DistrictName as Text from District ";
            str += "where StateId in(Select StateId from District) ";
            str += "Union all ";
            str += "select DistrictID as ParentID,LocationId as Value,LocationName as Text from ";

            str += "Location where districtId in(select DistrictId from Location)";
            SqlCommand cmd = new SqlCommand(str, con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);


            DataTable table = new DataTable();
            table.Columns.Add("ID");
            table.Columns.Add("ParentID");
            table.Columns.Add("Value");
            table.Columns.Add("Text");
            if (ds.Tables[0].Rows.Count != 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = table.NewRow();
                    //int j = i + 1;
                    //dr["ID"] = j;
                    string st = "";
                    //if (st == ds.Tables[0].Rows[i]["ParentID"].ToString())
                    //{
                    //    dr["ParentID"] = "null";
                    //}
                    //else
                    //{
                    //    dr["ParentID"] = ds.Tables[0].Rows[i]["ParentID"].ToString();
                    //}
                    if (st == ds.Tables[0].Rows[i]["ParentID"].ToString())
                    {
                        dr["ParentID"] = "";
                    }
                    else
                    {
                        //{
                        //    dr["ParentID"] = ds.Tables[0].Rows[i]["ParentID"].ToString();
                        //}

                        dr["ParentID"] = ds.Tables[0].Rows[i]["ParentID"].ToString();

                    }
                    dr["ID"] = ds.Tables[0].Rows[i]["Value"].ToString();

                    dr["Value"] = ds.Tables[0].Rows[i]["Value"].ToString();
                    dr["Text"] = ds.Tables[0].Rows[i]["Text"].ToString();
                    table.Rows.Add(dr);

                }
            }

                //string id = "";
                ////int lastid = 0;
                //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                //{
                //    DataRow dr = table.NewRow();
                //    if (id == ds.Tables[0].Rows[i]["ParentID"].ToString())
                //    {
                //        dr["ID"] = ds.Tables[0].Rows[i]["ID"].ToString();
                //        //lastid = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"].ToString());
                //        id = ds.Tables[0].Rows[i]["ParentID"].ToString();
                //        dr["ParentID"] = null;
                //    }
                //    else
                //    {
                //        dr["ID"] = ds.Tables[0].Rows[i]["Value"].ToString();
                //        dr["ParentID"] = ds.Tables[0].Rows[i]["ParentID"].ToString();
                //        //lastid = lastid + 1;
                //        //dr["ID"] = lastid + 1;
                //        //lastid = lastid + 1;
                //    }

                //    dr["Value"] = ds.Tables[0].Rows[i]["Value"].ToString();
                //    dr["Text"] = ds.Tables[0].Rows[i]["Text"].ToString();
                //    table.Rows.Add(dr);



            //DataTable table = new DataTable();
            //table.Columns.Add("ID");
            //table.Columns.Add("ParentID");
            //table.Columns.Add("Value");
            //table.Columns.Add("Text");
            //table.Rows.Add(new String[] { "22", null, "22", "Tamilnadu" });
            //table.Rows.Add(new String[] { "23", null, "23", "Kerala" });
            //table.Rows.Add(new String[] { "24", null, "24", "Karnataka" });
            //table.Rows.Add(new String[] { "11", "22", "11", "Kanyakumari" });
            //table.Rows.Add(new String[] { "12", "22", "12", "Chennai" });
            //table.Rows.Add(new String[] { "13", "22", "13", "Nellai" });
            //table.Rows.Add(new String[] { "14", "23", "14", "TVM" });


            //table.Rows.Add(new String[] { "15", "24", "15", "Bangalore" });
            //table.Rows.Add(new String[] { "16", "11", "16", "Nagercoil" });
            //table.Rows.Add(new String[] { "17", "12", "17", "Pallavaram" });
            //table.Rows.Add(new String[] { "18", "13", "18", "Nellai" });
            //table.Rows.Add(new String[] { "19", "14", "19", "trivandrum" });
            //table.Rows.Add(new String[] { "20", "15", "20", "Mysore" });

            return table;
        
        }       
        }
}
    




Error:
____Server Error in '/' Application.This constraint cannot be enabled as not all values have corresponding parent values.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.

Source Error: 

Line 27: RadDropDownTree1.DataTextField = "Text";
Line 28: RadDropDownTree1.DataSource = GetData();
Line 29: RadDropDownTree1.DataBind();
Line 30:
Line 31: }
Source File: E:\Vishnu\TElerik\Samples\Grid\Grid\Grid\Exam\StudentDetail.aspx.cs    Line: 29 

Stack Trace: 

[ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.]
System.Data.ConstraintCollection.AddForeignKeyConstraint(ForeignKeyConstraint constraint) +1825731
System.Data.ConstraintCollection.Add(Constraint constraint, Boolean addUniqueWhenAddingForeign) +413
System.Data.DataSetRelationCollection.AddCore(DataRelation relation) +794
System.Data.DataRelationCollection.Add(DataRelation relation) +153
System.Data.DataRelationCollection.Add(DataColumn parentColumn, DataColumn childColumn) +45
Telerik.Web.UI.ControlDataBinder.BindToDataTable(DataTable table, String dataFieldID, String dataFieldParentID) +97
Telerik.Web.UI.HierarchicalControlItemContainer.PerformDataBinding(IEnumerable data) +425
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
Telerik.Web.UI.DropDownTree.EmbeddedTreeAdapter.DataBind() +101
Telerik.Web.UI.RadDropDownTree.DataBind() +46
Grid.Exam.StudentDetail.Page_Load(Object sender, EventArgs e) in E:\Vishnu\TElerik\Samples\Grid\Grid\Grid\Exam\StudentDetail.aspx.cs:29
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Version Information: Microsoft .NET Framework Version:2.0.50727.6421; ASP.NET Version:2.0.50727.6412





Tags
TreeView
Asked by
Yngwie
Top achievements
Rank 1
Answers by
Yngwie
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Vishnu
Top achievements
Rank 2
Share this question
or