I have my codes here without errors (at least not when I Debug it, I use VS 2010), but what I wish to happen is when I click on the add button, the number/s in the textbox(txtQty) would add to the numbers currently saved in the column "Quantity". (ex: txtQty "100" and current on the column is "200", I want to add "100" from the textbox to the column which has "200" and save it as a new number "300". Simple math I know, but the problem is I have yet to know how to use math equations on VB.NET Datagridview or MySql Database This Code executes but when I click Add the numbers in the column return to 0. Any Hint or Tips would be much appreciated, thanks.
cmd.Connection = conn
Dim Result = "Quantity" + txtQty.Text
cmd.CommandText = " UPDATE incomingdeliveries SET Quantity ='" & Result & "';"
cmd.Parameters.Add(New MySqlParameter("@Quantity", txtQty.Text))
cmd.ExecuteNonQuery()
MsgBox("Data Added to the Database")
Me.IncomingdeliveriesTableAdapter.Dispose()
Me.IncomingdeliveriesTableAdapter.Fill(Me.IncomingDeliveriesDataSet.incomingdeliveries)
lblCode.Text = ("Tables Refreshed!")
解决方案
your command text must be parameterized also
cmd.CommandText = " UPDATE incomingdeliveries SET Quantity = @iQuantity + Quantity"
cmd.Parameters.Add(New MySqlParameter("@iQuantity", txtQty.Text))
UPDATE 1
Using _conn As New MySqlConnection("connectionStr here")
Using _comm As New MySqlCommand()
With _comm
.Connection = _conn
.CommandText = "UPDATE incomingdeliveries SET Quantity = @iQuantity + Quantity"
.CommandType = CommandType.Text
.Parameters.AddWithValue("@iQuantity", txtQty.Text)
End With
Try
_conn.Open()
_comm.ExecuteNonQuery()
Catch ex As MySqlException
Msgbox(ex.Message.ToString())
End Try
End Using
End Using