Reputation: 389
We have an ASPX site that displays charts given stats captured by a server onto a database. Recently, it's giving us a "There is no row at position 0." error and "Index was outside the bounds of the array. " error. We usually get this error when some fields failed to be filled by the capture, thus giving us NULL values. But today, it's giving us these errors despite not having NULL values.
Here are two functions that give us the error:
"Index was outside the bounds of the array" (at this line: 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
"There is no row at position 0" (at this line: 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
When I checked the SQL results for the "Admin" account, these are the results:
There are no NULL values, yet I get those errors in strUserCode = "ADMIN" :( it works fine in Individual users
Please help :( Thanks in advance!
Upvotes: 0
Views: 346
Reputation: 6496
An "Index was outside the bounds of the array" error has nothing to do with NULL database values, it has to do with you trying to access an index in an array that is outside the array length. One thing to keep in mind is that arrays are 0 index based, which means that the first item in the array will be index 0, and then the last item in an array with length of 3 will have an index of 2.
Check anywhere that is accessing a collection using its index, and makes sure that you don't have any code that will step outside of that last index. Keep in mind that when using loops, the last index will be the Count - 1.
In your first code block that gives the out of bounds exception:
Do Until ctr = m_ctr
ctrDMX(ctr) = "<set value='0' />" 'This line gives the error
The ctrDMX
array is only initialized to contain a max of 13 items, but m_ctr
is set based on your SQL query results #2 which has many more values (my guess is "admin" user sees more categories?). Since your values that are going into this array are dynamically generated from the database, you may either have to look at ReDim
'ing your arrays based on the DB results, or just switch to use something easier like a List(of String)
where you can call .Add(myString)
without worrying about the indexes.
For the 2nd exception, you have 0 rows in the table when you are getting to the line:
OldMonth = DS.Tables(0).Rows(0)("Month").ToString 'This line gives the error
so check the make sure you have at least one row before accessing:
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
One more very important issue that was not in your original question, do NOT build your SQL strings by concatenating string together. This creates a large SQL Injection vulnerability on your website. Use SQL Parameters for your filter values. Instead of having things like "WHERE (FirstResponse BETWEEN '" & dpFrom.SelectedDate.ToString & "' AND '" & dpTo.SelectedDate.ToString & "') "
in your SQL String, change it to "WHERE (FirstResponse BETWEEN @dateFrom AND @dateTo) "
and then add parameter values to your 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)
Upvotes: 1