Solved

Comparing 2 datatables

Posted on 2003-10-27
7
227 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 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