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