p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Classic ASP Basics (http://p2p.wrox.com/forumdisplay.php?f=61)
-   -   data mismatch while updating record (http://p2p.wrox.com/showthread.php?t=39335)

tropical March 3rd, 2006 11:17 PM

data mismatch while updating record
 
Hi there,

I'm a novice in ASP, I'm stuck in this issue:

I've an existing table "Item" with columns of "Qty" and "AveragePrice". When users add some more items on it, the code supposes to update the AveragePrice based on the newly added quantity and price.

Qty is Integer and AveragePrice is Money with lenght 8. But when I was trying to do "update" in following code, it tells me to use CAST or CONVERT to explicitly convert the data type. But I've tried with CAST(newAverage AS money) or CONVERT(money, newAverage), both got error messages. Actually the piece of code was existing before, but the AveragePrice column is what I'm trying to add.

================================
Dim Item2
set Item2 = Server.CreateObject ("ADODB.Recordset")
Item2.Open SQL, conn

Dim newQTY, oldQTY, oldAverage, newAverage
if not Item2.EOF then
  oldQTY = Item2("Qty")
  oldAverage = Item2("AveragePrice")
  newQTY = oldQTY + CLng(qty) 'qty given some time before
  newAverage = ( oldQTY * oldAverage + CLng(qty) * cost ) / ( oldQTY + CLng(qty)))

  SQL = "update Item " &_
    "set Qty = '" & newQTY & "', AveragePrice = '" & newAverage & "' " &_
    "WHERE ItemID = '" & itemID & "'"
End if
============================

I've tried to change the newAverage in the line "update Item..." to CAST(newAverage AS money) or CONVERT(money, newAverage), seems both not working.

Looking forward eagerly to some help

Thanks a lot
Tropical


Dj Kat March 7th, 2006 12:38 PM

Hi,

a quick guess is that your trying to put a string value where the type is integer.
try this
Code:

'convert string to integer
Cint(newAverage)

__________________________________________________ ________
This is my junk I'm gona eat it

mat41 March 7th, 2006 06:23 PM

If you are inserting into an integer field you dont need '" "' You need " " Eg:

SQL = "UPDATE Item SET Qty = " & cint(trim(newQTY)) & ", AveragePrice = '" & trim(newAverage) & "' WHERE ItemID = '" & itemID & "';"

Its a good habbit to use the trim function in you sql and use a trailing semi colon (as you can see above)

What data type is the field ItemID? Once agin you have '" "' if this is an integer remove the single quotes Eg:

SQL = "UPDATE Item SET Qty = " & cint(trim(newQTY)) & ", AveragePrice = '" & trim(newAverage) & "' WHERE ItemID = " & itemID & ";"



Wind is your friend
Matt

tropical March 8th, 2006 03:33 AM

Thanks DJ Kat and Mat41.

Thank you so much for your answers, I've fixed this error based on your suggestion.

Since AveragePrice's data type is money, so I should use " & newAverage & " instead of '" & newAverage & "'.


mat41 March 8th, 2006 09:02 PM

You are welcome

;;Since AveragePrice's data type is money, so I should use " & newAverage & " instead of '" & newAverage & "'.

Is that a question?

FYI: Run the statement with the " " if it doesnt work use '" "' is generally a good way to figure solve this problem

Wind is your friend
Matt

tropical March 8th, 2006 09:56 PM

No, it's not a question. That's how I fixed the error.

That's a very useful suggestion, always try " " when '""' doesn't work, because that was exactly my issue.

Thanks again, Mat41



All times are GMT -4. The time now is 06:40 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.