Solved

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

Posted on 2004-08-16
3
197 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
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now