VB.net更新数据表错误索引超出了数组的范围

时间:2014-08-14 12:47:08

标签: vb.net updates

我正在尝试更新" qnty" ds.Tables(0)中特定行的列,但我不知道为什么会出现这个问题,即使" qnty"在ds.Table(0)存在?

"索引超出了数组的范围。"

我使用VS 2012,VB.NET

Dim Minus_qnty As String
Dim Row() As Data.DataRow
Row = ds.Tables(0).Select("Prodno = '" & ProdItem_No & "'")                 
Dim ds_qnty As String = ds.Tables(0).Rows(0)("qnty").ToString()
Minus_qnty = ds_qnty + Prod_taken_qnty
Row(0)("qnty") = Minus_qnty 'The problem appear in this line

2 个答案:

答案 0 :(得分:1)

更改最后一行以检查行长度

If Row.Length > 0 Then Row(0)("qnty") = Minus_qnty 

答案 1 :(得分:0)

错误是因为DataTable中没有与您的条件Prodno = ProdItem_No匹配的记录。

我认为这就是你想要做的事情:

    Dim Minus_qnty As String
    Dim Row() As Data.DataRow
    Row = ds.Tables(0).Select("Prodno = '" & ProdItem_No & "'")
    If Row.Length > 0 Then
        Dim ds_qnty As String = Row(0)("qnty").ToString()
        Minus_qnty = ds_qnty + Prod_taken_qnty
        Row(0)("qnty") = Minus_qnty
    End If

或者只是这个:

    Dim Row() As Data.DataRow = ds.Tables(0).Select("Prodno = '" & ProdItem_No & "'")
    If Row.Length > 0 Then
        Row(0)("qnty") = Row(0)("qnty") + Prod_taken_qnty
    End If