0

I have a SqlDataReader that reads data from the database. How can I format the phone number to return as (123) 456-7890 instead of 1234567890 on my aspx page? My reader as follow:

txtFaxPhone.Text = reader("FaxPhone").ToString()

7
  • That bit is not hard. What do you want doing with null, blank shorter numbers, longer numbers. Already formatted numbers? Already formatted numbers is a different format.. Commented Mar 7, 2013 at 18:00
  • @TonyHopkinson If null leave blank. numbers are not formatted. Just read data from the database and output to a phone number format. Thank you. Commented Mar 7, 2013 at 18:06
  • 1
    I figured it out by using: txtFaxPhone.Text = Format(PhoneFormat(reader("FaxPhone").ToString())) Commented Mar 7, 2013 at 18:36
  • Didn't even know there was a PhoneFormat. I shall remember that. Commented Mar 7, 2013 at 18:48
  • I don't think there is any PhoneFormat method,i think OP is using custom method that is already defined in their project. Commented Mar 7, 2013 at 18:56

1 Answer 1

1

Try something like this:

If reader.IsDbNull(reader.GetOrdinal("FaxPhone"))
   txtFaxPhone.Text = String.Empty
Else
   txtFaxPhone.Text = String.Format("(000) 000-0000", reader("FaxPhone"))
End If

Note: this assumes your phone number is a number. If it's a string, you'll have to substring it.

2
  • I tried your suggestion and got the following error: System.IndexOutOfRangeException: FaxNumber. How would you substring it? Commented Mar 7, 2013 at 18:56
  • Sounds like you used "FaxNumber" rather than "FaxPhone" as the field name. Which one is it? Commented Mar 7, 2013 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.