Solved

How is this handling a null....and how can I fix it?

Posted on 2004-08-16
3
233 Views
Last Modified: 2010-04-07
I am borrowing this function that was created in VBscript/ASP

Private Function MkPhoneNum(byVal number)
      Dim tmp
      number = CStr( number )
      number = Trim( number )
      number = Replace( number, " ", "" )      
      number = Replace( number, "-", "" )
      number = Replace( number, "(", "" )
      number = Replace( number, ")", "" )
      Select Case Len( number )
            Case 7
                  tmp = tmp & Mid( number, 1, 3 ) & "-"
                  tmp = tmp & Mid( number, 4, 4 )
            Case 10
                  tmp = tmp & "(" & Mid( number, 1, 3 ) & ") "
                  tmp = tmp & Mid( number, 4, 3 ) & "-"
                  tmp = tmp & Mid( number, 7, 4 )
            Case 11
                  tmp = tmp & Mid( number, 1, 1 ) & " "
                  tmp = tmp & "(" & Mid( number, 2, 3 ) & ") "
                  tmp = tmp & Mid( number, 5, 3 ) & "-"
                  tmp = tmp & Mid( number, 8, 4 )
            Case Else
                  MkPhoneNum = Null
                  Exit Function
      End Select
      MkPhoneNum = tmp
End Function


However, when I pass a null field through it, it results in the following error:
      

Microsoft VBScript runtime error '800a005e'

Invalid use of Null: 'CStr'

on line 6...which is the line where it defines
number = CStr( number )

any suggestions for how to fix this to just leave null cases blank????

Thanks!
0
Comment
Question by:htillberg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 11812038
Use IsDbNull to find if the value that is passed is null:

Example:
Dim MyVar As Object
Dim MyCheck As Boolean
MyCheck = IsDBNull(MyVar)   ' Returns False.
MyVar = ""
MyCheck = IsDBNull(MyVar)   ' Returns False.
MyVar = System.DBNull.Value
MyCheck = IsDBNull(MyVar)   ' Returns True.
MsgBox(MyCheck)


---------------------------------------------------------
Private Function MkPhoneNum(byVal number)
    if IsDbNull(number) then
       throw ArgumentNullException("number","The parameter number cannot be null")
    else

     Dim tmp
     number = CStr( number )
     number = Trim( number )
     number = Replace( number, " ", "" )    
     number = Replace( number, "-", "" )
     number = Replace( number, "(", "" )
     number = Replace( number, ")", "" )
     Select Case Len( number )
          Case 7
               tmp = tmp & Mid( number, 1, 3 ) & "-"
               tmp = tmp & Mid( number, 4, 4 )
          Case 10
               tmp = tmp & "(" & Mid( number, 1, 3 ) & ") "
               tmp = tmp & Mid( number, 4, 3 ) & "-"
               tmp = tmp & Mid( number, 7, 4 )
          Case 11
               tmp = tmp & Mid( number, 1, 1 ) & " "
               tmp = tmp & "(" & Mid( number, 2, 3 ) & ") "
               tmp = tmp & Mid( number, 5, 3 ) & "-"
               tmp = tmp & Mid( number, 8, 4 )
          Case Else
               MkPhoneNum = Null
               Exit Function
     End Select
     MkPhoneNum = tmp
 end if
End Function

HTH, nauman
0
 

Accepted Solution

by:
tdkh earned 250 total points
ID: 11815543
I would just change the syntax of line 6 to the following...

number = "" & number

The result of a concatination operation is a string.  This should work if number has a value or is null.
0
 

Author Comment

by:htillberg
ID: 11821414
Nice suggestions. The new code by nauman still kept returning the same error though. However, modifying the number line did the trick. Thanks so much!@!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question