Help needed using Relations on a Typed DataSet

J

J055

Hi

I have a relation and foreign key constraint set up on two DataTables within
a typed DataSet. I want to return the child rows from one of the tables,
e.g.

MyDataSet dataSet = primaryTbl.DataSet;

(MyDataSet.MyPrimaryRow[])primaryRow.GetChildRows(dataSet.Relations[0]);

I've tried a few different ways but I gets errors like null exceptions
because the dataSet is null.

What is the correct way of use relations when they have been setup in the
typed DataSet? Also I'm doing this to try and get a performance improvement
to replace this approach:

(MyDataSet.foreignRow[])foreignTbl.Select("ID = " + primaryRow.ID);

Can you tell me if this will help or am I barking up the wrong tree here?
Have I got to create DataView objects instead because you can't have indexes
on non primary key columns in a DataTable, etc, etc?

Hope this all makes sense.

Confused
Andrew
 
R

RobinS

The GetChildRows is the right thing, but I don't understand what you mean
by the dataset is null? There are no children?

Here's an example of GetChildRows; this uses the Northwind Database, which
has Customers, and a list of Orders for each customer.

Dim ds as DataSet
Dim tblCustomers as DataTable
Dim tblOrders as DataTable
Dim rel as DataRelation
'create DataSet, then continue
rel = ds.Relations.Add("Customers_Orders", _
tblCustomers.Columns("CustomerID"), _
tblOrders.Columns("CustomerID"))
For each rowCustomer as DataRow in tblCustomers.Rows
Console.WriteLine(rowCustomer("CompanyName"))
For each rowOrder as DataRow in rowCustomer.getChildRows(rel)
Console.WriteLine("Order ID = {0}", rowOrder("OrderID"))
Next
Next

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
J

J055

Hi

Thanks for this. I'm using Typed DataSets so the relation already exists. I
just need to know how to get the relation so I can use it in the
GetChildRows call.

MyDataSet ds;
MyDataSet.DataTable1 dt1;
MyDataSet.DataTable2 dt2;

dt1 = ta1.GetAll(); // this has rows
dt2 = ta2.GetAll(); // this has rows
ds = dt1.DataSet;

dt1 has a relation with dt2, dt1 has the primary key. But ds is null so I
can't get the ds.Relations[0] from the DataRelationCollection to use in the
GetChildRows. There must be a good example out there somewhere but I can't
find one anywhere.

What's the answer?

Thanks
Andrew
 
W

WenYuan Wang

Hi Andrew,

Accoring to your descritpion, I understand you want to get the ChildRows
from the ParentTable with Typed DataSet. Please don't hesitate to correct
me if I have misunderstood anything here. Thanks.

As far as I know, we use the following steps to create a typed dataset and
get ChildRows from the relation. Would you mind trying the method as below
and let me know whether or not this is what you need?

[MyDataSet] ds = new [MyDataSet]()
[MyDataSetTableAdapters].[Table1TableAdapter] t1ta= new
[MyDataSetTableAdapters].[Table1TableAdapter]();
[MyDataSetTableAdapters].[Table2TableAdapter] t2ta= new
[MyDataSetTableAdapters].[Table2TableAdapter]();

t1ta.fill(ds. [DataTable1]);
t2ta.fill(ds. [DataTable2]);

([MyDataSet].[DataTable2] [])DataTable1.GetChildRows(ds.Relations[0]);
//Another way to achieve this if the relation name is "Table1_Table2"
//([MyDataSet].[DataTable2] [])DataTable1.GetChildRows("Table1_Table2");

Hope this helps.
Sincerely,
Wen Yuan
 
J

J055

Hi Wen Yuan

Thanks for this. My TableAdapters don't have Fills only Get methods. Can you
suggest a way to use them with the DataSet?

Thanks again
Andrew
 
R

RobinS

What is the name of the relation? Can't you see if in the DataSet Designer?
Try clicking on it and see if it shows the name of it.

Then you can get the child rows like in the example I posted earlier.

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
W

WenYuan Wang

Hi Andrew,

Base on my experience, there should be a Fill() method in Typed Dataset.
Even though in GetData method, typed dataset still need DataAdapter.Fill()
method to load data from database. Anyway, if you just want to load data by
GetData method, we suggest you can use table.Merge() method.
For example:
TestDataBase20060908DataSet1 ds = new TestDataBase20060908DataSet1();
TestDataBase20060908DataSet1TableAdapters.Table1TableAdapter ta1 = new
TestDataBase20060908DataSet1TableAdapters.Table1TableAdapter();
TestDataBase20060908DataSet1TableAdapters.Table2TableAdapter ta2 = new
TestDataBase20060908DataSet1TableAdapters.Table2TableAdapter();

ds.Table1.Merge(ta1.GetData());
ds.Table2.Merge(ta2.GetData());

ds.Table1.Rows[0].GetChildRows(ds.Relations[0]);

Please try the method as above and let me know whether or not this is what
you need. I'm glad to assist you.

Have a great day!
Wen Yuan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top