DataSet.Merge problem

J

J055

Hi

This code does not update the copyTbl rows. I.e. when the copyTbl object is
merged into the dataset actions on the rows of this table are not reflected
in the copyTbl. I want the copyTbl to reflect changes made to the
ds.Tables["Sets"].Rows.

private void PopulateCopyTableFromGroupsTable(DataTable copyTbl)
{
DataSet ds = new DataSet();
DataTable groupsTbl = new GroupsBLL().GetAll();

ds.Merge(groupsTbl);
ds.Merge(copyTbl);

DataRelation relation =
ds.Relations.Add(ds.Tables["Groups"].Columns["GroupID"],
ds.Tables["Sets"].Columns["GroupID"]);

DataRow groupRow;

foreach (DataRow setRow in ds.Tables["Sets"].Rows)
{
groupRow = setRow.GetParentRow(relation);

setRow["GroupCode"] = groupRow["Code"];
}
}

How should I do this?

Thanks
Andrew
 
J

J055

OK

I added these lines to the end of the method:

copyTbl.Clear();
copyTbl.Merge(ds.Tables["Sets"]);


But is that really the best approach? Seems like there may be unnecessary
operations here?

Thanks
Andrew
 
W

WenYuan Wang [MSFT]

Hello Andrew,

The implement of Merge method copies each row of source datatable, thus
there is no relationship between the original table and the merged table.
The change what you made in merged table will not reflect to the original
table.

Clearing the copyTbl table and populating it again is the correct way.
However, this is not the best way. What you need is Add() method, rather
than Merge(). Merge meaning copy the original rows into target table.

You could add copyTbl and groupsTbl tables into dataset, build relationship
and modify dataset.
All actions will also reflect to the copyTbl table.
Such as:
DataSet ds = new DataSet();
DataTable groupsTbl = new GroupsBLL().GetAll();

* ds.Tables.Add(groupsTbl);
* ds.Tables.Add(copyTbl);

DataRelation relation
=ds.Relations.Add(ds.Tables["Groups"].Columns["GroupID"],ds.Tables["Sets"].C
olumns["GroupID"]);
DataRow groupRow;
foreach (DataRow setRow in ds.Tables["Sets"].Rows)
{
groupRow = setRow.GetParentRow(relation);
setRow["GroupCode"] = groupRow["Code"];
}

Hope this helps. Please let me know if you meet any further issue.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

WenYuan Wang [MSFT]

Hello Andrew,

I have not heard from you a couple of days. Have you resolved the issue so
far?
Please feel free to update here if you meet any further issue. I'm glad to
assist you.

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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