Adding String Values in Classic ASP

Results 1 to 6 of 6

Thread: Adding String Values in Classic ASP

  1. #1
    Join Date
    Dec 1969
    Posts
    17

    Default Adding String Values in Classic ASP

    Please be gentle! Newbie to ASP! Am trying to add multiple form fields together to get a sum of the values:<BR><BR>strFormFieldResult = strFormField1 + strFormField2<BR><BR>Assume both fields hold the value "100". The result displayed is "100100", not "200". <BR><BR>Could someone please tell me what I am doing incorrectly, or point me to an article that explains how to sum two string values inside ASP?<BR><BR>Thanks! :o)

  2. #2
    Join Date
    Dec 1969
    Posts
    6,476

    Default RE: Adding String Values in Classic ASP

    When you get the form values via the "Request Object"<BR>ie: someString = Request.Form("SomeForm")<BR><BR>The request object will always return a "String variant", no matter what was entered.<BR><BR>So it will be your job to convert the string to the "Proper" datatype you want to work with.<BR><BR>someString = CInt( Request.Form("SomeFormField") )<BR><BR>now the datatype is INT<BR>CLng() converts to long datatype...<BR>CDate() converts to Date datatype<BR><BR>the list goes on.<BR><BR>Go to this page and book mark it right now.<BR>It has all the built in vbs functions() that can be used.<BR><BR>Official Docs.<BR>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vbscripttoc.asp<BR><BR>VBS Functions.<BR>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vtorifunctions.asp

  3. #3
    Join Date
    Dec 1969
    Posts
    17

    Default RE: Adding String Values in Classic ASP

    Thank you so much, Russell! I have two great new bookmarks (thanks to you) and will attempt to convert the string during the request. Cross your fingers for me! :o)

  4. #4
    Join Date
    Dec 1969
    Posts
    297

    Default RE: Adding String Values in Classic ASP

    I&#039;ve run into this some times when you are adding rows that are &#039;text&#039; you&#039;ll have to do something like (strFormField1*1) + (strFormField*1) = x


  5. #5
    Join Date
    Dec 1969
    Posts
    6,476

    Default The only reason that works

    Is because sometimes VBS figures out what your trying to do and converts the value to be a numerical data type...<BR><BR>Now here is where your work around Blows up.<BR><BR>Suppose your formfield has a (string value) of 10<BR>10 is entered in a form<BR><BR>strNumber = request.Form("Testnumber")<BR>ok strNumber is a string value<BR><BR>strTest = (strNumber*1) + 20<BR>vbs sees the multiplier * and then (key word here) "tries" to convert the string to a numerical datatype so that it will work....<BR><BR>Remember that key word "tries"? what happens if it fails?<BR><BR>Ahh, test it and see for your self.<BR><BR>Enter this into that same form field 1a0<BR>(Or enter any non numerical number)<BR><BR>strNumber = request.Form("Testnumber")<BR>strTest = (strNumber*1) + 20<BR><BR>Now your get an error crashing your whole page...<BR>Not too good huh?<BR><BR>Always test and confirm then convert to datatype as needed.<BR><BR>IF isNumeric(Request.Form("FormField") then<BR> we can convert this to a valid numerical datatype<BR>Else<BR> &#039;not a valid number entered, so redirect them back<BR>end if...<BR><BR>More detailed check would be needed of course but this should point out the flaws with what your doing.<BR>

  6. #6
    Join Date
    Dec 1969
    Posts
    17

    Default RE: The only reason that works

    Russell, thanks so much! Your suggestion worked and my form is adding superbly! Thanks again and this is a great forum! :o)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •