Reputation: 21
I have two columns in my table (ID
and Auto
); when I click the button 1+, I want to increase the value in column auto
.
Dim cmd As New SqlCommand
cmd.Connection = cn
cmd.CommandText = ("UPDATE Table_8 SET ID = @ID,Auto = @Auto WHERE ID = @ID")
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = "1"
cmd.Parameters.Add("@Auto+1", SqlDbType.VarChar).Value = +1
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
Upvotes: 0
Views: 281
Reputation: 987
You don't need second parameter @Auto You can simply do it in Query
And the second thing is, why is the auto column in your DB VARCHAR
it may need to INT
if you want to perform Increment like operation on It.
I recommend you to change otherwise you need to convert that value in integer to perform addition.
Please try below code after change column:
Dim cmd As New SqlCommand
cmd.Connection = cn
cmd.CommandText = ("UPDATE Table_8 SET Auto = Auto+1 WHERE ID = @ID")
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = "1"
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
Or:
cmd.CommandText = ("UPDATE Table_8 SET Auto =CAST((CAST(Auto AS INT)+1) AS VARCHAR(your size)) WHERE ID = @ID")
Upvotes: 3