Search code examples
vb.netado.netadodb

Converting to ADO.net from ADODB recordset and having difficulty figuring out what the problem is


If gItemCnt > 0 Then
    For iRow = 1 To gItemCnt
        sSpecies = Mid(gItemArray(iRow), 15, 2)
        bFound = False
        For x = 1 To rsSum.RecordCount
            If rsSum.Fields("Species").Value = sSpecies Then
                bFound = True
                Exit For
            End If
            rsSum.MoveNext()
        Next x
        If bFound = False Then
            rsSum.AddNew()
            rsSum.Fields("SpeciesSort").Value = GetDesc(gSpeciesCodes, sSpecies, 3, 1)
            rsSum.Fields("Species").Value = sSpecies
        End If
        lFootage = cPrint.GetItemArrayFootage(iRow, True)
        rsSum.Fields("MTDShipFootage").Value = rsSum.Fields("MTDShipFootage").Value + lFootage
        rsSum.Update()
        rsSum.MoveFirst()
    Next iRow
End If

Above is the old code, below one of the many things I have tried but my index's get out of whack and throw an error that index 'whatever number' does not exist.

Dim bFound As Boolean
Dim lFootage As Integer
Dim iRow As Short
Dim lPlcHoldr As Long
Dim y As Integer = -1
x = -1

If gItemCnt > 0 Then
    For iRow = 1 To gItemCnt
        sSpecies = Mid(gItemArray(iRow), 15, 2)
        bFound = False
        Dim my_row As DataRow

        For Each my_row In dtSum.Rows
            If my_row("Species") = sSpecies Then
                bFound = True
                y += 1
                Exit For
            End If
            x += 1
        Next

        'For x = 0 To dtSum.Rows.Count - 1
        '    If dtSum.Rows(x)("Species") = sSpecies Then
        '        bFound = True
        '        Exit For
        '    End If
        'Next x
        Dim r As DataRow = dtSum.NewRow
        If bFound = False Then
            'Dim r As DataRow = dtSum.NewRow
            r("SpeciesSort") = GetDesc(gSpeciesCodes, sSpecies, 3, 1)
            r("Species") = sSpecies
            r("DlyShipFootage") = lPlcHoldr
            r("DlyRemanFootage") = lPlcHoldr
            r("DlyClaimFootage") = lPlcHoldr
            r("MTDShipFootage") = lPlcHoldr
            r("MTDRemanFootage") = lPlcHoldr
            r("MTDClaimFootage") = lPlcHoldr
            r("MTDClaimFootage") = lPlcHoldr
            dtSum.Rows.Add(r)
        End If
        If bFound = True Then
            x = y
            lFootage = cPrint.GetItemArrayFootage(iRow, True)
            dtSum.Rows(x)("MTDShipFootage") = dtSum.Rows(x)("MTDShipFootage") + lFootage
            daSum.Update(ds, "tblTmpDlySum")
            Debug.WriteLine(lFootage)
        Else
            x += 1
            lFootage = cPrint.GetItemArrayFootage(iRow, True)
            r("MTDShipFootage") = r("MTDShipFootage") + lFootage
            Debug.WriteLine(lFootage)
        End If

    Next iRow
End If

Can anyone see where I am messing up? I am sure it is with the x and/or y that I am adding in there. I am very new to vb.net and coding all together, since December 2022 is when I was put in a position to help translate all our legacy apps from vb6 to .net and I figured I should just update everything to ado.net while updating everything else. However, this section is a little more complicated to me. Any direction would be appreciated.


Solution

  • If gItemCnt > 0 Then
    
        For iRow = 1 To gItemCnt
            sSpecies = Mid(gItemArray(iRow), 15, 2)
            bFound = False
    
            For x = 0 To dtSum.Rows.Count - 1
                If dtSum.Rows(x)("Species") = sSpecies Then
                    bFound = True
                    Exit For
                End If
            Next x
            Dim r As DataRow = dtSum.NewRow
            If bFound = False Then
                'Dim r As DataRow = dtSum.NewRow
                r("SpeciesSort") = GetDesc(gSpeciesCodes, sSpecies, 3, 1)
                r("Species") = sSpecies
                r("DlyShipFootage") = lPlcHoldr
                r("DlyRemanFootage") = lPlcHoldr
                r("DlyClaimFootage") = lPlcHoldr
                r("MTDShipFootage") = lPlcHoldr
                r("MTDRemanFootage") = lPlcHoldr
                r("MTDClaimFootage") = lPlcHoldr
                r("MTDClaimFootage") = lPlcHoldr
                dtSum.Rows.Add(r)
            End If
            If bFound = True Then
    
                lFootage = cPrint.GetItemArrayFootage(iRow, True)
                dtSum.Rows(x)("MTDShipFootage") = dtSum.Rows(x)("MTDShipFootage") + lFootage
                daSum.Update(ds, "tblTmpDlySum")
                Debug.WriteLine(lFootage)
                x = 0
            Else
    
                lFootage = cPrint.GetItemArrayFootage(iRow, True)
                r("MTDShipFootage") = r("MTDShipFootage") + lFootage
                Debug.WriteLine(lFootage)
                x = 0
            End If
    
        Next iRow
    End If
    

    This is what got it working. Thanks for your help. I guess having someone and somewhere to talk things out really made me see what was going on. Reseting x back to 0 allowed the for loop to start back at the first row in the datatable.