|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
sql / asp help
I'm trying to do an insert statement from my asp page into my sql server database. I'm reading a value from a textbox and then trying to insert it. The value is of type smallmoney. The error I get is
Disallowed implicit conversion from data type varchar to data type smallmoney So how do I convert the text in the box to smallmoney? Do I do it in asp or in sql server? Thanks |
|
#2
|
|||
|
|||
|
Quote:
You can do it in SQL Server using the CAST function, such as UPDATE MyTable Set MoneyField = CAST('1234.56' to Money) |
|
#3
|
|||
|
|||
|
Or you can use CONVERT(smallmoney, 'varchar')
|
|
#4
|
|||
|
|||
|
Thanks for you reply
I want to cast the input text from my text box to money... I've called the field "cost_price", so do I just do: UPDATE Item Set cost_price = CAST('cost_price' to smallmoney) ? I've tried this and it doesnt work... I'm assuming the cost_price in ' ' should be digits... but I don't know what the value will be because it's stored in a variable from the users input....... Please help! |
|
#5
|
|||
|
|||
|
try
UPDATE Item SET Cost_Price = CONVERT(smallmoney, 'cost_price') I swear down this works....if cost price is a varchar...! I've used it a zillion times |
|
#6
|
|||
|
|||
|
Doesn't work
My problem is that cost_price is smallmoney in the database... but when I want to update it from a text field in asp, the input is varchar..... Hence: Disallowed implicit conversion from data type varchar to data type smallmoney. I don't know if I can change the input type of the text field? I'm pretty new to all this so I don't know if the problem is with the db or with the asp page... |
|
#7
|
|||
|
|||
|
Very odd - its always worked with me - the user types in an amount to a textfield in the browser, its posted to your server side script, comes in as a string value. The database field is set to smallmoney, hence you use the CONVERT function to convert the varchar value to smallmoney, to prevent MSSQL trying to do it implicity.
I normally receive this error if I'm trying to stick a value of on datatype into a field of another datatype without manually converting it myself using CAST or CONVERT. You can't change the input type of the text field, it will always return a string, but I suppose you could try casting it as a vbLong (or whatever its called, I use PHP/Perl) in ASP before you run the query. Do you have MSSQL Enterprise manager?? If so you should have MSSQL Books On Line, have a look on this for CAST or CONVERT, it should help you further. |
|
#8
|
|||
|
|||
|
Quote:
Are you looking for something like this? Code:
Dim strMoneyVar
Dim strSQLString
strMoneyVar = Request.QueryString("Money")
' or - if using POST instead of GET
' strMoneyVar = Request.Form("Money")
strSQLString = "UPDATE Item Set cost_price = CAST('" & strMoneyVar & "' to smallmoney)"
objADOCMD.CommandText = strSQLString
objADOCMD.Execute
|
|
#9
|
|||
|
|||
|
Hi, yeah I think thats what I'm looking for. However I've tried doing it like that and I still get the same error
![]() |
|
#10
|
|||
|
|||
|
And it doesn't work with
Code:
strSQLString = "UPDATE Item Set cost_price = CONVERT(smallmoney, '" & strMoneyVar & "') WHERE Something=something" either? ??? |
|
#11
|
|||
|
|||
|
No it doesn't! It looks as though it should!
![]() |
|
#12
|
|||
|
|||
|
Hang on - I'm going to replicate it here with asp and MSSql and see what happens - as I said, I normally use PHP or Perl and this has always been ok......
|
|
#13
|
|||
|
|||
|
Sorry, I tried it here on my SQL Server using CONVERT and it works OK. I only wrote a very simple script, just one html page pointing to an asp script that connects to the database via ODBC and updates the smallmoney field with the teststring submitted via the textfield.
It then reads back the field and displays the data entered. Heres the code for the asp page: Code:
<%
'Connect to the database
Session.timeout = 15
Set myConn = Server.CreateObject("ADODB.Connection")
myConn.open "Testing","user","password"
Set Session("MyDB_conn") = myConn
Dim strMoneyVar
Dim strSQLQuery
Dim returnedMoney
strMoneyVar = Request.Form("Money")
strSQLQuery = "Update Testing SET Testing_Money=CONVERT(smallmoney, '" & strMoneyVar & "') WHERE Testing_Id=1"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQLQuery, myConn, 3, 3
strSQLQuery = "SELECT TOP 1 Testing_Money FROM Testing ORDER BY Testing_Id DESC"
rs.Open strSQLQuery, myConn, 3, 3
returnedMoney = rs("Testing_Money")
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<% Response.Write(returnedMoney) %>
<br>
<a href="test.htm">BACK </a>
</body>
</html>
<%
myConn.close
set myConn = Nothing
%>
Hope this helps in some way....?! Sorry - forgot to mention, this only works if you enter numbers in the correct format, you'd have to do some either client side (javascript) or server side checking to make sure the user enters data in the correct format before its posted. |
|
#14
|
|||
|
|||
|
Thanks for that... below is what I have which doesn't work. It looks pretty similar to what you posted so I really don't know what I'm doing wrong! Do I need to insert the cost_price in a different insert statement?
Code:
<html>
<head>
<title>Form to database</title>
</head>
<body>
<%
Dim cat_ref, descrip, date_added, cat_type, contract, cost_price, supplier_name, supplier_phone
Dim conn, SQLStmt
descrip = Request.Form("descrip")
date_added = Request.Form("date_added")
cat_type = Request.Form("cat_type")
contract = Request.Form("contract")
cost_price = Request.Form("cost_price")
supplier_name = Request.Form("supplier_name")
supplier_phone = Request.Form("supplier_phone")
SQLStmt = "INSERT into Item (descrip, date_added, cat_type, contract, cost_price, supplier_name, supplier_phone) VALUES ('" & _
descrip & "', '" & date_added & "', '" & cat_type & "', '" & contract & "', 'CONVERT(smallmoney,'" & cost_price & "')', '" & supplier_name & "', '" & supplier_phone & "')"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=catalogue; UID=sa; PWD=Joanne12!"
conn.execute(SQLStmt)
response.write "The form information was inserted successfully."
conn.Close
Set conn = Nothing
%>
</body>
</html>
|
|
#15
|
|||
|
|||
|
Ahhh - the CONVERT function is an SQL function, it doesn't need to be included in quotes at the SQL level...., only string literals do, I imagine it'd be the same with CAST.
Try this Code:
SQLStmt = "INSERT into Item (descrip, date_added, cat_type, contract, cost_price, supplier_name, supplier_phone) VALUES ('" & _
descrip & "', '" & date_added & "', '" & cat_type & "', '" & contract & "', CONVERT(smallmoney,'" & cost_price & "'), '" & supplier_name & "', '" & supplier_phone & "')"
(nb: I removed the ' ' from around the CONVERT(smallmoney,'" & cost_price & "') ) ![]() |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > sql / asp help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|