将空字符串数组传递给WebMethod时的IndexOutOfRangeException

时间:2012-10-04 20:44:15

标签: ajax arrays webmethod indexoutofboundsexception string

尝试了我能想到的每一个组合:String.Empty,IsDBNull。

selectPONumber从“selectPONumber:['+ JSON.stringify('')+']”

传递给“ByVal selectPONumber()As String”

StackTrace指向此VB的第二行

VB

    If Not IsDBNull(selectPONumber) Then
        If selectPONumber.Length > 1 Then
            qry = qry + "and ("
            For u As Integer = 0 To selectPONumber.Length - 1
                If u <> selectPONumber.Length Then
                    qry = qry + "and B.PONumber = '" & selectPONumber(u) & "' or "
                Else
                    qry = qry + "and B.PONumber = '" & selectPONumber(u) & "'"
                End If
            Next
            qry = qry + ") "
        Else
            qry = qry + "and B.PONumber = '" & selectPONumber(0) & "' "
        End If
    End If

非常感谢提前!

更新

用计数替换所有长度,并采取了tranceporter的建议。现在,它在“for”行上给出了相同的错误。它是如何通过If selectPONumber.Length&gt; 1?

更新代码

    If selectPONumber IsNot Nothing AndAlso selectPONumber.Count > 1 Then
            qry = qry + "and ("
            For u As Integer = 0 To (selectPONumber.Count - 1)
                If u <> selectPONumber.Count Then
                    qry = qry + "B.PONumber = '" & selectPONumber(u) & "' or "
                Else
                    qry = qry + "B.PONumber = '" & selectPONumber(u) & "'"
                End If
            Next
            qry = qry + ") "
    ElseIf selectPONumber(0) <> "" Then
        qry = qry + "and B.PONumber = '" & selectPONumber(0) & "' "
    End If

解决方案

tranceporter的解决方案是正确的。我愚蠢地将常规字符串视为代码中其他地方的数组。它开始将整个函数的其余部分指向indexoutofbounds。

1 个答案:

答案 0 :(得分:1)

尝试使用它(除非你已经拥有)

If selectPONumber IsNot Nothing Then
    If selectPONumber.Count > 0 Then
       qry = qry + "and ("
        For u As Integer = 0 To selectPONumber.Count - 1
            If u <> selectPONumber.Count Then
                qry = qry + "B.PONumber = '" & selectPONumber(u) & "' or "
            Else
                qry = qry + "B.PONumber = '" & selectPONumber(u) & "'"
            End If
        Next
        qry = qry + ") "
    Else
        qry = qry + "and B.PONumber = '" & selectPONumber(0) & "' "
    End If
End If