0

我已经将检查的值存储在一个数组中,但是当只检查一项并尝试保存它时,我收到一个错误:索引超出了数组的范围,而检查 2 个或更多值将没有错误。请帮忙。

Dim count as Integer
Dim ListedItems As New ArrayList()
for count = 0 To (CLB1.Items.Count - 1)
    ListItems.Add(CLB1.GetItemText(CLB1.CheckedItems(count)).ToString)
    query("Insert Into accnts (accnts.IDNo, accnts.PayName) Values ('" & IDTB.Text &"','" & ListItems(count) & "')
    MsgBox("Record Saved",MsgBoxStyle.OkOnly)
Next
4

1 回答 1

0

Try taversing only the checked item and not all the items in checkbox list like:

For Each indexChecked In CLB1.CheckedIndices 
    ListItems.Add(CLB1.Items(indexChecked).ToString)
    query("Insert Into accnts (accnts.IDNo, accnts.PayName) Values ('" & IDTB.Text &"','" & ListItems(ListItems.Count-1) & "')
    MsgBox("Record Saved",MsgBoxStyle.OkOnly)
Next

By the way your query is susceptible to sql injection try using parameters to prevent one link here

于 2013-07-04T15:35:33.527 回答