VB.NET中IndexOutOfRangeException报错排查求助(新手)
Hey there, let's break down why you're hitting that IndexOutOfRangeException and how to fix it. The core issue is pretty clear: your data array isn't sized correctly to handle the number of rows your SQL query returns, or it might not even be initialized at all before you start assigning values to it. When x increments past the array's predefined length, you get the out-of-bounds error at data(x, 0) = myData.GetInt32("id").
Recommended Fix: Use a Dynamic Collection
Fixed-size arrays are rigid and risky here—you can never be sure exactly how many rows your query will return. Switching to a List(Of Object()) lets you add rows dynamically without worrying about size limits. Here's how to adjust your code:
Public Sub load_stockid_monthly_byUP(ByVal type As Integer, ByVal filter As Integer, ByVal input As String) ' ... keep your existing SQL building logic here ... stock_id_count = 0 ConnectmyDB() myCommand.Connection = conn myCommand.CommandText = sqlquery myData = myCommand.ExecuteReader ' Replace fixed array with a dynamic list Dim dataList As New List(Of Object()) Dim x As Integer = 0 If myData.HasRows Then While myData.Read ' Initialize a row array with enough columns (matches your max index 16) Dim rowData As Object() = New Object(16) {} rowData(0) = myData.GetInt32("id") rowData(1) = myData.GetString("stock_id") rowData(2) = myData.GetString("desp") rowData(8) = myData.GetString("unit_price") rowData(9) = myData.GetString("rate") rowData(12) = myData.GetString("cat") rowData(14) = myData.GetString("uom") rowData(16) = myData.GetString("packing_id") dataList.Add(rowData) x += 1 End While End If ' If you still need a fixed array later, convert the list: Dim data As Object()() = dataList.ToArray() DisconnectDatabase() stock_id_count = x End Sub
Alternative Fix: Pre-Size the Array with Row Count
If you must stick with a fixed array, first load your results into a DataTable to get the exact row count, then initialize the array to match that size:
Public Sub load_stockid_monthly_byUP(ByVal type As Integer, ByVal filter As Integer, ByVal input As String) ' ... keep your existing SQL building logic here ... stock_id_count = 0 ConnectmyDB() myCommand.Connection = conn myCommand.CommandText = sqlquery ' Load results into a DataTable to get row count Dim dt As New DataTable() dt.Load(myCommand.ExecuteReader()) ' Initialize array with exact row count Dim data As Object()() = New Object(dt.Rows.Count - 1)() {} Dim x As Integer = 0 For Each row As DataRow In dt.Rows data(x) = New Object(16) {} data(x)(0) = row("id") data(x)(1) = row("stock_id") data(x)(2) = row("desp") data(x)(8) = row("unit_price") data(x)(9) = row("rate") data(x)(12) = row("cat") data(x)(14) = row("uom") data(x)(16) = row("packing_id") x += 1 Next DisconnectDatabase() stock_id_count = x End Sub
Critical Security Note: Fix SQL Injection Risk
I noticed you're directly concatenating user input (input variable) into your SQL query. This is a massive security vulnerability—attackers could inject malicious SQL to steal or modify your data. Replace all string concatenations with parameterized queries. For example:
Instead of this:
where = " AND r.type =" & input
Do this:
where = " AND r.type = @Input" myCommand.Parameters.AddWithValue("@Input", input)
Repeat this pattern for every instance where you use input in your SQL clauses to keep your code safe.
内容的提问来源于stack exchange,提问作者Neqia

