Professional Web Applications Themes

error in aspfaq #2304?? - ASP Database

This is referring to aspfaq #2304... I've got a column of data that sometimes is null, sometimes is 0, and sometimes is some other number. I'm filling out a table with it and I don't want the 0's to show, so I wrote this function to get rid of them. I looked on aspfaq.com why my simple replace(column, "0", "") didn't work, and sure enough I found an answer :) However, when I tried the recommendation it didn't work... If I do this (basically 2304): function ReplaceZeros(column) if len(column) = 0 then column = "" else column = replace(column, "0", ...

  1. #1

    Default error in aspfaq #2304??

    This is referring to aspfaq #2304...

    I've got a column of data that sometimes is null, sometimes is 0, and
    sometimes is some other number. I'm filling out a table with it and I
    don't want the 0's to show, so I wrote this function to get rid of
    them. I looked on aspfaq.com why my simple replace(column, "0", "")
    didn't work, and sure enough I found an answer :) However, when I
    tried the recommendation it didn't work...

    If I do this (basically 2304):

    function ReplaceZeros(column)
    if len(column) = 0 then
    column = ""
    else
    column = replace(column, "0", "")
    end if
    ReplaceZeros = column
    end function

    I get this:

    Microsoft VBScript runtime (0x800A005E)
    Invalid use of Null: 'Replace'
    /tradelist_3.asp, line 100

    If I change the code to this it works fine:

    function ReplaceZeros(column)
    if isNull(column) then
    column = ""
    else
    column = replace(column, "0", "")
    end if
    ReplaceZeros = column
    end function

    -Mike
    Mike Guest

  2. #2

    Default Re: error in aspfaq #2304??

    Mike wrote: 

    The length of Null (which really means "unknown") will always evaluate to
    Null ("unknown"). You cannot know the length of an unknown piece of data.
    Since it evaluates to Null, and Null is not equal to 0, this logic fails.
    You could reverse the logic of this to:

    If len(column) > 0 then
    column = replace(column, "0", "")
    else
    column = ""
    end if

    or use IsNull as you did. The only problem with using IsNull is if you need
    to react to both a Null value and an empty string. Using IsNull would
    require this:

    If IsNull(somevariable) then
    ...
    elseif somevariable = "" then
    ... 'this may be the same code as above
    else
    ...
    end if

    Using "if len(somevariable)> 0 then" results in more concise code.

    Bob Barrows


    Bob Barrows


    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Guest

  3. #3

    Default Re: error in aspfaq #2304??

    > if len(column) = 0 then

    I had the logic reversed in the article. (Though I believe it worked in
    some previous version of VBScript, because I test all code samples before I
    post them.)

    To illustrate why you can use len>0 to determine not null, but you can't use
    len=0 to determine null,

    <%
    w = "foo"
    x = NULL
    y = ""
    ' no z

    response.write "Len(w): " & len(w) & "<br>"
    response.write "Len(x): " & len(x) & "<br>"
    response.write "Len(y): " & len(y) & "<br>"
    response.write "Len(z): " & len(z) & "<p>"

    lenwg0 = false: lenxg0 = false: lenyg0 = false: lenzg0 = false

    if len(w) > 0 then lenwg0 = true
    if len(x) > 0 then lenxg0 = true
    if len(y) > 0 then lenyg0 = true
    if len(z) > 0 then lenzg0 = true

    response.write "Len(w) > 0: " & lenwg0 & "<br>"
    response.write "Len(x) > 0: " & lenxg0 & "<br>"
    response.write "Len(y) > 0: " & lenyg0 & "<br>"
    response.write "Len(z) > 0: " & lenzg0 & "<br>"
    %>

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)


    Aaron Guest

Similar Threads

  1. Yes, aspfaq.com is down.
    By Aaron Bertrand [MVP] in forum ASP Database
    Replies: 0
    Last Post: February 18th, 06:34 AM
  2. Is ASPFAQ down?!
    By Chris Hohmann in forum ASP Database
    Replies: 4
    Last Post: February 3rd, 01:46 AM
  3. ASP.NET on ASPFAQ.com?
    By Brynn in forum ASP Database
    Replies: 2
    Last Post: January 12th, 09:46 PM
  4. About www.aspfaq.com
    By Ray at in forum ASP Components
    Replies: 3
    Last Post: December 24th, 11:43 AM
  5. Replies: 0
    Last Post: July 9th, 02:30 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139