Solved

C# update one datatable from another datatable

Posted on 2014-01-17
2
2,143 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now