经典ASP:填充3D数组。下标超出范围:'objRS(...)'

时间:2015-09-09 10:17:06

标签: asp-classic active-directory

我正在制作经典的ASP组织结构图。问题是我得到“下标超出范围:'objRS(...)'”错误。如果fullname数组的index_one等于部门数组的index_one中保存的公司,并且fullname数组的index_two等于部门的index_two,那么理论上这应该意味着用户在部门中,所以我将回显名称< / p>

我有以下代码

 'Define the AD OU that contains our users
 dim ADUser, department, fullname, index_one, index_two, index_three
 fullname           = Array()
 department     = Array()     
 index_one = 0
 index_two = 0
 index_three = 0

 ...

 ADUser = "LDAP://OU=Staff,OU=Users,DC=example,DC=internal"
 ' Make AD connection and run query
 Set objCon = Server.CreateObject("ADODB.Connection")
 objCon.provider ="ADsDSOObject"
 objCon.Properties("User ID") = "EXAMPLE\user"
 objCon.Properties("Password") = "Pasword"
 objCon.Properties("Encrypt Password") = TRUE
 objCon.open "Active Directory Provider"
 Set objCom = CreateObject("ADODB.Command")
 Set objCom.ActiveConnection = objCon
 objCom.CommandText ="select company, department, givenName, sn, telephoneNumber, mail, title FROM '"& ADUser &"' where company ='*' ORDER BY department ASC"
 Set objRS = objCom.Execute
 ' Loop over returned recordset and output HTML
 Do While Not objRS.EOF Or objRS.BOF
      'If index_one of the fullname array equals the company held in index_one of the department array, and index_two of the fullname array equals index_two of department, then this should, in theory, mean the user is in the department so I will echo out the name
      department(index_one) = objRS("company")
      department(index_two) = objRS("department")
      fullname(index_one) = objRS("company")
      fullname(index_two) = objRS("department")
      fullname(index_three) = objRS("givenName") & " " & objRS("sn")
      index_one = index_one + 1
      index_two = index_two + 1
      index_three = index_three + 1
      objRS.MoveNext
      Response.Flush
 Loop
 ' Clean up
 objRS.Close
 objCon.Close
 Set objRS = Nothing
 Set objCon = Nothing
 Set objCom = Nothing

我收到错误:

  

Microsoft VBScript运行时错误'800a0009'

     

下标超出范围:'objRS(...)'

     

/activedirectory/ldap2.asp,第38行

第38行是注释的最后一行,所以我删除了它,然后当我尝试定义每个数组元素时我得到了错误,所以只是将它们定义为:

department() = objRS("company")
department() = objRS("department")

但我仍然收到错误

1 个答案:

答案 0 :(得分:1)

我没有看到你实际上在任何地方为阵列分配尺寸。没有任何参数的Array()函数将创建一个零维数组;显然,任何索引都将超出范围。您需要在某处使用Redim,或者如果您要添加已具有值的数组Redim Preserve

另外,我不知道你认为这会做什么:

department() = objRS("company")
department() = objRS("department")

...但是在VBScript中,你不会使用那样的空括号。

(旁白:你实际上是严格使用一维数组。三维数组是Dim my3D(10,15,100):这是一个10列宽,15列深,100行长的立方体。表格最多是2D数组,你几乎不需要使用3D数组。事实上,在大多数情况下,3D数组是一种过于复杂的思维症状,而不是好的代码。)