Forum Moderators: open

Message Too Old, No Replies

ASP string to number convertion

ASP string to number convertion

         

threebytes

4:08 pm on Jan 19, 2004 (gmt 0)

10+ Year Member



i have tried searching thru the board and it seem i not able to find any related thread on it so i decided to post a new one.

i have tried to add in a module to my previous written message board. i have a field in the database which have a numberic data type. and now i would like to edit the field from the form. but i seem having problem with the datatype.
it return an error saying "Type mismatch:" so perhaps i think it have something to do with the datatype. correct if i'm wrong. so how i would convert the form input to numberic instead of string. ( i currently have two page where else the 1st page will pass the form value to the next and the 2nd page would request.form to get all the input data.) thanks for helping =)

too much information

4:12 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I usually take all input as strings, then if I need a number later I convert the variable to a number and store it in a temp variable:

input_variable = "1234"
temp_variable = 1 * input_variable

So far I have had no problems.

IanTurner

4:18 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



CInt(varname)
CLng(varname)

There are other similar conversions for other numeric types but I can't remember them offhand.

threebytes

4:19 pm on Jan 19, 2004 (gmt 0)

10+ Year Member



hmm, ya i also think about to change the database field data type. but it seem alot of work to do. so instead of doing that i am seeking is there any convertion from string to numberic. i have try the cInt, but heck i not really sure how do use that. so in sense that i fail. hehe .. i not sure whether i am doing the right one or not.

threebytes

4:24 pm on Jan 19, 2004 (gmt 0)

10+ Year Member



hmm about the cInt i not really sure am i using the right way.

num1 = request.form("str")
num1 = cInt(num1)

..
.. ( database connection )

rs2("myfield") = num1

this is what i did but i still get the same error.
it further said that Type mismatch: 'cInt'
i think i might did something really stupid with the cInt

IanTurner

4:51 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The CInt function converts an expression to a variant of subtype Integer.

Note: The value must be a number between -32768 and 32767.

The CLng function converts an expression to a variant of subtype Long.

Note: The value must be a number between -2147483648 and 2147483647.

Other conversions available are CSng and CDbl

aspdaddy

4:54 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As Ian suggest only check for a valid number first.

num1="" & num1 ' avoid nulls
if isNumeric( num1 ) then.. '

threebytes

3:55 am on Jan 20, 2004 (gmt 0)

10+ Year Member



sigh, the same problem still accure and i have tried aspdaddy suggestion. it ignore the isNumberic(num1). yes i got pretty sure it is string, what i wanna to know was the way to change string to integer, (in sense that the number i entered into the form was number but the after the value passing from the form it had changed to string.)

so i would like to know is there a way to change it to numberic value back? because i having a numberic data type in the database.

txbakers

4:52 am on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



don't use the same variable name for your string value and your integer value:

dim strNum, intNum

strNum = Request("strVal")
iif (strNum <> "",intNum = cint(strNum),intnum=0)

aspdaddy

1:38 pm on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you sure iif's work in asp/vbScript :)

mattglet

3:50 pm on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



are you sure the error is occuring because of this particular problem? cint() is a very reliable function, as is isNumeric(). i have a feeling that your problem may lie elsewhere in your code.

-Matt

txbakers

3:55 pm on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you sure iif's work in asp/vbScript :)

Nope, not sure...... 8)

I use JScript for my ASP coding and use the ternary operator very often. In regular VB I use the IIF all over the place, and just assumed that it worked in VBScript.

So....I guess it doesn't?

In that case you'll have to use the long IF, Then, Else