Adding a datarow and setting the parent

  • Thread starter Thread starter Ben Bloom
  • Start date Start date
B

Ben Bloom

I keep getting

System.SystemException {"ForeignKeyConstraint
SecuritySecurity_Cross_Reference requires the child key values
(0c6cbf28-790f-494c-b05c-64e70521d18600) to exist in the parent table."

when I try to insert a row in a child table. It happens when I create
the row, which has me a little confused. Here's my code:

....

DataRow drSec = ds.Tables["Security"].NewRow();
drSec["Security_ID"] = System.Guid.NewGuid() + i.ToString();
//Set a bunch of values in the table
ds.Tables["Security"].Rows.Add(drSec);

DataRow dr;
dr = ds.Tables["BlockTrade"].NewRow(); <-- error here.
dr.SetParentRow(drSec);
dr["Security_Id"] = ds.Tables["Security"].Rows[0]["Security_Id"];

....


What am I doing wrong?

Thanks.
 
Ben,

It would appear that you have a relationship that you are not
satisfying. Can you post the schema of the dataset, so that we can try it
ourselves? The schema should reveal the relationships between the tables.
 
Nicholas said:
Ben,

It would appear that you have a relationship that you are not
satisfying. Can you post the schema of the dataset, so that we can try it
ourselves? The schema should reveal the relationships between the tables.


Sure. Here's the XSD file.

My understanding of what I created is that I've got a Security table
which relates to a Security_Cross_Reference table based on Security_Id.
I've got a Blocktrade table that relates to the Security table through
Security_Id as well.

Thanks.
 
Back
Top