vb.net - 索引超出了数组的范围

时间:2018-01-06 05:58:28

标签: vb.net

我在vb.net遇到了问题。顺便说一句我是新手。我需要你的帮助 这个错误有什么问题:

我的代码:

Public Sub load_stockid_monthly_byUP(ByVal type As Integer, ByVal filter As Integer, ByVal input As String)
    Dim sqlquery As String = ""
    Dim myCommand As New MySqlCommand
    Dim myData As MySqlDataReader
    Dim x As Integer = 0
    Dim where As String = ""
    Dim order As String = ""
    If filter = 1 Then
        where = ""
    ElseIf filter = 2 Then
        where = " AND r.type =" & input
    ElseIf filter = 3 Then
        where = " AND s.cat_id =" & input
    ElseIf filter = 4 Then
        where = " AND (s.desp like '% " & input & " %' OR s.desp like '" & input & " %' OR s.desp like '% " & input & "' OR s.desp = '" & input & "')"
    ElseIf filter = 5 Then
        where = " AND (s.cat_id = 1 OR s.cat_id = 2 OR s.cat_id = 4 OR s.cat_id = 5 )"
    ElseIf filter = 6 Then
        where = " AND (s.pallet_id <> 0 OR s.carton_id <> 0)"
        order = " ORDER BY  carton_id, pallet_id , stock_id"
    ElseIf filter = 7 Then
        where = " AND (s.pallet_id <> 0)"
        order = " ORDER BY pallet_id , stock_id"
    ElseIf filter = 8 Then
        where = " AND (s.carton_id <> 0)"
        order = " ORDER BY carton_id, stock_id"
    ElseIf filter = 9 Then
        where = " AND (s.uniform_id <> 0)"
        order = " ORDER BY uniform_id"
    End If


    If type = 1 Then
        sqlquery = "(SELECT s.id AS id,s.stock_id AS stock_id ,s.packing_id AS packing_id ,s.carton_id AS carton_id, s.pallet_id AS pallet_id, s.desp AS desp,i.unit_price AS unit_price, i.rate AS rate, c.type AS cat, u.unit AS uom, s.currency AS currency, s.uniform_id As uniform_id  FROM incoming AS i LEFT JOIN stock AS s ON i.stock_id = s.id LEFT JOIN uom AS u ON s.uom = u.id LEFT JOIN stock_cat AS c ON s.cat_id = c.id LEFT JOIN supplier AS r ON s.supplier_id = r.id WHERE s.active = 1 AND (i.active = 1 OR i.active = 2)" & where & " GROUP BY c.type, s.stock_id , i.unit_price, i.rate ) UNION " _
                    & "(SELECT s.id AS id,s.stock_id AS stock_id ,s.packing_id AS packing_id ,s.carton_id AS carton_id, s.pallet_id AS pallet_id,s.desp AS desp,i.unit_price AS unit_price, i.rate AS rate, c.type AS cat, u.unit AS uom, s.currency AS currency, s.uniform_id As uniform_id  FROM adjust AS i LEFT JOIN stock AS s ON i.stock_id = s.id LEFT JOIN uom AS u ON s.uom = u.id LEFT JOIN stock_cat AS c ON s.cat_id = c.id LEFT JOIN supplier AS r ON s.supplier_id = r.id WHERE s.active = 1 AND i.adjust_qty > 0 AND (i.active = 1 OR i.active = 2)" & where & " GROUP BY c.type, s.stock_id , i.unit_price, i.rate ) " & order
        ' sqlquery = "SELECT s.id AS id,s.stock_id AS stock_id ,s.desp AS desp,i.unit_price AS unit_price, i.rate AS rate, c.type AS cat, u.unit AS uom, s.currency AS currency FROM incoming AS i LEFT JOIN stock AS s ON i.stock_id = s.id LEFT JOIN uom AS u ON s.uom = u.id LEFT JOIN stock_cat AS c ON s.cat_id = c.id LEFT JOIN supplier AS r ON s.supplier_id = r.id WHERE (i.active = 1 OR i.active = 2)" & where & " GROUP BY c.type, s.stock_id , i.unit_price ORDER BY c.type ,s.stock_id "
    ElseIf type = 2 Then
        sqlquery = "SELECT s.id AS id,s.stock_id AS stock_id ,s.desp AS desp,n.unit_price AS unit_price, n.rate AS rate, c.type AS cat, u.unit AS uom, s.currency AS currency FROM issue AS i LEFT JOIN stock AS s ON i.stock_id = s.id  LEFT JOIN uom AS u ON s.uom = u.id LEFT JOIN incoming AS n ON i.irm_id = n.id LEFT JOIN stock_cat AS c ON s.cat_id = c.id LEFT JOIN supplier AS r ON s.supplier_id = r.id WHERE s.active = 1 AND i.active = 1" & where & " GROUP BY c.type, s.stock_id , n.unit_price ORDER BY c.type ,s.stock_id"
    End If


    stock_id_count = 0
    ConnectmyDB()
    myCommand.Connection = conn

    myCommand.CommandText = sqlquery
    myData = myCommand.ExecuteReader

    If myData.HasRows = False Then
        x = 1
    Else
        While myData.Read
            data(x, 0) = myData.GetInt32("id")
            data(x, 1) = myData.GetString("stock_id")
            data(x, 2) = myData.GetString("desp")
            data(x, 8) = myData.GetString("unit_price")

            data(x, 9) = myData.GetString ("rate")

            data(x, 12) = myData.GetString("cat")
            data(x, 14) = myData.GetString("uom")
            data(x, 16) = myData.GetString("packing_id")
            x = x + 1
        End While
    End If
    DisconnectDatabase()
    stock_id_count = x

End Sub 'closing stock unit price

我收到错误:

  

索引超出了该代码'data(x, 0) = myData.GetInt32("id")'

的数组范围

如何解决这个问题?

这是我在程序中运行时得到的结果:

  

**************异常文本**************
  System.IndexOutOfRangeException:索引超出了数组的范围。      at Purchasing.mdlStoreBalance.load_stockid_monthly_byUP(Int32类型,Int32过滤器,字符串输入)      at Purchasing.frmStockBalanceReport.bt_preview_Click(Object sender,EventArgs e)

谢谢

1 个答案:

答案 0 :(得分:1)

data(x,?)在哪里定义?您在data循环中填充while,但没有检查while循环是否超出定义的边界。

我也不知道你期望读取多少数据。这将影响while循环的运行时间以及预期会有多高x

(会问这个评论,但我的代表在这种形式上还不够高。)

另外,filter的检查可以在select case声明中完成。这样,您就可以一致地检查您是否已涵盖whereorder。此外,作为编码约定,命名变量与您的一个关键字(在本例中为SQL关键字)完全相同可以隐藏简单错误。将它们称为whereStmntorderStmnt可能有所帮助。