Formatting asp Label to a US Phone Number

803 views
Skip to first unread message

BD

unread,
Oct 12, 2010, 12:06:38 PM10/12/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Working with sql server back end that allows nulls, trying to get the
label of a US Phone Number to display in a Label on a FormView. My
page has two formviews that have phone numbers in them. My attempts
have not worked.

Mike Fry

unread,
Oct 12, 2010, 12:59:20 PM10/12/10
to dotnetde...@googlegroups.com

Telling (or showing) us what's going wrong would be a start.

--
Regards,
Mike Fry
Johannesburg

BD

unread,
Oct 12, 2010, 2:16:01 PM10/12/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I am storing phone numbers on the back end sql server database in an
unformatted nvarch type of column. I want to display the stored data
as US phone number (XXX) XXX-XXXX. I tried to do this in the asp tag
for a label and have been unable to make it work. When it did work,
it threw me an error such as:

FormatException was unhandled in user code. Input string was not
in a correct format.

I think it has to do with the column allows nulls.

BD

unread,
Oct 12, 2010, 1:41:02 PM10/12/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Sorry, the database stores the number as nvarch type without any
formatting. I want to display the number in a formatted state such as
(xxx) xxx-xxxx. I have looked for how to do this in the asp
formatting text and did get it to work, but had an issue when there
was a null value in the table. I have tried the following:

<asp:Label ID="PhoneLabel" runat="server"
Text='<%@ String.Format("{);(###) ###-####}",
Convert.ToInt64(DataBinder.Eval(Container.DataItem, "BillingPhone")))
%>' />

It gives me the following error:
FormatException was unhandled by user code. Input string was not in
a correct format.

I may be trying to do something either the hard way or in a way that
is not possible.

On Oct 12, 11:59 am, Mike Fry <mike...@iafrica.com> wrote:

Stephen Russell

unread,
Oct 12, 2010, 3:26:54 PM10/12/10
to dotnetde...@googlegroups.com
On Tue, Oct 12, 2010 at 1:16 PM, BD <bwal...@charter.net> wrote:
> I am storing phone numbers on the back end sql server database in an
> unformatted nvarch type of column.  I want to display the stored data
> as US phone number (XXX) XXX-XXXX.  I tried to do this in the asp tag
> for a label and have been unable to make it work.  When it did work,
> it threw me an error such as:
>
>     FormatException was unhandled in user code.  Input string was not
> in a correct format.
>
> I think it has to do with the column allows nulls.
---------------------------

No it has to do with data collection and nothing to do with storage.
You can fix it on the extraction easily with ISNULL(phoneNumber,
'YourStringHere' )


--
Stephen Russell

Sr. Production Systems Programmer
CIMSgts

901.246-0159 cell

Cerebrus

unread,
Oct 12, 2010, 3:34:17 PM10/12/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Close but some syntactic errors do exist in your code. Try this:

(Formatted for readability)
---
<asp:Label ID="PhoneLabel" runat="server"
Text='
<%# String.Format("{0:(###) ###-####}",
Int64.Parse(Eval("BillingPhone")))
%>
' />
---

Haven't tested this but it should be a better way to do it.

Cerebrus

unread,
Oct 12, 2010, 3:36:59 PM10/12/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
IMO, it has to do with the fact that the syntax is erroneous. The
other possibility is the fact that the number is stored as VarChar
which results in it being retrieved as a string in .NET. Strings do
not get formatted using the "#" format specifier.

BD

unread,
Oct 16, 2010, 8:04:24 AM10/16/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Sorry for taking so long to respond, work took over. I used both of
the suggestion you guys gave me and came up with the following
solution. Thanks very much for all your help!!

<asp:Label ID="PhoneLabel" runat="server"
Text='<%#
(String.IsNullOrEmpty(Convert.ToString(Eval("Phone"))))?"":String.Format("{0:
(###) ###-####}", Convert.ToInt64(Eval("Phone"))) %>' />
Reply all
Reply to author
Forward
0 new messages