I have a ListView control that pulls a phone number from a db table. I'd like to format the phone number in the presentation layer to look like (###) ###-####.
The code line currently looks like this. The page displays the phone# but unformatted, i.e. 9999999999.
Based on other posts I've looked at in this forum, I've also tried:
<li>Phone: <%# String.Format("{0:(###) ###-####}", Databinder.Eval (Container.DataItem("Phone"))) %> with no success. In fact, this particular line throws an exception in my application (Input string not in the correct format).
The datatype I'm using in SQL is varchar(15)
The MSDN documentation for the String.Format method hasn't been very helpful thus far.
I should have stated in the original post that I had also tried this. This line produces the same server error as the "Databinder.Eval...." code. The error is shown below.
Maybe I should change the datatype in SQL Server? I really don't want to go that route due to the probability that other parts of the application that access this table could break.
Server Error in '/' Application.
Input string was not in a correct format.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 49: <li><%# Eval("Address2") %></li>
Line 50: <li><%# Eval("City") %> <%# Eval("State") %> <%# Eval("Zip") %></li>
Line 51: <li>Phone: <%# String.Format("{0:(###) ###-####}", Convert.ToInt64(Eval("Phone"))) %></li>
Line 52: <li>Fax: <%# Eval("Fax") %></li>
Line 53: <li>Email: <%# Eval("Email") %></li>
sycomputing
Member
82 Points
84 Posts
String.Format not working in ListView control
Aug 05, 2010 08:21 PM|LINK
I have a ListView control that pulls a phone number from a db table. I'd like to format the phone number in the presentation layer to look like (###) ###-####.
The code line currently looks like this. The page displays the phone# but unformatted, i.e. 9999999999.
<li>Phone: <%# String.Format("{0:(###) ###-####}", Eval("Phone")) %></li>Based on other posts I've looked at in this forum, I've also tried:
<li>Phone: <%# String.Format("{0:(###) ###-####}", Databinder.Eval (Container.DataItem("Phone"))) %> with no success. In fact, this particular line throws an exception in my application (Input string not in the correct format).
The datatype I'm using in SQL is varchar(15)
The MSDN documentation for the String.Format method hasn't been very helpful thus far.
Any advice is appreciated.
chapmanjw
Participant
904 Points
182 Posts
Re: String.Format not working in ListView control
Aug 05, 2010 08:43 PM|LINK
Try <%# Eval("Phone", "{0:(###) ###-####}") %>
John Chapman
-----------------------------
John Chapman's Blog
sansan
All-Star
53942 Points
8147 Posts
Re: String.Format not working in ListView control
Aug 05, 2010 08:46 PM|LINK
you have to convert that to a number before formatting.
try this way
String.Format("{0:(###) ###-####}",Convert.ToInt64(Eval("Phone")))
That works for me.
sycomputing
Member
82 Points
84 Posts
Re: String.Format not working in ListView control
Aug 05, 2010 09:09 PM|LINK
Thank you for your help, however, this had no effect. The number is still lacking any formatting.
chapmanjw
Participant
904 Points
182 Posts
Re: String.Format not working in ListView control
Aug 05, 2010 09:11 PM|LINK
Like sansan said, you may be required to convert it to a number first:
<%# Eval((int)"Phone", "{0:(###) ###-####}") %>
or
<%# Eval(Convert.ToInt64("Phone"), "{0:(###) ###-####}") %>
John Chapman
-----------------------------
John Chapman's Blog
sansan
All-Star
53942 Points
8147 Posts
Re: String.Format not working in ListView control
Aug 05, 2010 09:16 PM|LINK
try the format that I posted, shud work
sycomputing
Member
82 Points
84 Posts
Re: String.Format not working in ListView control
Aug 05, 2010 09:16 PM|LINK
Thanks for your post.
I should have stated in the original post that I had also tried this. This line produces the same server error as the "Databinder.Eval...." code. The error is shown below.
Maybe I should change the datatype in SQL Server? I really don't want to go that route due to the probability that other parts of the application that access this table could break.
Server Error in '/' Application.
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
sycomputing
Member
82 Points
84 Posts
Re: String.Format not working in ListView control
Aug 05, 2010 09:25 PM|LINK
Using either of these lines of code causes the designer to complain that "The best overloaded match for....has some invalid arguments"
The application won't build and this is the error: Error 3 Argument 1: cannot convert from 'long' to 'string' C:\Websites\ChiEpsilon\Test.aspx 51
chapmanjw
Participant
904 Points
182 Posts
Re: String.Format not working in ListView control
Aug 05, 2010 09:33 PM|LINK
Try this:
John Chapman
-----------------------------
John Chapman's Blog
sansan
All-Star
53942 Points
8147 Posts
Re: String.Format not working in ListView control
Aug 05, 2010 09:33 PM|LINK
OK, see this
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblPhone" runat="server" Text='<%# String.Format("{0:(###) ###-####}",Convert.ToInt64(Eval("Phone"))) %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>code behind:
GridView1.DataSource = new[] { new { Phone = "1234567890" } }; GridView1.DataBind();[URL=http://imagefra.me/][IMG]http://img02.imagefra.me/img/img02/8/8/5/dsanthoshece/f_silexm_48631f3.png[/IMG][/URL]
There must be something wrong with your HTML or data itself. Pls check