views:

2509

answers:

3

Consider an SQL Server table containing:

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

i fill a SqlDataSet with the table, and now i want to add the Parent-Child relation to the DataSet:

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

Now this guy uses:

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

But when i do that i get the exception:

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

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

And what is a child and what is a parent anyway? i would have thought the child column is the column that needs pointing to a parent, and parent column is the thing that does the pointing, which would reverse the relation:

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

So which is it? Why does his work? What's with the exception? Why can he create the constraint when he has nulls?

God it's hot in here.

+1  A: 

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

the child has a column that holds the key of the parent (aka the "foreign key" column)

in your example, it probably does not like nulls for the parent id; passing false to avoid enabling constraints will likely make the error go away

Steven A. Lowe
A: 

One reason why his works might be because all the rows in he result set might have a parentID. Child column is the one that refers and parent column is the one that is being refered by the child column. In other words child refers the parent. So in your case Id is the parent column and parentId is the child column.

Satish
A: 

Try reversing the parameters of your relation.

korchev