I have few tables in the database with parent table say P is having 1:1 relation with some child tables whereas having 1:M relation with other child tables.
For this I have added DataSet at design time by ‘Add -> New Item -> DataSet -> DataSet1.xsd’
So, I have all the required tables along with their 1:1 or 1:M relations.
I have applied some filter on main table adapter so that only selected records from parent table and their related child records are populated in the dataset. And finally, I want to write this data from DataSet to Xml file.
But, I am getting one issue – the final dataset that I am getting is having all the records from child table. Instead it should have only those child records for which parent table is having related records.
I am using following code –
ParentDataSet parentDataSet = new ParentDataSet();
ParentTableAdapter parentTableAdapter = new ParentTableAdapter();
parentTableAdapter.Fill(parentDataSet.ParentTable, column1Value); // All fine until here as I have applied filter in tableAdapter SQL
ChildTableAdapter childTableAdapter = new ChildTableAdapter();
//returns all rows in the child table -- shouldn't it return only those child records for which parent dataset table is having records??
childTableAdapter.Fill(parentDataSet.ChildTable);
parentDataSet.WriteXml(xmlFilePath);
Please guide what I am missing here?
Thank you!
Have you seen this article? The author says that
Try changing the type of the parent-child relation to a foreign key constraint. Then, check the
SelectCommandproperty of theChildTableAdapter. It is filtering the data the way you expect.