G
Guest
Ive been struggling for hours on a very simple problem. How can I use the
VS2005 Dataset Designer generated strongly type dataset to insert many rows
into 2 two table linked together by a simple Parent/Child relationship?
I am using SQL server 2000.
The "Prime" table as an ID primary key, identity.
The "Secondary" table as a FK on Prime PK.
Check this code, it should insert 5 rows into each table:
for (int i = 0; i < 5; i++)
{
DataSet.PrimeRow row = dataSet1.Prime.NewPrimeRow();
row.Data = "data 1";
dataSet1.Prime.AddPrimeRow(row);
DataSet.SecondaryRow secrow =
dataSet1.Secondary.NewSecondaryRow();
secrow.Data2 = "data 2";
secrow.PrimeRow = row;
dataSet1.Secondary.AddSecondaryRow(secrow);
}
DataSetTableAdapters.PrimeTableAdapter adapter = new
TestDataSet.DataSetTableAdapters.PrimeTableAdapter();
adapter.Update(dataSet1.Prime.GetChanges() as
DataSet.PrimeDataTable);
DataSetTableAdapters.SecondaryTableAdapter sec = new
TestDataSet.DataSetTableAdapters.SecondaryTableAdapter();
sec.Update(dataSet1.Secondary.GetChanges() as
DataSet.SecondaryDataTable);
When i run this, i receive a SqlException. {"INSERT statement conflicted
with COLUMN FOREIGN KEY constraint 'FK_Secondary_Prime'. The conflict
occurred in database 'JunkTest', table 'Prime', column 'Id'.\r\nThe statement
has been terminated."}
The exception is thrown because the parameter passed for the foreign key is
null. Since the commnad invocation code is generated by VS2005 how am i
supposed to pass that parameter back correctly.
I am lost. I have seen countless of manual workarounds which will take me
days to code.
Please help
VS2005 Dataset Designer generated strongly type dataset to insert many rows
into 2 two table linked together by a simple Parent/Child relationship?
I am using SQL server 2000.
The "Prime" table as an ID primary key, identity.
The "Secondary" table as a FK on Prime PK.
Check this code, it should insert 5 rows into each table:
for (int i = 0; i < 5; i++)
{
DataSet.PrimeRow row = dataSet1.Prime.NewPrimeRow();
row.Data = "data 1";
dataSet1.Prime.AddPrimeRow(row);
DataSet.SecondaryRow secrow =
dataSet1.Secondary.NewSecondaryRow();
secrow.Data2 = "data 2";
secrow.PrimeRow = row;
dataSet1.Secondary.AddSecondaryRow(secrow);
}
DataSetTableAdapters.PrimeTableAdapter adapter = new
TestDataSet.DataSetTableAdapters.PrimeTableAdapter();
adapter.Update(dataSet1.Prime.GetChanges() as
DataSet.PrimeDataTable);
DataSetTableAdapters.SecondaryTableAdapter sec = new
TestDataSet.DataSetTableAdapters.SecondaryTableAdapter();
sec.Update(dataSet1.Secondary.GetChanges() as
DataSet.SecondaryDataTable);
When i run this, i receive a SqlException. {"INSERT statement conflicted
with COLUMN FOREIGN KEY constraint 'FK_Secondary_Prime'. The conflict
occurred in database 'JunkTest', table 'Prime', column 'Id'.\r\nThe statement
has been terminated."}
The exception is thrown because the parameter passed for the foreign key is
null. Since the commnad invocation code is generated by VS2005 how am i
supposed to pass that parameter back correctly.
I am lost. I have seen countless of manual workarounds which will take me
days to code.
Please help