我们有一个ASPX站点,显示由服务器捕获到数据库的统计数据的图表。最近,它给了我们一个“位置0没有排”。错误和“索引超出了数组的范围。”错误。当某些字段无法通过捕获填充时,我们通常会收到此错误,从而为我们提供NULL值。但是今天,它给了我们这些错误,尽管没有NULL值。
以下是两个给我们错误的函数:
“索引超出了数组的范围”(在此行: ctrDMX(ctr)=“”)
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 (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 (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)
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 ctrDMX(12), ctrMHM(12), ctrFCX(12), ctrAPM(12), ctrPOR(12), ctrONL(12), ctrDVX(12), ctrOTH(12) As String
'counters for Categories
'Dim ctrOL(12), ctrAR(12), ctrPR(12), ctrST(12), ctrPQ(12), ctrFW(12), ctrOO(12) As String
Dim ctrAR(12), ctrDA(12), ctrDB(12), ctrDC(12), ctrDD(12), ctrDE(12), ctrDF(12), ctrDG(12), ctrDH(12), ctrDI(12), ctrDJ(12), ctrDK(12) As String
Dim ctrDL(12), ctrDM(12), ctrDN(12), ctrDO(12), ctrDP(12), ctrDQ(12), ctrFW(12), ctrOL(12), ctrOO(12), ctrPR(12), ctrPQ(12), ctrST(12) 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"
Case "DA"
strCatName = "DA"
Case "DB"
strCatName = "DB"
Case "DC"
strCatName = "DC"
Case "DD"
strCatName = "DD"
Case "DE"
strCatName = "DE"
Case "DF"
strCatName = "DF"
Case "DG"
strCatName = "DG"
Case "DH"
strCatName = "DH"
Case "DI"
strCatName = "DI"
Case "DJ"
strCatName = "DJ"
Case "DK"
strCatName = "DK"
Case "DL"
strCatName = "DL"
Case "DM"
strCatName = "DM"
Case "DN"
strCatName = "DN"
Case "DO"
strCatName = "DO"
Case "DP"
strCatName = "DP"
Case "DQ"
strCatName = "DQ"
Case "FW"
strCatName = "FW"
Case "OL"
strCatName = "OL"
Case "OO"
strCatName = "OO"
Case "PR"
strCatName = "PR"
Case "PQ"
strCatName = "PQ"
Case "ST"
strCatName = "ST"
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
ctrDMX(ctr) = "<set value='0' />" 'This line gives the error
ctrFCX(ctr) = "<set value='0' />"
ctrMHM(ctr) = "<set value='0' />"
ctrPOR(ctr) = "<set value='0' />"
ctrONL(ctr) = "<set value='0' />"
ctrAPM(ctr) = "<set value='0' />"
ctrDVX(ctr) = "<set value='0' />"
ctrOTH(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 "DMX"
ctrDMX(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 "POR"
ctrPOR(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 "APM"
ctrAPM(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 "OTH"
ctrOTH(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='AMS Device' color='AFD8F8' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrDMX(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Field Communicator' color='8BBA00' showValues='0'>" 'Documentation
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='F6BD0F' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrMHM(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Portables' color='AFD8F8' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrPOR(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Online' color='A186BE' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrONL(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Asset Portal' color='FF8E46' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrAPM(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='DeltaV' color='9D080D' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrDVX(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Others' color='008E8E' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctrOTH(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
“位置0没有行”(在此行: OldMonth = DS.Tables(0).Rows(0)(“Month”)。ToString )
Public Function FRTChart() 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(FirstResponse) AS TotalQueries, SUBSTRING(CONVERT(VARCHAR(11), FirstResponse, 113), 4, 8) AS Month, RIGHT(CONVERT(VARCHAR(7), FirstResponse , 120), 2) AS Month_Order, CAST(YEAR(FirstResponse) AS VARCHAR(4)) AS Year, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) <= 2 THEN 1 ELSE 0 END) AS LessThan2Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 3 AND 24 THEN 1 ELSE 0 END) AS LessThan24Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 25 AND 48 THEN 1 ELSE 0 END) AS LessThan48Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 49 AND 72 THEN 1 ELSE 0 END) AS LessThan72Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 73 AND 96 THEN 1 ELSE 0 END) AS LessThan96Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) > 96 THEN 1 ELSE 0 END) AS Over96Hours " _
& "FROM [AskAO].dbo.AskAO_Stats " _
& "WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') " _
& "AND (Status = 'CLOSED' OR Status = 'ACTIVE') " _
& "AND (FirstResponse IS NOT NULL) " _
& "GROUP BY SUBSTRING(CONVERT(VARCHAR(11), FirstResponse , 113), 4, 8), CONVERT(VARCHAR(7), FirstResponse , 120), CAST(YEAR(FirstResponse) AS VARCHAR(4)) " _
& "ORDER BY Year, Month_Order"
Else
SQLstring = "SELECT Count(FirstResponse) AS TotalQueries, SUBSTRING(CONVERT(VARCHAR(11), FirstResponse, 113), 4, 8) AS Month, RIGHT(CONVERT(VARCHAR(7), FirstResponse , 120), 2) AS Month_Order, CAST(YEAR(FirstResponse) AS VARCHAR(4)) AS Year, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) <= 2 THEN 1 ELSE 0 END) AS LessThan2Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 3 AND 24 THEN 1 ELSE 0 END) AS LessThan24Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 25 AND 48 THEN 1 ELSE 0 END) AS LessThan48Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 49 AND 72 THEN 1 ELSE 0 END) AS LessThan72Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) between 73 AND 96 THEN 1 ELSE 0 END) AS LessThan96Hours, " _
& "SUM(CASE WHEN DATEDIFF(HOUR, Date_Sent, FirstResponse) > 96 THEN 1 ELSE 0 END) AS Over96Hours " _
& "FROM [AskAO].dbo.AskAO_Stats " _
& "WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') " _
& "AND (Status = 'CLOSED' OR Status = 'ACTIVE') AND (Deferred = '" & strUserCode & "') " _
& "AND (FirstResponse IS NOT NULL) " _
& "GROUP BY SUBSTRING(CONVERT(VARCHAR(11), FirstResponse , 113), 4, 8), CONVERT(VARCHAR(7), FirstResponse , 120), CAST(YEAR(FirstResponse) AS VARCHAR(4)) " _
& "ORDER BY Year, Month_Order"
End If
Dim SQLcmd As SqlCommand = New SqlCommand(SQLstring, Conn)
Dim DA As New SqlDataAdapter(SQLstring, Conn)
Dim DS As New DataSet
DA.Fill(DS)
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 CatMonth As String
Dim OldMonth As String
Dim ctr2(12), ctr24(12), ctr48(12), ctr72(12), ctr96(12), ctro96(12) As String
strXML = ""
CatMonth = ""
OldMonth = ""
strXML = strXML & "<chart palette='2' legendPosition='RIGHT' labelDisplay='Rotate' slantLabels='1' showBorder='0' bgColor='FFFFFF,FFFFFF' caption='First Response Time' shownames='1' showvalues='0' showSum='1' decimals='0' useRoundEdges='1' showPercentValues='1' showPercentInToolTip='1'>"
strXML = strXML & "<categories>"
Do Until ctr = DS.Tables(0).Rows.Count
CatMonth = DS.Tables(0).Rows(ctr)("Month").ToString
If CatMonth <> OldMonth Then
strXML = strXML & "<category label='" & DS.Tables(0).Rows(ctr)("Month").ToString & "' value='" & DS.Tables(0).Rows(ctr)("TotalQueries").ToString & "' />"
m_ctr = m_ctr + 1
End If
OldMonth = CatMonth
ctr = ctr + 1
Loop
strXML = strXML & "</categories>"
'initialize
ctr = 0
Do Until ctr = m_ctr
ctr2(ctr) = "<set value='0' />"
ctr24(ctr) = "<set value='0' />"
ctr48(ctr) = "<set value='0' />"
ctr72(ctr) = "<set value='0' />"
ctr96(ctr) = "<set value='0' />"
ctro96(ctr) = "<set value='0' />"
ctr = ctr + 1
Loop
ctr = 0
CatMonth = ""
OldMonth = DS.Tables(0).Rows(0)("Month").ToString 'This line gives the error
Do Until ctr = DS.Tables(0).Rows.Count
'ProdCode = DS.Tables(0).Rows(ctr)("ProductCode").ToString
'respTime = DS.Tables(0).Rows(ctr)("ResponseTime")
CatMonth = DS.Tables(0).Rows(ctr)("Month").ToString
If CatMonth <> OldMonth Then
m1_ctr = m1_ctr + 1
End If
ctr2(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("LessThan2Hours").ToString & "' />"
ctr24(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("LessThan24Hours").ToString & "' />"
ctr48(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("LessThan48Hours").ToString & "' />"
ctr72(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("LessThan72Hours").ToString & "' />"
ctr96(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("LessThan96Hours").ToString & "' />"
ctro96(m1_ctr) = "<set value='" & DS.Tables(0).Rows(ctr)("Over96Hours").ToString & "' />"
OldMonth = CatMonth
ctr = ctr + 1
Loop
ctr = 0
strXML = strXML & "<dataset seriesName='2 Hrs' color='AFD8F8' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctr2(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='24 Hrs' color='8BBA00' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctr24(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='24 - 48 Hrs' color='F6BD0F' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctr48(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='48 - 72 Hrs' color='008E8E' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctr72(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='72 - 96 Hrs' color='A186BE' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctr96(ctr)
ctr = ctr + 1
Loop
strXML = strXML & "</dataset>"
ctr = 0
strXML = strXML & "<dataset seriesName='Over 96 Hrs' color='FF8E46' showValues='0'>" 'Documentation
Do Until ctr = m_ctr
strXML = strXML & ctro96(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", "370", False)
'for gauge
End Function
当我检查“Admin”帐户的SQL结果时,结果如下:
没有NULL值,但我在strUserCode =“ADMIN”中得到了这些错误:(它在个人用户中工作正常
请帮助:(提前致谢!
答案 0 :(得分:1)
&#34;索引超出了数组的范围&#34;错误与NULL数据库值无关,它与您尝试访问数组长度之外的数组中的索引有关。要记住的一件事是数组是基于0索引的,这意味着数组中的第一项将是索引0,然后长度为3的数组中的最后一项将具有索引2.
检查使用其索引访问集合的任何位置,并确保您没有任何代码将超出最后一个索引。请记住,使用循环时,最后一个索引将是Count - 1.
在第一个给出越界异常的代码块中:
Do Until ctr = m_ctr
ctrDMX(ctr) = "<set value='0' />" 'This line gives the error
ctrDMX
数组仅初始化为最多包含13个项目,但m_ctr
是根据您的SQL查询结果#2设置的,其中包含更多值(我猜是&#34; admin&#34;用户看到更多类别?)。由于进入此数组的值是从数据库动态生成的,因此您可能必须根据数据库结果查看ReDim
数组,或者只是切换到使用更简单的数据库List(of String)
您可以在其中调用.Add(myString)
而无需担心索引。
对于第二个例外,当您到达该行时,表格中有0行:
OldMonth = DS.Tables(0).Rows(0)("Month").ToString 'This line gives the error
所以请检查以确保在访问之前至少有一行:
If DS.Tables(0).Rows(0) >= 1 Then
OldMonth = DS.Tables(0).Rows(0)("Month").ToString 'This line gives the error
Else
OldMonth = String.Empty
End If
在原始问题中没有的另一个非常重要的问题,不要通过将字符串连接在一起来构建SQL字符串。这会在您的网站上创建一个大型SQL注入漏洞。使用SQL参数作为过滤器值。不要在SQL字符串中添加"WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') "
之类的内容,而是将其更改为"WHERE (FirstResponse BETWEEN @dateFrom AND @dateTo) "
然后将参数值添加到SQLCommand:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx
Dim SQLcmd As SqlCommand = New SqlCommand(SQLstring, Conn)
SQLcmd.Parameters.AddWithValue("@dateFrom", dpFrom.SelectedDate)
SQLcmd.Parameters.AddWithValue("@dateTo", dpTo.SelectedDate)