Search code examples
arraysdictionaryasp-classic

ASP Classic Dictionary [Key, Array] Error


I have a function that uploads an Excel file to my server and saves this file in a dictionary[Key, Array].

I loop the rows of the Excel file, create an array with the values of that row and add this array to the dictionary like that:

Set excel = Server.CreateObject("ADODB.Recordset")
excel.Open sql, conn

Set newExcel = Server.CreateObject("Scripting.Dictionary")
i = 0

Do Until excel.EOF
   newRow = Array(excel(0), excel(1), excel(2), excel(3), excel(4), excel(5), excel(6), excel(7))
   newExcel.Add i, newRow

   i = i + 1
   excel.MoveNext
Loop

And then I try to access the filled dictionary to print the values but I get an error on the Response.Write row(j).

For Each k In newExcel.Keys
   row = newExcel.Item(k)

   For j = LBound(row) To UBound(row)
      Response.Write row(j)
   Next
Next

The error:

ADODB.Field error '80020009'

Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.

Thanks for helping!


Solution

  • The error is because you are copying the ADODB.Field objects not their underlying values. This means when you access the array it tries to access the ADODB.Field object which requires an active database connection. If you have since closed the database connection you will receive the error;

    ADODB.Field error '80020009'

    Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.

    Adjusting this line

    newRow = Array(excel(0), excel(1), excel(2), excel(3), excel(4), excel(5), excel(6), excel(7))
    

    to

    newRow = Array(excel(0).Value, excel(1).Value, excel(2).Value, excel(3).Value, excel(4).Value, excel(5).Value, excel(6).Value, excel(7).Value)
    

    will store the underlying values, not the objects themselves.