SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


AddWithValue issue - Update the current field content ASP.Net/SQL


AddWithValue issue - Update the current field content ASP.Net/SQL

Author
Message
Joe Mizrahi
Joe Mizrahi
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 12
I have an ASP.Net VB shopping cart app where when an item is "Removed" from a shopping cart, and the corresponding Database field "NewTotal" is updated with the amount removed. So, that for example, if NewTotal is currently (5) and the amount being returned to inventory is QtyToAddBack (3), then the NEW "NewTotal" amount should be (8)

I am using the following code. If I use SQL Server Management Studio to run the query, it performs as expected; but in the code page it does not... what am I doing wrong.. I suspect the issue is with the "AddWithValue" definition?

Thank you in advance!

Dim strSQL02 As String = "Update [ClinicTest2].[dbo].[ICDbS_Products] SET "& _
"NewTotal = (NewTotal +'" & QtyToAddBack & "') " & _
"WHERE ProductNoID = '" & ItemNumber & "'"

Dim myCommand02 As New SqlCommand(strSQL02, objConn02)

myCommand02.Parameters.AddWithValue("@NewTotal", "@NewTotal"+ QtyToAddBack)
jimbobmcgee
jimbobmcgee
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 Visits: 916
You are mixing up the SQL with the VB and confusing yourself. Not to mention, introducing a massive SQL-injection hole, by building up the string in that fashion.

Instead, define @variables within the SQL string and use AddWithValue to populate them.
For example:


Dim strSQL02 As String = "Update [ClinicTest2].[dbo].[ICDbS_Products] SET " & _
"NewTotal = (NewTotal + @qty_to_add) " & _
"WHERE ProductNoID = @prod_id"

Dim myCommand02 As New SqlCommand(strSQL02, objConn02)
myCommand02.Parameters.AddWithValue("@qty_to_add", QtyToAddBack)
myCommand02.Parameters.AddWithValue("@prod_id", ItemNumber)



You should also look into separating your SQL code from your VB code, preferably in the form of stored procedures but possibly with a separate class of SQL strings. For best results, the SQL strings should be constants.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63170 Visits: 17961
Honestly I think you are doing this the hard way. You should not store the total of your cart, you should instead calculate it based on the items in your cart. Then all you need to store is a CartID, ItemNumber, Quantity, Price. The total is the sum of Quantity * Price. Your code becomes a lot simpler and so does your data. Now all you have to do is add/remove items from the cart and rebind your display.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search