Click here to Skip to main content
15,942,847 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm using visual studio (vb.net) and have two datatables (dt and dt2). I want to compare each row of datatables only with each other --> row1 in dt with row1 in dt2, row2 in dt with row2 in dt2, row3 in dt with row3 in dt2 and so on.

My code now looks like this and the program compares each row in dt with each row in dt2, which is not ok.
VB
For Each row As DataRow In dt.Rows
    For Each row As DataRow In dt2.Rows

        Dim PTP_zakl As Date = row(1).ToString
        Dim AX_prevz As Date = row(1).ToString

        If PTP_zakl < AX_prevz Then
            sendMail
        Else
            Do nothing
        End If

    Next
Next


Please help me.
Thanks
Posted

As already pointed out you compare all of the rows with each other. You wrote that the requirement is to compare rows only with each other but the first question is: Is the index of the row really defining the rows to compare?

What I mean is that:
- do you have the same amount of rows in both tables?
- are the rows added to the tables so that row 2 in dt is really the same thing as row 2 in dt2 and not the row 3?

Having that said, of you have the same amount of rows then simply loop through one table and fetch corresponding row. Something like:
VB
For counter As Integer = 1 To dt.Rows.Count

   Dim PTP_zakl As Date = dt.Rows(counter)(1).ToString
   Dim AX_prevz As Date = dt2.Rows(counter)(1).ToString

   If PTP_zakl < AX_prevz Then
      sendMail()
   End If
Next counter

As a side note, if the column 1 contains a date I'd use CType to convert the object to the date, not ToString and implicit conversion.

ADDITION:

If the comparison can be done based on a column to find a matching row the loop could be simplified to something like the following:
VB
For Each dt2_row As System.Data.DataRow In dt2.Rows
   If (dt.Select("Manjkajoči_MA <'" & dt2_row("Prevzeti_MA").ToString & "'").Count = 0) Then
      sendMail()
   End If
Next dt2_row
 
Share this answer
 
v4
Comments
mrUR 26-Jul-15 5:39am    
I should explain all that earlier, sorry:
- No dt table has more rows than dt2 (seen bellow)
- both tables get rows from sql query, from different database. I guess that's why some rows are not present in dt2.

I'll try to provide how the output looks like...
Wendelius 26-Jul-15 5:41am    
Ok, so if dt2 has rows 1,2,3 and 4 and dt has rows 1,2,3 how do you know that is't number 4 missing and not some other row? In my opinion you should have some column to identify the row and not to rely on the index number of the row.
mrUR 26-Jul-15 5:47am    
This is the first couple of rows for dt:

Manjkajoči_MA; Datum_konca
MA31623; 19.5.2015 0:00
MA11702; 11.6.2015 0:00
MA05840; 22.6.2015 0:00
MA05840; 22.6.2015 0:00
MA05840; 22.6.2015 0:00
MA01366; 2.7.2015 0:00
MA01366; 2.7.2015 0:00
MA31623; 7.7.2015 0:00
MA31887; 13.7.2015 0:00
MA20017; 13.7.2015 0:00
MA05840; 13.7.2015 0:00
MA31389; 13.7.2015 0:00
MA05485; 13.7.2015 0:00
MA32347; 15.7.2015 0:00
MA05485; 15.7.2015 0:00
MA06170; 15.7.2015 0:00
MA32917; 15.7.2015 0:00
MA32917; 15.7.2015 0:00
MA01546; 16.7.2015 0:00
MA01546; 16.7.2015 0:00
MA01546; 16.7.2015 0:00
MA01546; 16.7.2015 0:00
MA01546; 16.7.2015 0:00
MA01546; 16.7.2015 0:00
MA01546; 16.7.2015 0:00
MA01546; 16.7.2015 0:00
MA01546; 16.7.2015 0:00
....

and this is first couple of rows for dt2

Prevzeti_MA; Datum_prevzema
MA31623; 5.5.2015 0:00
MA11702; 16.3.2015 0:00
MA05840; 4.6.2015 0:00
MA05840; 4.6.2015 0:00
MA05840; 4.6.2015 0:00
MA01366; 11.3.2015 0:00
MA01366; 11.3.2015 0:00
MA31623; 5.5.2015 0:00
MA31887; 17.3.2015 0:00
MA05840; 4.6.2015 0:00
MA31389; 5.3.2015 0:00
MA05485; 8.6.2015 0:00
MA32347; 2.6.2015 0:00
MA05485; 8.6.2015 0:00
MA32917; 19.3.2015 0:00
MA32917; 19.3.2015 0:00
MA01546; 24.7.2015 0:00
MA01546; 24.7.2015 0:00
MA01546; 24.7.2015 0:00
MA01546; 24.7.2015 0:00
MA01546; 24.7.2015 0:00
MA01546; 24.7.2015 0:00
MA01546; 24.7.2015 0:00
MA01546; 24.7.2015 0:00
MA01546; 24.7.2015 0:00
MA01546; 24.7.2015 0:00
MA01546; 24.7.2015 0:00
....


If we look at row10 there is "MA20017" but it's only present in dt. In dt2 row10 should have value NULL or something, otherwise further row comparison on MA is impossible. Hope it's clear enough :)
Wendelius 26-Jul-15 5:58am    
Have a look at the modified answer. Is that what you're after?
mrUR 26-Jul-15 6:29am    
When i copy ADDITION code in my program it gets underline saying 'Count' is not a member of 'System.Array' so i used .Lenght instead of .Count, i guess that's ok too?

But when i start the program i get this error: "Additional information: There is no row at position 1" and it refers to Dim PTP_zakl As Date = dt.Rows(counter)(1).ToString. But there is row1 in dt, so why the error??
Advice: Learn about debugger and look what append to row.

You need something like:
Go to first row of Dt
Go to first row of Dt2
repeat
  do your stuff here

  go to next row of Dt
  go to next row of Dt2
until End of Dt or End of Dt2


You also need to learn how to access your data
 
Share this answer
 
v2
Comments
mrUR 25-Jul-15 17:40pm    
I do use debugger, but all i see is that the program loops through all of possible row combinations.

Your hint is good, but still don't know exactly how to do it :/
The code you posted would compare each row from dt1 with each other row from dt2 (if it's coded right). What you have to do is :
- get the maximum number of rows (either in dt1 or dt2).
VB
NumberOfRows = Math.Max(dt1.rows.count , dt2.rows.count)

- make a loop with this variable, but check if the rows are assigned and instanced
VB
for i as integer = 0 to NumberOfRows -1
   ' check and compare here
next

- compare the entries and if not equal do your action
 
Share this answer
 
v3
Comments
Patrice T 25-Jul-15 16:52pm    
No, its code is bugged to the core.
For each row of Dt, it read each row of Dt2, and store the first field from Dt2 in both PTP_zakl and AX_prevz
it then compare both vars for a difference which always fail
Ralf Meier 25-Jul-15 16:59pm    
You are right - but if the code is written correctly it would work like I described (I improved my answer). The actual code only compares both rows(1) - completly without sense. My intension was to help to find the right Solution - not to work with the inquirers code-snipped - I think that was your intension too ...
Patrice T 25-Jul-15 17:05pm    
Sure, that is why I propose to learn about debugger :)
mrUR 25-Jul-15 17:33pm    
Ok, i've taken your part of code Ralf, but i'm not sure if i did it right way.

Hide Copy Code
Dim NumberOfRows = Math.Max(dt.Rows.Count, dt2.Rows.Count)

<pre>
For i As Integer = 0 To NumberOfRows - 1

If dt.Rows.Count <= dt2.Rows.Count Then
sendMail
Else
Do nothing
End If
Next</pre>

As a result for example i get "dt.rows.count 4 < dt2.rows.count 4" and this is not ok because it should check a value inside rows.

All rows are in DATE format and if row1(dt) has value 5/5/2015 and row1(dt2) has value 7/5/2015, program should send an email (because dt < dt2) and so on for all rows.
Patrice T 25-Jul-15 22:35pm    
You really need to understand what your code is doing. Go to Documentation, abuse Help, use debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900