Hi!
My application uses two DataTables, one retrieved from XML-file
containing "Previous" situation, if application was closed and opened,
and "Current"-table as a copy of "Previous"-table when application is
just started. These both are similar DataTables and containing "ID" as
Unique-field
Application retrieves ID's from reader after certain delays into the
"Current"-table, and it's working fine. When ID's are retrieved from
reader, after that application need to check which ID's are removed or
added comparing between new "Current"- and "Previous"-table, because
it's only possible to get present ID's from reader. After removed or
added ID's are checked, then application adds these events into Event
Log. Finally new "Current"-DataTable is copied as new
"Previous"-DataTable like dtPrevious = dtCurrent.Copy(), and after that
new content of "Previous"-DataTable is saved into XML-file.
My question is, is there better way to compare changes between these
DataTables? Better like the following code below I'm using now? I'm
quite sure yes there must be (Please don't laught)!

Any succestions
how to do this better way. Anyway this code works!
'// Create "New Events" DataTable
Dim dtNewEvents As DataTable = New DataTable("Event")
dtNewEvents.Columns.Add("Function", GetType(Integer))
dtNewEvents.Columns.Add("ID", GetType(String))
Dim drNewEvent As DataRow
Dim strID As String
'// *** Check for ID's added after previous situation ***
If (dtPrevious Is Nothing) Then
'// "Previous" table does not exist:
'// -> Mark all ID's directly as Added into Event Log
For Each dr As DataRow In dtCurrent.Rows
drNewEvent = dtNewEvents.NewRow
drNewEvent("Function ID") = 2
drNewEvent("ID") = dr("ID")
dtNewEvents.Rows.Add(drNewEvent)
Next
Else
'// Previous table exists -> Compare DataRows between tables
'// *** Check for ID's added after previous situation ***
For Each drCurrent As DataRow In dtCurrent.Rows
strID = drCurrent("ID").ToString
For Each drPrevious As DataRow In dtPrevious.Rows
If (drPrevious("ID").Equals(drCurrent("ID"))) Then
strID = ""
Exit For
End If
Next
'// ID was not found in Previous table
'// -> Mark as Added into Event Log
If (strID.Length > 0) Then
drNewEvent = dtNewEvents.NewRow
drNewEvent("Function ID") = 2
drNewEvent("ID") = strID
dtNewEvents.Rows.Add(drNewEvent)
End If
Next
'// *** Check for ID's removed after previous situation ***
For Each drPrevious As DataRow In dtPrevious.Rows
strID = drPrevious("ID").ToString
For Each drCurrent As DataRow In dtCurrent.Rows
If (drCurrent("ID").Equals(drPrevious("ID"))) Then
strID = ""
Exit For
End If
Next
'// Does not exist in Current table
'// -> Mark as Removed into Event Log
If (strID.Length > 0) Then
drNewEvent = dtNewEvents.NewRow
drNewEvent("Function ID") = 3
drNewEvent("ID") = strID
dtNewEvents.Rows.Add(drNewEvent)
End If
Next
End If
'// *** Save into Event Log if new events was found ***
If (dtNewEvents.Rows.Count > 0) Then
Dim el As EventsLog = New EventsLog()
el.NewEvents = dtNewEvents
el.SaveNewEvents()
End If
....code continues...
--
Thanks in advance!
Mika