2014-04-11 26 views
0

我們有一個ASPX站點,它顯示給定服務器捕獲到數據庫的統計信息的圖表。最近,它給了我們一個「位置0沒有排。」錯誤和「索引超出數組範圍」錯誤。當某些字段未能被捕獲填充時,我們通常會得到這個錯誤,從而給我們NULL值。但是今天,儘管沒有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)( 「月」)的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 

當我檢查了SQL結果爲「管理員」賬戶,這些都是結果:

FRT Chart Bar_Task_Cat chart

有沒有NULL值,但我在strUserCode =「ADMIN」獲得這些錯誤:(它在個人用戶中工作正常

請幫忙:(提前致謝!

回答

1

「索引超出數組範圍」錯誤與NULL數據庫值無關,它與您嘗試訪問數組長度外數組中的索引有關。需要注意的一點是數組是基於0索引的,這意味着數組中的第一項將是索引0,然後長度爲3的數組中的最後一項的索引將爲2.

使用其索引檢查訪問集合的任何位置,並確保您沒有任何代碼會超出最後一個索引。請記住,使用循環時,最後的指數將計數 - 1

在你的第一個代碼塊,讓越界異常:

Do Until ctr = m_ctr 
    ctrDMX(ctr) = "<set value='0' />" 'This line gives the error 

ctrDMX陣列只初始化爲包含最多13項,但m_ctr是基於你的SQL查詢結果#2,它有更多的值(我猜是「admin」用戶看到更多的類別?)。由於您的值將會從數據庫中動態生成,因此您可能需要根據數據庫結果查看ReDim陣列,或者切換爲使用更簡單的方法,例如List(of String),您可以撥打.Add(myString)而無需擔心指數。

對於第2個例外,你必須在表中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) 
+0

非常感謝這一點,以及爲額外的小費!是的,我認爲這是NULL值,但事實證明,他們添加了一些ProductCodes和CategoryCodes沒有通知我們:(我相應地更改了代碼:)再次感謝:) – Poch

相關問題