Nested For....Loop!! HELP

martin_d_bell

Newcomer
Joined
Oct 8, 2004
Messages
17
Hi All,

I am quite new to programming and have grasped most things except for Nested For Loops which just will not click!!

I am trying to write a small application which loops through two datasets and compares values in dataset1 to the values in dataset2. So far my code looks like this

Code:
lblCheckDone.Visible = False
        lvbPartCodes.Items.Clear()

        If IsDBNull(DsPOPPartCodes1.POP_Parts_Costs) = False Then DsPOPPartCodes1.POP_Parts_Costs.Clear()
        SqlDAPOPPartCodes.Fill(DsPOPPartCodes1.POP_Parts_Costs)

        If IsDBNull(DsWDPartCodes1.POP_WDPcodes) = False Then DsWDPartCodes1.POP_WDPcodes.Clear()
        SqlDAWDPartCodes.Fill(DsWDPartCodes1.POP_WDPcodes)

        Dim x As Int16
        Dim y As Int16
        Dim PCode As String
        Dim PCodeDesc As String
        Dim Counter As String

        x = 0
        y = 0
        Counter = 0

        For y = 0 To DsWDPartCodes1.POP_WDPcodes.Rows.Count - 1

            PCode = DsWDPartCodes1.POP_WDPcodes.Rows(y).Item(1)
            PCodeDesc = DsWDPartCodes1.POP_WDPcodes.Rows(y).Item(2).ToString

            For x = 0 To DsPOPPartCodes1.POP_Parts_Costs.Rows.Count - 1

                If " " + Replace(PCodeDesc, ",", "") <> DsPOPPartCodes1.POP_Parts_Costs.Rows(x).Item(2).ToString Then

                    Dim lvbPartRow As New ListViewItem(PCode)
                    lvbPartRow.SubItems.Add(PCodeDesc)
                    lvbPartCodes.Items.Add(lvbPartRow)

                    Counter = Counter + 1
                    lblCheckDone.Text = Counter
                    Exit For
                End If
            Next
        Next
        SqlDAWDPartCodes.Update(DsWDPartCodes1.POP_WDPcodes)
        lblCheckDone.Visible = True

I have a listview and a button on my form, when the user clicks the button the values which do not match need to be displayed in the listview.

I just need help with the Nested For Loop as at the minute it lists all of the values in the ListView whether they match up or not.

Thanks
Martin
 
Your problem is that the condition on the if statement is '<>', that means if any row in the second dataset differs from the one in the first being checked you will add to the list box.

Code:
matched=false
For x = 0 To DsPOPPartCodes1.POP_Parts_Costs.Rows.Count - 1

                If " " + Replace(PCodeDesc, ",", "") = DsPOPPartCodes1.POP_Parts_Costs.Rows(x).Item(2).ToString Then
                   matched=true
                   Exit For
                End If
Next
if matched=false then
 Dim lvbPartRow As New ListViewItem(PCode)
  lvbPartRow.SubItems.Add(PCodeDesc)
  lvbPartCodes.Items.Add(lvbPartRow)

  Counter = Counter + 1
  lblCheckDone.Text = Counter
endif

This logic will fix the problem.

One other point - if the data is in datasets - why not have the data loaded into 2 tables in the same dataset then running an outer join SQL query between the tables to select ones that don't match?
 
Query

Thanks for the advice on using a query with an outer join. That works perfect and saves me having to program anything! Sometimes you can overcomplicate things and forget somebody else has programmed the tools to do the job.

Thanks again
 
Back
Top