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()
Asked
Active
Viewed 645 times
0

g_shockTan
- 325
- 2
- 8
- 15
-
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.. – Tony Hopkinson Mar 07 '13 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. – g_shockTan Mar 07 '13 at 18:06
-
1I figured it out by using: txtFaxPhone.Text = Format(PhoneFormat(reader("FaxPhone").ToString())) – g_shockTan Mar 07 '13 at 18:36
-
Didn't even know there was a PhoneFormat. I shall remember that. – Tony Hopkinson Mar 07 '13 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. – rs. Mar 07 '13 at 18:56
-
@TonyHopkinson The reason the PhoneFormat worked was because I added a Public Shared Function PhoneFormat. So you are correct, there is no PhoneFormat method. – g_shockTan Mar 07 '13 at 20:00
-
That's a pity, would have been handy. That would have been one of my suggestions, other would have been to do it in SQL – Tony Hopkinson Mar 07 '13 at 20:47
1 Answers
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.

Ann L.
- 13,760
- 5
- 35
- 66
-
I tried your suggestion and got the following error: System.IndexOutOfRangeException: FaxNumber. How would you substring it? – g_shockTan Mar 07 '13 at 18:56
-
Sounds like you used "FaxNumber" rather than "FaxPhone" as the field name. Which one is it? – Ann L. Mar 07 '13 at 18:57