ADO.NET: adding a DataRelation to a DataSet; which is parent and child?

Consider a SQL Server table containing:

ID ParentID Text === ========= ============= 1 (null) Product 2 (null) Applications 3 1 Background 4 1 Details 5 2 Mobile 

i populate the SqlDataSet table with the table, and now I want to add the parent-child relationship to the DataSet:

 public DataRelation( string relationName, DataColumn parentColumn, DataColumn childColumn, bool createConstraints ) 

Now this guy is using:

 DataRelation relation = newDataRelation("ParentChild", ds.Tables[0].Columns["ID"], //parentColumn ds.Tables[0].Columns["ParentID"] //childColumn, true //createConstraints ); 

But when I do this, I get an exception:

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

People suggested passing false for createConstraints ; but then why does he work for him?

What is a child and what is a parent? I would think that the child column is the column that should point to the parent, and the parent column is what the pointer does, which will lead to the opposite relation:

 DataRelation relation = newDataRelation("ParentChild", ds.Tables[0].Columns["ParentID"], //parentColumn ds.Tables[0].Columns["ID"], //childColumn true //createConstraints ); 

So what is this? Why is his work? What except? Why can he create a constraint when he has values?

God, it's hot here.

+4
source share
5 answers

There was nothing terrible answer ...

The problem is that the example you read is under the label "Step 3 - Extract the data and create the Nested Relationship ".

If you want to add a relationship between two SAME TABLE columns (nested), then you must set the Nested variable to true (before adding), as shown on its website.

 relation.Nested = true; ds.Relations.Add(relation); 
+3
source

parent-child is also called a one-to-many relationship, where "one" is the parent and "many" is the child.

the child has a column that contains the parent key (aka the foreign key column)

in your example, this probably doesn't look like null for the parent id; passing false to avoid inclusion restrictions is likely to result in an error.

+1
source

One of the reasons why his work may be related to the fact that all rows in the result set may have parentID. The parent column is the one that refers, and the parent column is the one that the child column refers to. In other words, the child refers to the parent. So in your case, Id is the parent column, and parentId is the child column.

0
source

Try changing your relationship settings.

0
source

All Articles