Solved

C# update one datatable from another datatable

Posted on 2014-01-17
2
2,363 Views
Last Modified: 2014-01-17
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
Comment
Question by:esak2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39787977
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
 

Author Comment

by:esak2000
ID: 39788198
nice and simple, thanks!
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question