2

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
user3224987
  • 83
  • 3
  • 12

4 Answers4

1

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
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

The SQL you want is:

"UPDATE PersonsA SET U_G_Studio= (U_G_Studio + @ugpoints) " & _
"WHERE Members_ID=@recevierID"
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

Take the current value of the U_G_Studio field, add the value of the parameter and reassign to U_G_Studio, but keep in mind that you need to pass the value as an integer because otherwise the AddWithValue will pass a string and you get conversion errors coming from the db.

    cmd = New SqlCommand("UPDATE PersonsA SET U_G_Studio=U_G_Studio + @ugpoints " & 
                         "WHERE Members_ID=@recevierID", con)
    cmd.Parameters.AddWithValue("@recevierID", tranmemberID.Text)
    cmd.Parameters.AddWithValue("@ugpoints", Convert.ToInt32(tranamount.Text))
    cmd.ExecuteNonQuery()
Steve
  • 213,761
  • 22
  • 232
  • 286
0

what about

cmd.Parameters.AddWithValue("@ugpoints",  (int)tranamount.Text) 
....
cmd = New SqlCommand("UPDATE PersonsA SET U_G_Studio= SET U_G_Studio + @ugpoints WHERE Members_ID=@recevierID", con)

edit1: STEVE WAS FASTER!

CodeHacker
  • 2,127
  • 1
  • 22
  • 35