Search code examples
arraysvb.netobjectadodbrecordset

VB.Net : Create a Recordset with manual inputs and GetRows() methods failing


First time writing on a forum but this one really left me no choice and it seems that nobody had the same problem as I have... not a good sign...

I have a project to use the COM Server of a software we use internally and need to use one of their built-in function which requires a recordset as an input and return another recordset with the results (important because I need to stick with the recordset).

Here's breifly what I tried. I create a recordset from scratch and fill it with some hardcoded data just for testing purposes. Once my recordset is filled, I want to look at the data just to be sure everything works well, but I'll have to do the same eventually with my results.

The problem I get is it seems that the GetRows() method return only 1 row every time depending on the last row I moved to. But once it's called, I cannot get the other records. I'm already using the GetRows() method with an actual query and still with an ADODB recordset and it works perfectly. Building a recodset from scratch seems less easy.

I need to put all my data in an object to work with it. But even if I want to use only a recordset, I cannot access to all data in it. Very fustrating... something I'm missing here...

Error I get: either bof or eof is true or the current record has been deleted

Thanks in advance,

Public Function GetFDBData() As Boolean

        Dim filtersView As New ADODB.Recordset
        Dim rsFields(1) As Object
        Dim fieldsAPT(3, 1) As Object
        Dim dataView As Object
        Dim i As Integer

        rsFields(0) = "Field Name"
        rsFields(1) = "Filter"

        fieldsAPT(0, 0) = "ISIN"
        fieldsAPT(0, 1) = "=CA89*"
        fieldsAPT(1, 0) = "Currency"
        fieldsAPT(1, 1) = "=CAD"
        fieldsAPT(2, 0) = "Line"
        fieldsAPT(2, 1) = "=Bond"
        fieldsAPT(3, 0) = "Redemption Date"
        fieldsAPT(3, 1) = "=20230*"

        Try
            'Build the recordset containing APT fields and filters (in the same variable fieldsAPT)
            filtersView.CursorLocation = ADODB.CursorLocationEnum.adUseClient
            filtersView.Fields.Append(rsFields(0), ADODB.DataTypeEnum.adVarChar, 30)
            filtersView.Fields.Append(rsFields(1), ADODB.DataTypeEnum.adVarChar, 30)
            filtersView.Open(, , ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)

            Dim fieldAPT(1)
            For i = 0 To UBound(fieldsAPT)
                fieldAPT(0) = fieldsAPT(i, 0)
                Console.WriteLine(fieldAPT(0))              'Works fine
                fieldAPT(1) = fieldsAPT(i, 1)
                Console.WriteLine(fieldAPT(1))              'Works fine
                filtersView.AddNew(rsFields, fieldAPT)
                filtersView.Update()
                Console.WriteLine(filtersView.RecordCount)  'I can see 1 2 3 4 no problem here
            Next i

            Dim xx As Integer = filtersView.RecordCount     'xx is 4 as expected
            Console.WriteLine("xx: " & xx)
            filtersView.MoveFirst()                         'Will move to the first record or whatever record
            'dataView = filtersView.GetRows()               'I expected this line to work, but same results

            For i = 0 To xx - 1
                dataView = filtersView.GetRows()
                Console.WriteLine(dataView(i, 0).ToString)      'ISIN, normal
                Console.WriteLine(filtersView.RecordCount)      'Still equals 4, normal
                Console.WriteLine(filtersView.BOF)              'False, normal
                Console.WriteLine(filtersView.EOF)              'True, which is NOT normal
                Console.WriteLine(filtersView.AbsolutePosition) 'Get -3 not sure why (position 1 related to 4???)
                Console.WriteLine(filtersView.MaxRecords)       'Get 0 not sure why
                filtersView.MoveNext()                          '!!!! Here is where it fails !!!! Cannot go more than i=0
            Next i

            GetFDBData = True
        Catch ex As Exception
            MsgBox(ex.Message)
            GetFDBData = False
        Finally
            'Clear memory
            filtersView.Close()
        End Try
End Function

Also, if I do this,

Dim xx As Integer = filtersView.RecordCount     'xx is 4 as expected
            Console.WriteLine("xx: " & xx)
            filtersView.MoveLast()   

Instead of

 Dim xx As Integer = filtersView.RecordCount     'xx is 4 as expected
                Console.WriteLine("xx: " & xx)
                filtersView.MoveFirst()  

It will return "Redemption date" in the for loop after. Which makes sense because it is the last record. But puting movefirst even after does'nt solve the issue... still one row only. So the data is there, but I really can't extract one line and one line only...


Solution

  • Try changing your cursor type to adOpenDynamic

    EDIT: OK,your line dataView = filtersView.GetRows()

    is causing your cursor to travel to the end of the recordset, try moving it outside your loop and following it with a new MoveFirst like so

         filtersView.MoveFirst()                         'Will move to the first record or whatever record
         'dataView = filtersView.GetRows()               'I expected this line to work, but same results
    
         dataView = filtersView.GetRows()
         filtersView.MoveFirst()
    
         Dim sTemp As String = ""
         For i = 0 To xx - 1
            'Console.WriteLine(dataView(i, 0).ToString)      'ISIN, normal
            Console.WriteLine(filtersView.RecordCount)      'Still equals 4, normal
            Console.WriteLine(filtersView.BOF)              'False, normal
            Console.WriteLine(filtersView.EOF)              'True, which is NOT normal
            Console.WriteLine(filtersView.AbsolutePosition) 'Get -3 not sure why (position 1 related to 4???)
            Console.WriteLine(filtersView.MaxRecords)       'Get 0 not sure why
            sTemp = sTemp & "(" & dataView(0, i).ToString & ", " & dataView(1, i).ToString & ")"
            filtersView.MoveNext()                          '!!!! Here is where it fails !!!! Cannot go more than i=0
         Next i
         Console.WriteLine(sTemp)
    

    You can verify that by checking the value of filtersView.AbsolutePosition immediately before and after your call to GetRows

    Also, you are reversing rows and columns in dataView, move i to the second subscript position. I put it in a temporary string to make it easier to view in the debugger.