Solved

Comparing 2 datatables

Posted on 2003-10-27
7
225 Views
Last Modified: 2010-04-16
I have created a dataset, added one table(Table1) to it, which is populated with the details of the folders in a particular directory. I want to add another table(Table2) which will get folder details from a table in a sql database. I would then like to compare the 2 tables and if there are rows(based on column -foldername), that are present in Table2 but not in Table1 then append only those(that are present in Table2 but not in Table1) to Table1. I would then bind Table 1 to a grid. What would be a good way to achieve the above. Code samples will be really helpful.
Thanks in advance.
0
Comment
Question by:Rintu
  • 5
  • 2
7 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 9630375


                  DataTable dt1 = new DataTable();
                  DataTable dt2 = new DataTable();



                  //Load Data into DataTables


                  DataRow resultant;
                  
                  foreach(DataRow dr in dt2.Rows)
                  {
                        resultant = dt1.Rows.Find(dr["foldername"].ToString());
                        if(resultant == null)
                              dt1.Rows.Add(dr);
                  }

                  dataGrid1.DataSource = dt1;
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9630396


In a nutshell, the above code has two DataTables

dt1 and dt2


the code also creates a new dataRow for temperary storage.


it then loops through each data row in dt2 and grabs the data from column "foldername" and tries to find that value in data table 1 (dt1).

if it finds the value then it sets the resultant (a datarow) to the value of the current datarow.  if it does not find a value, it sets the resultant to null.

If the resultant is null (i.e. the row was not found in dt1) then the current datarow in dt2 is added to dt1.

0
 

Author Comment

by:Rintu
ID: 9635618
The above solution does not seem to work in my case as the col. FolderName is not the primarykey. Can you please suggest an alternative  ?
Thanks in advance.
0
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.

 
LVL 22

Expert Comment

by:_TAD_
ID: 9635736


Do you have a primary key specified?

If not, the only alternative I see is to go through each and every directory in both tables and see if that value exists in the other table.

If your data tables have less than 100 rows each then you should not notice a performance hit, but many more than that and you may notice some delay.



I suppose you could create a query string and then execute a filter and see how many rows are available.

I haven't played with the filter command much, but I think it might work.

I'll play around with some code this afternoon and see what I can come up with that doesn't use the "find' function
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9644377


Are each of the rows in your data table unique?

If the rows are unique, then you can use the row as your key since the Find() function uses an object as a key.


               DataTable dt1 = new DataTable();
               DataTable dt2 = new DataTable();



               //Load Data into DataTables


               DataRow resultant;
               
               foreach(DataRow dr in dt2.Rows)
               {
                    resultant = dt1.Rows.Find(dr);
                    if(resultant == null)
                         dt1.Rows.Add(dr);
               }

               dataGrid1.DataSource = dt1;
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 250 total points
ID: 9644415


The only problem with the code above is that I don't know if you will ever find a match.  I don't know if the find function is actually using the values of the data row or if it is using the datarow object identifier or possibly its hash value.
0
 

Author Comment

by:Rintu
ID: 9644416
No, the rows are not unique. However I do want to ensure that only rows which have a unique value for the the col.'foldername' are added to the table.
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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 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

19 Experts available now in Live!

Get 1:1 Help Now