Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-16
3
Medium Priority
?
246 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 1000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 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