Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Update Column By Adding A Number To Current Int

Simple enough, I can't figure out how to add (that's +) an integer from a textbox to the integer in the SQL Field.

So for example, the SQL Field may have '10' in it and the textbox may have '5' in it. I want to add these numbers together to store '15' without having to download the SQL Table.

The textbox that contains the integer to be added to the SQL integer is tranamount.Text and the SQL Column in the SQL Table is @ugpoints. Please note, without the '+' - which is in the below code and is admittedly wrong- the value of tranamount.Text is added to the Table without an issue, but it simply replaces the original value; meaning the end result would be '5' in the SQL Field.

What would be the proper way to structure this? I've tried the below code, but that clearly doesn't work.

cmd = New SqlCommand("UPDATE PersonsA SET U_G_Studio=@ugpoints WHERE Members_ID=@recevierID", con)

cmd.Parameters.AddWithValue("@recevierID", tranmemberID.Text)
cmd.Parameters.AddWithValue("@ugpoints", + tranamount.Text) '<--- Value to add.
cmd.ExecuteNonQuery()

Newbies question I know, I'm new to SQL in vb.

like image 703
user3224987 Avatar asked Feb 20 '26 08:02

user3224987


1 Answers

You have to use the correct sql:

Dim sql = "UPDATE PersonsA SET U_G_Studio=U_G_Studio + @ugpoints WHERE Members_ID=@recevierID"

Also use the correct type with AddWithValue:

Using cmd = New SqlCommand(sql, con)
    ' use the using-statement to dispose everything that implements IDisposable, so also the connection '
        cmd.Parameters.AddWithValue("@ugpoints", Int32.Parse(tranamount.Text)) 
    ' .... '
End Using
like image 133
Tim Schmelter Avatar answered Feb 21 '26 21:02

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!