Error when using System.Data.Relations.Add

G

Guest

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);
 
G

geeksgk

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"]);
 
K

Kevin Yu [MSFT]

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."
 
G

Guest

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.
 
G

Guest

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

Thanks!
Diane Droubay
 
K

Kevin Yu [MSFT]

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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top