Update one DataTable from another

  • Thread starter Thread starter Greg Peck
  • Start date Start date
G

Greg Peck

Hi I need to update a field (fldX) in one DataTable (dt1) where there is a
match on fldA in another DataTable (dt2).

Is the only way to do this by itterating through the DataRows of dt1,
serching dt2 for a matching fldA and if found do the update and then move on
to the next row?

Thanks Greg
 
Greg,

You can use a relation and than use the childcollections and parent
properties.

I hope this helps,

Cor
 
Cor,
A relation will not update dt1, will it?
I need to update the field where there is a match and then later update the
field on records where there were no matches with data from a different
source.

So I assume the only way is itterating through the DataTables?

Greg
 
Cor,
I must not be explaining this correctly.

I can see that I could display a child field and even retrieve it but that
is not adequate, I need to update the master table.

I start with a DataTable, SOs with 2 fields that contain 'Nulls', UnitsTo
and UnitsConv.

I need to populate these two fields.

Taking 1st Field as an example, UnitsTo, populating it involves:
find matching records in a Table ItemMaster. ItemMaster has fields including
ItemProd and ItemToUnits.
Matching records are found using SOProd = ItemProd And SOWhs = ItemWhs

First stage involves Updating [SOs].UnitsTo with value of
[ItemMaster ].ItemToUnits
Not all records in SOs has a matching record in ItemMaster so not all
records will be updated

Next stage involves Updating [SOs].UnitsTo with value from
[SOs].UnitsStocking

Updating [SO].UnitsConv is even more complicated.

I suppose that I can create a relationship between SOs and ItemMaster
Then itterate through SOs
and use the code in the link you provided to return the value of ItemToUnits
if there is a matching record
and use it to update SOs.UnitsTo
and if there is not a matching record then use SOs.UnitsStocking to update
SOs.UnitsTo

This is better than itterating both tables but still involves itterating
SOs.

Is that the way you would achieve the result?

Greg
 
Greg,

In my opinion is one thing you would not be afraid of in a modern computer,
that is (normally) itterating. It takes mostly *no* time. When you compare
what a simple resizing of a window does, than you know why I write this.

Therefore in my opinion you would take the approach which is the most
comfortable for you. (Which you can easy maintain). I only gave you an
alternative.

Cor
 
Sorry but I can not see how the link you provided provides me a different
way to achieve the results that I am looking for.

Thanks anyway.
 
Greg,
Sorry but I can not see how the link you provided provides me a different
way to achieve the results that I am looking for.

For me that is obvious, with setting the right relation I can in one time
get the rows that are affected.

However nobody says that you would use that. All your replys look for me if
you don't want to have an answer, only somebody who is telling that your way
is the best way. And than you probably will not get here.

Cor
 
Greg,
I don't think that you can create complex relationships in a DataSet ie
where the relationship uses multiple fields.

If you just need to display the correct for the UnitsTo etc and that is not
in a DataGrid, you could do the logical selection when the record is
selected.

But if you need the data in a Grid or to update the Source DB I think you
will need to go about it the way you originally proposed, Record by record
doing the search, if found replace otherwise substitue.

Doug

Greg Peck said:
Cor,
I must not be explaining this correctly.

I can see that I could display a child field and even retrieve it but that
is not adequate, I need to update the master table.

I start with a DataTable, SOs with 2 fields that contain 'Nulls', UnitsTo
and UnitsConv.

I need to populate these two fields.

Taking 1st Field as an example, UnitsTo, populating it involves:
find matching records in a Table ItemMaster. ItemMaster has fields including
ItemProd and ItemToUnits.
Matching records are found using SOProd = ItemProd And SOWhs = ItemWhs

First stage involves Updating [SOs].UnitsTo with value of
[ItemMaster ].ItemToUnits
Not all records in SOs has a matching record in ItemMaster so not all
records will be updated

Next stage involves Updating [SOs].UnitsTo with value from
[SOs].UnitsStocking

Updating [SO].UnitsConv is even more complicated.

I suppose that I can create a relationship between SOs and ItemMaster
Then itterate through SOs
and use the code in the link you provided to return the value of ItemToUnits
if there is a matching record
and use it to update SOs.UnitsTo
and if there is not a matching record then use SOs.UnitsStocking to update
SOs.UnitsTo

This is better than itterating both tables but still involves itterating
SOs.

Is that the way you would achieve the result?

Greg

Cor Ligthert said:
Greg,

You can get a(the) child row(s) using a relation, the same is for the parent
http://msdn.microsoft.com/library/d...rfsystemdatadatarowclassgetchildrowstopic.asp
I hope this helps,

Cor
 

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

Back
Top