Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i'd like to deduct quantity from database but it doesn't work.

What I have tried:

this is the code i have wrote..

("UPDATE tbl_medicine SET quantity = quantity - '" & textbox1.Text & "' WHERE Item_code= '" & txtCode.Text & "'", conn)

where textbox1 is the quantity i want to deduct. (from user)
Posted
Updated 3-Aug-17 0:29am
v2
Comments

Never put direct user accessible values in your queries, you are just asking for trouble with sql injection attacks : SQL injection - Wikipedia[^]

Use SQL Parameters : Using SQLParameters with VB.NET/C#[^]

Try not putting the numeric values in single quotes :
"UPDATE tbl_medicine SET quantity = quantity - " & qvalue & " WHERE Item_code= '" & itemcode & "'"
 
Share this answer
 
Comments
Member 13343055 3-Aug-17 9:12am    
thanks for the info :)
Use parameters, and make sure the quantity input is numeric:
VB.NET
Dim quantity As Integer
If Not Integer.TryParse(textbox1.Text, quantity) Then
    ' TODO: Show an error message to the user
    Return
End If

Using con As New SqlConnection("...")
    Using cmd As New SqlCommand("UPDATE tbl_medicine SET quantity = quantity - @quantity WHERE Item_code = @ItemCode", con)
        cmd.Parameters.AddWithValue("@quantity", quantity)
        cmd.Parameters.AddWithValue("@ItemCode", txtCode.Text)
        
        connection.Open()
        cmd.ExecuteNonQuery()
    End Using
End Using
 
Share this answer
 
Comments
Member 13343055 3-Aug-17 9:12am    
it works! thanks mate! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900