Using an informix database, I'm retrieving two datasets and copying the data
table from one dataset into the other, so I've got 1 dataset with 2 tables.
This is just background because it might be part of the problem.

After this, I simply want to create a data relation between the two tables,
so I call the constructor for a new datarelation, which appears to work fine.

I get an error when I'm trying to use the Add method of the dataset. The
error is:
"System.ArgumentException: This constraint cannot be enabled as not all
values have corresponding parent values."

Any ideas? Below is the code I'm using.

Thanks!

Diane Droubay
-----------------------------
//copy the budget detail from the detail dataset, then add to the budget
dataset
DataTable dt = dsDetail.Tables[0].Copy();
dsBudget.Tables.Add(dt);

dsBudget.Tables[0].TableName = "Budget";
dsBudget.Tables[1].TableName = "Detail";

DataColumn dcBudgetId;
DataColumn dcBudgetDetailId;

//budget_id is primary key in Budget table and foreign key in Detail table.
dcBudgetId = dsBudget.Tables["Budget"].Columns["budget_id"];
dcBudgetDetailId = dsBudget.Tables["Detail"].Columns["budget_id"];

DataRelation rel = new DataRelation("Budget2Detail", dcBudgetId,
dcBudgetDetailId);

//crashes on the next call.
dsBudget.Relations.Add(rel);

Re: Error when using System.Data.Relations.Add by geeksgk

geeksgk
Wed Feb 16 17:16:21 CST 2005

I have done the same numerous time and it works like a charm....Here is
a part of the code which does it ... hope it gives you some lead

//Prep the deficiency_Id column
DataColumn PrimaryIdcolumn =
NewDataSet.Tables["Regulator"].Columns.Add("regulator_Id",
typeof(Int32));
//Uncomment the following if required
//PrimaryIdcolumn.Unique = true;
//PrimaryIdcolumn.AutoIncrement = true;
//PrimaryIdcolumn.AutoIncrementSeed = -1;
//PrimaryIdcolumn.AutoIncrementStep = -1;

//Prep the regulator_Id column for Name table
DataColumn ForeignIdcolumn =
NewDataSet.Tables["Name"].Columns.Add("regulator_Id", typeof(Int32));
NewDataSet.Relations.Add("RegName",
NewDataSet.Tables["Regulator"].Columns["regulator_Id"],
NewDataSet.Tables["Name"].Columns["regulator_Id"]);


Re: Error when using System.Data.Relations.Add by v-kevy

v-kevy
Wed Feb 16 19:31:09 CST 2005

Hi Diane,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you are getting and exception which
says "the constraint cannot be enabled as not all values have corresponding
parent values" when trying to add a relationship between two tables. If
there is any misunderstanding, please feel free to let me know.

Generally, when we get this kind of exception, it means that some foreign
key value in the child table is not contained in the primary key of the
parent table. Because when you add the DataRelation object to the
collection, a ForeignKeyConstraint object is automatically created in the
child table. So please check if all the data is contained in the parent
table.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


Re: Error when using System.Data.Relations.Add by dianed

dianed
Thu Feb 17 09:33:09 CST 2005

Thanks. I checked this out, but it looks like you're doing pretty much the
exact same thing I'm doing. It's great to know about how to add the
AutoIncrement if I need it though. I'm pretty new to all of this.

"geeksgk@yahoo.com" wrote:

> I have done the same numerous time and it works like a charm....Here is
> a part of the code which does it ... hope it gives you some lead
>
> //Prep the deficiency_Id column
> DataColumn PrimaryIdcolumn =
> NewDataSet.Tables["Regulator"].Columns.Add("regulator_Id",
> typeof(Int32));
> //Uncomment the following if required
> //PrimaryIdcolumn.Unique = true;
> //PrimaryIdcolumn.AutoIncrement = true;
> //PrimaryIdcolumn.AutoIncrementSeed = -1;
> //PrimaryIdcolumn.AutoIncrementStep = -1;
>
> //Prep the regulator_Id column for Name table
> DataColumn ForeignIdcolumn =
> NewDataSet.Tables["Name"].Columns.Add("regulator_Id", typeof(Int32));
> NewDataSet.Relations.Add("RegName",
> NewDataSet.Tables["Regulator"].Columns["regulator_Id"],
> NewDataSet.Tables["Name"].Columns["regulator_Id"]);
>
>

Re: Error when using System.Data.Relations.Add by dianed

dianed
Thu Feb 17 09:35:07 CST 2005

Hi Kevin,
That solved it. I was filtering one table and not the other.

Thanks!
Diane Droubay

"Kevin Yu [MSFT]" wrote:

> Hi Diane,
>
> First of all, I would like to confirm my understanding of your issue. From
> your description, I understand that you are getting and exception which
> says "the constraint cannot be enabled as not all values have corresponding
> parent values" when trying to add a relationship between two tables. If
> there is any misunderstanding, please feel free to let me know.
>
> Generally, when we get this kind of exception, it means that some foreign
> key value in the child table is not contained in the primary key of the
> parent table. Because when you add the DataRelation object to the
> collection, a ForeignKeyConstraint object is automatically created in the
> child table. So please check if all the data is contained in the parent
> table.
>
> HTH.
>
> Kevin Yu
> =======
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
>
>

Re: Error when using System.Data.Relations.Add by v-kevy

v-kevy
Thu Feb 17 23:47:21 CST 2005

You're welcome, Diane.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."