J
john
Hi,
I have a question about inserting records into Multiple tables in both a
dataset and database.
Currently I have a dataset created from a database with 3 tables. The
Database (ClientName) generates the ClientID and the other two tables
(Address, Misc) have ClientID as their primary key as well.
This is an MS Access Database.
In my code I have set up a DataRelation Between ClientName and Address Table
with ClientID field set and ClientName and Misc Table with ClientID set as
well.
I've also set up the ClientID field as the primary key in each table in the
dataset.
When I start to insert the records (information is coming from text boxes on
a form), I have an OleDbDataAdapter set up with the insert command for each
table.
When I insert the ClientName information it works fine, but when I go to
insert the Address information I get the following error:
"Foriegn key constraint <constraint name> requires the child key values (65)
to exist in the parent table"
This is the code for the second insert:
dbadapter = Return_Address_OleDbDataAdapter();
myNewRow = AddClientDataSet.Tables["Address"].NewRow();
myNewRow["ClientID"] = ClientID;
myNewRow["Address"] = this.tbStreet.Text;
myNewRow["Town"] = this.tbTown.Text;
myNewRow["PostalCode"] = this.tbPostalCode.Text;
myNewRow["HomeNumber"] = this.tbHomeNumber.Text;
myNewRow["WorkNumber"] = this.tbWorkNumber.Text;
AddClientDataSet.Tables["Address"].Rows.Add(myNewRow);
dbadapter.Update(AddClientDataSet, "Address");
I know it's saying that the ClientID (65) needs to be in the parent table
(ClientName) but it is from my first insert statement and I'm passing the
ClientID generated by the ClientName table to the Address table as well.
This has something to do with DataRelations and I'm not sure what I need to
do to correct this.
Any help would be appreciated.
Thanks.
John
I have a question about inserting records into Multiple tables in both a
dataset and database.
Currently I have a dataset created from a database with 3 tables. The
Database (ClientName) generates the ClientID and the other two tables
(Address, Misc) have ClientID as their primary key as well.
This is an MS Access Database.
In my code I have set up a DataRelation Between ClientName and Address Table
with ClientID field set and ClientName and Misc Table with ClientID set as
well.
I've also set up the ClientID field as the primary key in each table in the
dataset.
When I start to insert the records (information is coming from text boxes on
a form), I have an OleDbDataAdapter set up with the insert command for each
table.
When I insert the ClientName information it works fine, but when I go to
insert the Address information I get the following error:
"Foriegn key constraint <constraint name> requires the child key values (65)
to exist in the parent table"
This is the code for the second insert:
dbadapter = Return_Address_OleDbDataAdapter();
myNewRow = AddClientDataSet.Tables["Address"].NewRow();
myNewRow["ClientID"] = ClientID;
myNewRow["Address"] = this.tbStreet.Text;
myNewRow["Town"] = this.tbTown.Text;
myNewRow["PostalCode"] = this.tbPostalCode.Text;
myNewRow["HomeNumber"] = this.tbHomeNumber.Text;
myNewRow["WorkNumber"] = this.tbWorkNumber.Text;
AddClientDataSet.Tables["Address"].Rows.Add(myNewRow);
dbadapter.Update(AddClientDataSet, "Address");
I know it's saying that the ClientID (65) needs to be in the parent table
(ClientName) but it is from my first insert statement and I'm passing the
ClientID generated by the ClientName table to the Address table as well.
This has something to do with DataRelations and I'm not sure what I need to
do to correct this.
Any help would be appreciated.
Thanks.
John