索引数组错误

时间:2014-05-26 05:52:27

标签: sql arrays vb.net

我有一个网站,根据数据库中的统计信息显示图表。但是,我收到此错误" 索引超出了数组的范围。"并指出此代码:

>Line 1462: ctrAPM(ctr) = "<set value='0' />"

这是我的整个潜艇:

Public Function Bar_Task_Cat() As String
    Dim Conn As SqlConnection = New SqlConnection(MSSQLstring)
    Conn.Open()

    Dim strUserCode = CType(Session("usrCd"), String)

    Dim SQLstring As String

    If strUserCode = "ADMIN" Then
        SQLstring = "SELECT COUNT(AskAOID) AS credits, CatCode AS category, ProductCode " _
                                & "FROM dbo.AskAO_Stats " _
                                & "WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') " _
                                & "AND (Status='ACTIVE' OR Status='CLOSED') " _
                                & "AND (FirstResponse IS NOT NULL) AND (CatCode IS NOT NULL) AND (ProductCode IS NOT NULL) " _
                                & "GROUP BY ProductCode, CatCode " _
                                & "ORDER BY CatCode"
    Else
        SQLstring = "SELECT COUNT(AskAOID) AS credits, CatCode AS category, ProductCode " _
                                & "FROM dbo.AskAO_Stats " _
                                & "WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') " _
                                & "AND (Status='ACTIVE' OR Status='CLOSED') " _
                                & "AND (Deferred = '" & strUserCode & "') " _
                                & "AND (FirstResponse IS NOT NULL) AND (CatCode IS NOT NULL) AND (ProductCode IS NOT NULL) " _
                                & "GROUP BY ProductCode, CatCode " _
                                & "ORDER BY CatCode"
    End If

    Dim SQLcmd As SqlCommand = New SqlCommand(SQLstring, Conn)
    SQLcmd.Parameters.AddWithValue("@dateFrom", dpFrom.SelectedDate)
    SQLcmd.Parameters.AddWithValue("@dateTo", dpTo.SelectedDate) '& "WHERE (FirstResponse BETWEEN @dateFrom AND @dateTo) " 
    Dim DA As New SqlDataAdapter(SQLstring, Conn)
    Dim DS As New DataSet
    DA.Fill(DS)

    Conn.Close()

    Dim Def_Color_t(12) As String
    Def_Color_t(0) = "AFD8F8"
    Def_Color_t(1) = "F6BD0F"
    Def_Color_t(2) = "8BBA00"
    Def_Color_t(3) = "FF8E46"
    Def_Color_t(4) = "008E8E"
    Def_Color_t(5) = "D64646"
    Def_Color_t(6) = "8E468E"
    Def_Color_t(7) = "588526"
    Def_Color_t(8) = "B3AA00"
    Def_Color_t(9) = "008ED6"
    Def_Color_t(10) = "9D080D"
    Def_Color_t(11) = "A186BE"
    Def_Color_t(12) = "AFD8F8"


    Dim ctr As Integer = 0
    Dim ctrD As Integer = 0
    Dim ctrM As Integer = 0
    Dim ctrF As Integer = 0
    Dim ctrA As Integer = 0
    Dim ctrO As Integer = 0
    Dim m_ctr As Integer = 0
    Dim m1_ctr As Integer = 0

    Dim strXML As String
    Dim Cat As String
    Dim OldCat As String
    Dim strCatName As String

    Dim ProdCode As String

    'counters for Products
    Dim ctrAPM(26), ctrDMX(26), ctrDVS(26), ctrDVX(26), ctrEAM(26), ctrEPR(26), ctrFCX(26), ctrMHM(26), ctrONL(26), ctrOTH(26), ctrPOR(26), ctrUFF(26), ctrXMT(26) As String

    'counters for Categories
    Dim ctrAR(13), ctrDA(13), ctrDB(13), ctrDC(13), ctrDD(13), ctrDE(13), ctrDF(13), ctrDG(13), ctrDH(13), ctrDI(13), ctrDJ(13), ctrDK(13), ctrDL(13) As String
    Dim ctrDM(13), ctrDN(13), ctrDO(13), ctrDP(13), ctrDQ(13), ctrFW(13), ctrOL(13), ctrOO(13), ctrPM(13), ctrPQ(13), ctrPR(13), ctrSS(13), ctrST(13) As String

    strXML = ""
    Cat = ""
    OldCat = ""
    strCatName = ""

    strXML = strXML & "<chart palette='2' labelDisplay='Rotate' slantLabels='1' showBorder='0' bgColor='FFFFFF,FFFFFF' showPercentValues='1' showPercentInToolTip='1' stack100Percent='1' caption='Percentage of Tasks by Category by Product' shownames='1' showvalues='0' showSum='1' decimals='0' useRoundEdges='1'>"
    strXML = strXML & "<categories>"
    Do Until ctr = DS.Tables(0).Rows.Count
        Cat = DS.Tables(0).Rows(ctr)("category").ToString
        If Cat <> OldCat Then
            Select Case DS.Tables(0).Rows(ctr)("category").ToString
                Case "AR"
                    strCatName = "AR" 'Architecture and Application
                Case "DA"
                    strCatName = "DA" 'Accessories
                Case "DB"
                    strCatName = "DB" 'Advanced Control
                Case "DC"
                    strCatName = "DC" 'Batch
                Case "DD"
                    strCatName = "DD" 'Bundles
                Case "DE"
                    strCatName = "DE" 'Engineering Tools
                Case "DF"
                    strCatName = "DF" 'Modernization and Migration
                Case "DG"
                    strCatName = "DG" 'DeltaV SIS
                Case "DH"
                    strCatName = "DH" 'Wireless
                Case "DI"
                    strCatName = "DI" 'DeltaV Hardware
                Case "DJ"
                    strCatName = "DJ" 'DeltaV Zones
                Case "DK"
                    strCatName = "DK" 'Integration and Connectivity
                Case "DL"
                    strCatName = "DL" 'Languages
                Case "DM"
                    strCatName = "DM" 'Cactus
                Case "DN"
                    strCatName = "DN" 'Operations Software
                Case "DO"
                    strCatName = "DO" 'PROVOX and RS3 Transitions
                Case "DP"
                    strCatName = "DP" 'Workstation Hardware and Software
                Case "DQ"
                    strCatName = "DQ" 'Simulate and Virtualization
                Case "FW"
                    strCatName = "FW" 'Forward to other groups
                Case "OL"
                    strCatName = "OL" 'Ordering, Licensing, Policies
                Case "OO"
                    strCatName = "OO" 'Others
                Case "PM"
                    strCatName = "PM" 'Forward to Product Manager
                Case "PQ"
                    strCatName = "PQ" 'Proposals and Quotations
                Case "PR"
                    strCatName = "PR" 'Product Information
                Case "SS"
                    strCatName = "SS" 'Forward to Sales Support
                Case "ST"
                    strCatName = "ST" 'Systems and Tools
            End Select
            strXML = strXML & "<category label='" & strCatName & "' value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            m_ctr = m_ctr + 1
        End If
        OldCat = Cat
        ctr = ctr + 1
    Loop
    strXML = strXML & "</categories>"

    'initialize
    ctr = 0

    Do Until ctr = m_ctr
        ctrAPM(ctr) = "<set value='0' />"
        ctrDMX(ctr) = "<set value='0' />"
        ctrDVS(ctr) = "<set value='0' />"
        ctrDVX(ctr) = "<set value='0' />"
        ctrEAM(ctr) = "<set value='0' />"
        ctrEPR(ctr) = "<set value='0' />"
        ctrFCX(ctr) = "<set value='0' />"
        ctrMHM(ctr) = "<set value='0' />"
        ctrONL(ctr) = "<set value='0' />"
        ctrOTH(ctr) = "<set value='0' />"
        ctrPOR(ctr) = "<set value='0' />"
        ctrUFF(ctr) = "<set value='0' />"
        ctrXMT(ctr) = "<set value='0' />"
        ctr = ctr + 1
    Loop

    ctr = 0
    Cat = ""
    OldCat = DS.Tables(0).Rows(0)("category").ToString

    Do Until ctr = DS.Tables(0).Rows.Count
        ProdCode = DS.Tables(0).Rows(ctr)("ProductCode").ToString

        Cat = DS.Tables(0).Rows(ctr)("category").ToString

        If Cat <> OldCat Then
            m1_ctr = m1_ctr + 1
        End If

        Select Case ProdCode
            Case "APM"
                ctrAPM(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "DMX"
                ctrDMX(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "DVS"
                ctrDVS(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "DVX"
                ctrDVX(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "EAM"
                ctrEAM(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "EPR"
                ctrEPR(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "FCX"
                ctrFCX(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "MHM"
                ctrMHM(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "ONL"
                ctrONL(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "OTH"
                ctrOTH(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "POR"
                ctrPOR(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "UFF"
                ctrUFF(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
            Case "XMT"
                ctrXMT(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("credits").ToString & "' />"
        End Select

        OldCat = Cat

        ctr = ctr + 1
    Loop

    ctr = 0
    strXML = strXML & "<dataset seriesName='Asset Portal' color='8E468E' showValues='0'>" 'Asset Portal
    Do Until ctr = m_ctr
        strXML = strXML & ctrAPM(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='AMS Device' color='F6BD0F' showValues='0'>" 'AMS Device
    Do Until ctr = m_ctr
        strXML = strXML & ctrDMX(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='DeltaV SIS' color='607416' showValues='0'>" 'DeltaV SIS
    Do Until ctr = m_ctr
        strXML = strXML & ctrDVS(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='DeltaV' color='588526' showValues='0'>" 'DeltaV
    Do Until ctr = m_ctr
        strXML = strXML & ctrDVX(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='Essential Asset Monitoring' color='A3CC77' showValues='0'>" 'Essential Asset Monitoring
    Do Until ctr = m_ctr
        strXML = strXML & ctrEAM(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='EPRO' color='B3AA00' showValues='0'>" 'EPRO
    Do Until ctr = m_ctr
        strXML = strXML & ctrEPR(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='Field Communicator' color='8BBA00' showValues='0'>" 'Field Communicator
    Do Until ctr = m_ctr
        strXML = strXML & ctrFCX(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='Machinery Health' color='FF8E46' showValues='0'>" 'Machinery Health
    Do Until ctr = m_ctr
        strXML = strXML & ctrMHM(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='Online' color='D64646' showValues='0'>" 'Online
    Do Until ctr = m_ctr
        strXML = strXML & ctrONL(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='Others' color='9D080D' showValues='0'>" 'Others
    Do Until ctr = m_ctr
        strXML = strXML & ctrOTH(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='Portables' color='008E8E' showValues='0'>" 'Portables
    Do Until ctr = m_ctr
        strXML = strXML & ctrPOR(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='USB Fieldbus Interface' color='AA9CB6' showValues='0'>" 'USB Fieldbus Interface
    Do Until ctr = m_ctr
        strXML = strXML & ctrUFF(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    ctr = 0
    strXML = strXML & "<dataset seriesName='9420 Wireless Vibration Transmitter' color='008ED6' showValues='0'>" '9420 Wireless Vibration Transmitter
    Do Until ctr = m_ctr
        strXML = strXML & ctrXMT(ctr)
        ctr = ctr + 1
    Loop
    strXML = strXML & "</dataset>"

    strXML = strXML & "</chart>"

    'Create the chart - Column 3D Chart with data from strXML variable using dataXML method
    Return RenderChartHTML("Charts/NewCharts/StackedColumn3D.swf", "", strXML, "myNext", "450", "500", False)
    'for gauge
End Function

但我遇到的问题是我只在管理员访问该网站时遇到错误(即strUserCode = "ADMIN")。非管理员帐户工作得很好。我无法弄清楚出了什么问题,但我知道它在一个月前工作正常:(

1 个答案:

答案 0 :(得分:0)

我猜你的查询在以管理员身份登录时返回超过26个类别。既然你说它在一个月前工作了,这看起来就像是可能的情况。

Case Else
     strCatName = "Others" 'Unknown category

可能会帮助您更好地调试它。 另一个选项是直接使用Admin运行SQL查询,并检查您是否没有获得任何意外类别。