Avatar of Chris Jones
Chris Jones
Flag for United States of America asked on

subtract value from database field with update

i have a field in my database that is an INT and i want to subtract 1 from it everytime someone registers how can i do this with a query update
ASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
Chris Jones

8/22/2022 - Mon
Jaime Olivares

You need a query string like this:

UPDATE YOURTABLE SET YOURFIELD=YOURFIELD-1 WHERE (SOME CONDITION HERE)

Do you need the vb code portion?
Chris Jones

ASKER
ok can you help me with this

why did this not subtract my value

Dim up As New SqlCommand("UPDATE CHRTR SET REMSEAT = REMSEAT-1 WHERE CourseID=@CourseID", conn)
up.Parameters.Add("@CourseID", Data.SqlDbType.Int).Value = ID
 conn.Open()
cmd.ExecuteNonQuery()
atcis

Dim sSQL as String = "UPDATE CHRTR SET REMSEAT = REMSEAT-1 WHERE CourseID=@CourseID"
cmd.CommandText = sSQL
cmd.Parameters.Add("@CourseID", Data.SqlDbType.Int).Value = ID
 conn.Open()
cmd.ExecuteNonQuery()
Your help has saved me hundreds of hours of internet surfing.
fblack61
Chris Jones

ASKER
hmmm, it still did not work
atcis

Dim cmd as New SQLCommand
Dim sSQL as String = "UPDATE CHRTR SET REMSEAT = REMSEAT-1 WHERE CourseID=@CourseID"
cmd.Connection = conn
cmd.CommandText = sSQL
cmd.Parameters.Add("@CourseID", Data.SqlDbType.Int).Value = ID
 conn.Open()
cmd.ExecuteNonQuery()
Chris Jones

ASKER
ok here is my code maybe this will help
    Protected Sub RegisterCourse(ByVal sender As Object, ByVal e As RepeaterCommandEventArgs)
        If e.CommandName = "Cancel" Then
            Dim ID As Integer = CType(e.CommandArgument, Integer)
            'Register the course here
            Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("TCPRODConnectionString3").ConnectionString)
                Dim cmd As New SqlCommand("UPDATE Registrations SET CancelledTime=getdate() WHERE CancelledDate IS NULL AND UserID=@UserID AND CourseID=@CourseID", conn)
                cmd.Parameters.Add("@UserID", Data.SqlDbType.Int).Value = Session("CWID")
                cmd.Parameters.Add("@CourseID", Data.SqlDbType.Int).Value = ID
                Dim sSQL As String = "UPDATE CHRTR SET REMSEAT = REMSEAT-1 WHERE CourseID=@CourseID"
                cmd.CommandText = sSQL
                cmd.Parameters.Add("@CourseID", Data.SqlDbType.Int).Value = ID
                conn.Open()
                cmd.ExecuteNonQuery()
                CourseTable.DataBind()
            End Using
        End If
    End Sub

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
atcis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Chris Jones

ASKER
ok i do not get an error but it only works when the user cancels a clas from my cancel link how can i make it were it will subtract the class on page load
   Protected Sub RegisterCourse(ByVal sender As Object, ByVal e As RepeaterCommandEventArgs)
        If e.CommandName = "Cancel" Then
            Dim ID As Integer = CType(e.CommandArgument, Integer)
            'Register the course here
            Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("TCPRODConnectionString3").ConnectionString)
                Dim cmd As New SqlCommand("UPDATE Registrations SET CancelledTime=getdate() WHERE CancelledDate IS NULL AND UserID=@UserID AND CourseID=@CourseID", conn)
                cmd.Parameters.Add("@UserID", Data.SqlDbType.Int).Value = Session("CWID")
                cmd.Parameters.Add("@CourseID", Data.SqlDbType.Int).Value = ID
                conn.Open()
                cmd.ExecuteNonQuery()
                cmd.Parameters.Clear()
                Dim sSQL As String = "UPDATE CHRTR SET REMSEAT = REMSEAT-1 WHERE ID=@CourseID"
                cmd.CommandText = sSQL
                cmd.Parameters.Add("@CourseID", Data.SqlDbType.Int).Value = ID
                cmd.ExecuteNonQuery()
                conn.Close()
                CourseTable.DataBind()
            End Using
        End If
    End Sub

Open in new window

Chris Jones

ASKER
ok i fixed it thanks for your help
Chris Jones

ASKER
Great answer fast respons works with the user
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes