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()
-
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– Tony Hopkinson03/07/2013 18:00:40Commented 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.g_shockTan– g_shockTan03/07/2013 18:06:06Commented Mar 7, 2013 at 18:06
-
1I figured it out by using: txtFaxPhone.Text = Format(PhoneFormat(reader("FaxPhone").ToString()))g_shockTan– g_shockTan03/07/2013 18:36:33Commented Mar 7, 2013 at 18:36
-
Didn't even know there was a PhoneFormat. I shall remember that.Tony Hopkinson– Tony Hopkinson03/07/2013 18:48:23Commented 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.rs.– rs.03/07/2013 18:56:22Commented Mar 7, 2013 at 18:56
|
Show 2 more comments
1 Answer
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.
-
I tried your suggestion and got the following error: System.IndexOutOfRangeException: FaxNumber. How would you substring it?g_shockTan– g_shockTan03/07/2013 18:56:36Commented Mar 7, 2013 at 18:56
-
Sounds like you used "FaxNumber" rather than "FaxPhone" as the field name. Which one is it?Ann L.– Ann L.03/07/2013 18:57:46Commented Mar 7, 2013 at 18:57