• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2831
  • Last Modified:

C# update one datatable from another datatable

How do I update a field of one DataTable from another Datatable?

I have 2 Datatables

Datatable #1:
UPC
Quantity
Price
NewPrice

Datatable #2
UPC
NewPrice


I want to update DataTable #1 with the NewPrice column from Datatable #2 when the UPCs of the 2 tables match. I'm interested in the exact code (for statement?) that will generate the results.
0
esak2000
Asked:
esak2000
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Assuming dt1 is the table to be updated and dt2 is where the new values are coming from - and that UPC is set as the primary key for both - you'd want something like:
foreach (DataRow row in dt2.Rows)
{
    dt1.Rows.Find(row["UPC"])["NewPrice"] = row["NewPrice"];
}

Open in new window

0
 
esak2000Author Commented:
nice and simple, thanks!
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now