繁体   English   中英

如何使用vb.net减少数量

[英]how to reduce quantity using vb.net

有一个错误(mscorlib.dll中发生了类型为'System.ArgumentOutOfRangeException'的未处理异常。其他信息:索引和长度必须引用字符串中的位置。)

mycmd.Connection = myconnection.open
Dim dami As Integer = quantityt.Text
mycmd.CommandText = "Update inventory set total_quantity=total_quantity-'" & dami & "'  where item_code='" & itemcodet.Text & "'"
mycmd.ExecuteNonQuery()
MsgBox("stocks decrease!!", MsgBoxStyle.Information, "Noticed..")
myconnection.close()

我认为您对以下行有疑问

      mycmd.CommandText = "Update inventory set total_quantity=total_quantity-'" & dami & "'  where item_code='" & itemcodet.Text & "'"

应该

      mycmd.CommandText = "Update inventory set total_quantity=total_quantity-" & dami & "  where item_code='" & itemcodet.Text & "'"

用整数计数时,请勿使用撇号。。仅在字符串情况下使用撇号

我使用SQL Server提供程序进行演示。 更改为您正在使用的任何数据库。 检查数据库中字段的实际数据类型。 在最后一分钟打开连接。 请参阅我关于使用块的评论。

Private Sub OPCode()
    Using myconnection As New SqlConnection("Your connection string")
        Using mycmd As New SqlCommand("Update inventory set total_quantity = total_quantity - @dami where item_code = @itemCode;", myconnection)
            mycmd.Parameters.Add("@dami", SqlDbType.Int).Value = CInt(quantityt.Text)
            mycmd.Parameters.Add("@itemCode", SqlDbType.VarChar).Value = itemcodet.Text
            myconnection.Open()
            mycmd.ExecuteNonQuery()
        End Using
    End Using
    MsgBox("stocks decrease!!", MsgBoxStyle.Information, "Noticed..")
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM