Search code examples
c#strongly-typed-dataset

Populating related datasets from a table adapter


I'm using table adapters and datasets in .NET (version 2.0).

I have two tables like this:

Table1
------
...
TypeId

Table2
------
Id
Name

where Table1.TypeId is linked to Table2.Id.

I've strongly generated these types by using the wizard so now I can do things like this:

Table1Adapter adapter = new Table1Adapter();
Table1DataSet data = adapter.GetData();

foreach(Table1Row row in data) { ... }
// Can now iterate through and display the data

This all works fine. But now I also want the data from Table2. I have noticed that in row there is generated field Table2Row which seems ideal but it is set to null. How do I populate this dataset properly?


Solution

  • Each DataTable in Typed-Dataset has its own TableAdapter. So you'll have to the same step for each DataTable in the dataset.

    There does not exists any api that lets you do this auto-fill of the entire typed-dataset or no such code is generated within typed-dataset that supports this. It is also difficult to do this because TableAdapters do not have a common base-class that can let you do this.

    If you really need to do this, you'll have to maintain a collection of DataTable type-names and TableAdapter type-names and iterate over the collection to perform the dataset fill.

    So I recommend to fill dataset for each table in 'hard-code' manner.