Create a relation between tables in dataset

mcerk

Well-known member
Joined
Nov 1, 2004
Messages
78
Hi I dont know what is wrong here.
I allways got this error message:
An unhandled exception of type System.ArgumentException occurred in system.data.dll

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

Here is my code:
Code:
        OleDbConnection2.ConnectionString = sConnect
        OleDbDataAdapter1.Fill(Me.DsOpisDM1, "opisDM")
        Me.daZahtevanaZnanja.Fill(Me.DsOpisDM1, "x_dodatnaZnanja")


        DsOpisDM1.Relations.Add("relation", DsOpisDM1.Tables("opisDM").Columns("id"), DsOpisDM1.Tables("x_dodatnaZnanja").Columns("id_opisDM"))

if I disable last row (DSOpisDM1.Relations.Add ...) then the program runs perfectly OK. Datasets are filled . . .

What am I doing wrong.


With this relation I want that only specific rows in x_dodatnaZnanja are displayed....


tx

matej
 
If you are creating a relationship between a parent and a child table then all the child entries must have a valid parent. i.e. Every entry found in id_opisDM must have a matching value in id
 
Yes as PlausiblyDamp stated. In some databases, you can have rows taht do not contain the foreign key for the other table. This does not violate any referential integrity rules. But if there is an FG there and it is not null, it must match the primary key of the other table. If this is the case, then when you pull the data over with your dataadpter it will error out. You could modify the dataadpter to say soemthing like "SELECT * FROM x_dodatnaZnanja WHERE opisDM.ID = x_dodatnaZnanja.id_opisDM;" My SQL may be a little off there, but you should be able tog et it from there. This method should prevent your error.

Chester
 
If you have some records that do not have a relationship with the parent then the DataRelationCollection has a createConstraints option, this defaults to true.

I think the following will work if this is the problem.

Code:
DsOpisDM1.Relations.Add("relation", DsOpisDM1.Tables("opisDM").Columns("id"), DsOpisDM1.Tables("x_dodatnaZnanja").Columns("id_opisDM"), false)
 
Back
Top