Format Phone - Anyone? Arithmetic operation resulted in an overflow.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vertigo262
    New Member
    • Jul 2007
    • 62

    Format Phone - Anyone? Arithmetic operation resulted in an overflow.

    Anyone know why this does this?

    Server Error in '/' Application.
    Arithmetic operation resulted in an overflow.


    Code:
     
    <asp:Label ID="Label4" runat="server" Font-Size="Medium" Text='<%# GetPhoneFormat(Eval("Phone")) %>' />
    
    
       Function GetPhoneFormat(ByVal Phone As String) As String
    
            Dim PhoneFormat As String
            If IsDBNull(Phone) Or IsNothing(Phone) Then
                PhoneFormat = " "
            Else
               
                Phone = CInt(Phone).ToString("(000) 000-0000")
                PhoneFormat = "<b>Phone:</b> " & Phone
    
            End If
    
    
            Return PhoneFormat
        End Function
  • danp129
    Recognized Expert Contributor
    • Jul 2006
    • 323

    #2
    The overflow is from using an integer to hold too big of a number. Use a long or do something like this:
    Code:
            ' Imports System.Text.RegularExpressions
            Dim cleanNumber As String = Regex.Replace(Phone, "[^0-9]", "")
            Dim PhoneFormat As String = "<b>Phone:</b> " & Regex.Replace(cleanNumber, "(\d{3})(\d{3})(\d{4})", "($1) $2-$3")

    Comment

    • vertigo262
      New Member
      • Jul 2007
      • 62

      #3
      that's a little different. But works great.

      the funny thing with the old code was that if I created a string with a phone number it worked. But if the data came from the database it would throw that error. Your code seems a lot more efficient. That's much!

      Comment

      Working...