<?xml version="1.0" encoding="utf-8"?><!-- generator="wordpress/2.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Formatting a phone number in ASP.NET data bound controls</title>
	<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/</link>
	<description>The scratchpad, the holding area, the ranting place</description>
	<pubDate>Thu, 28 Aug 2008 01:33:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1</generator>

	<item>
		<title>By: harsha</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-50</link>
		<author>harsha</author>
		<pubDate>Thu, 08 Dec 2005 20:45:55 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-50</guid>
					<description>rice one..it would rather do all my formating on client side...</description>
		<content:encoded><![CDATA[<p>rice one..it would rather do all my formating on client side&#8230;</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: harsha</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-51</link>
		<author>harsha</author>
		<pubDate>Thu, 08 Dec 2005 20:48:02 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-51</guid>
					<description>What i really meant was that I would my formating using javascript/vbscript ...:)</description>
		<content:encoded><![CDATA[<p>What i really meant was that I would my formating using javascript/vbscript &#8230;:)</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Luke</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-52</link>
		<author>Luke</author>
		<pubDate>Thu, 08 Dec 2005 21:00:15 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-52</guid>
					<description>Yeah, but the idea here is to do less work, not more.  Do you have a URL with an example of formatting a phone number via javascript?  It sounds like overkill to me...

Luke</description>
		<content:encoded><![CDATA[<p>Yeah, but the idea here is to do less work, not more.  Do you have a URL with an example of formatting a phone number via javascript?  It sounds like overkill to me&#8230;</p>
<p>Luke</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Kelly</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-58</link>
		<author>Kelly</author>
		<pubDate>Tue, 14 Mar 2006 20:12:44 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-58</guid>
					<description>Luke, you are the man. I've been trying to use formatting in item templates with the old VBScript formatting functions and sometimes they just don't work.  It never occurred to me that I could use the String.Format Static method in the HTML.  Thanks</description>
		<content:encoded><![CDATA[<p>Luke, you are the man. I&#8217;ve been trying to use formatting in item templates with the old VBScript formatting functions and sometimes they just don&#8217;t work.  It never occurred to me that I could use the String.Format Static method in the HTML.  Thanks</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Luke</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-59</link>
		<author>Luke</author>
		<pubDate>Tue, 14 Mar 2006 21:49:49 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-59</guid>
					<description>Funny you should mention that today, Kelly, I just did a weird bit of data binding this morning that reminded me of this post!  I should get off my butt and write more about this stuff, it's the only way I'll remember it...</description>
		<content:encoded><![CDATA[<p>Funny you should mention that today, Kelly, I just did a weird bit of data binding this morning that reminded me of this post!  I should get off my butt and write more about this stuff, it&#8217;s the only way I&#8217;ll remember it&#8230;</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: jeaux</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-1961</link>
		<author>jeaux</author>
		<pubDate>Mon, 16 Oct 2006 18:22:26 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-1961</guid>
					<description>DataBinder.Eval(Container.DataItem, "Phone_Number","{0:(###) ###-####}")</description>
		<content:encoded><![CDATA[<p>DataBinder.Eval(Container.DataItem, &#8220;Phone_Number&#8221;,&#8221;{0:(###) ###-####}&#8221;)</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-2005</link>
		<author>Jason</author>
		<pubDate>Fri, 27 Oct 2006 14:43:05 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-2005</guid>
					<description>Jeaux, that won\'t work - the var is defined as a string, so the formatting is pretty much ignored, as far as I can tell - you need to convert it to a number first.  I had a lovely example page made up, but WP doesn\'t want to let me add it to the comments, so you\'ll just have to trust me, or try it for yourself.</description>
		<content:encoded><![CDATA[<p>Jeaux, that won\&#8217;t work - the var is defined as a string, so the formatting is pretty much ignored, as far as I can tell - you need to convert it to a number first.  I had a lovely example page made up, but WP doesn\&#8217;t want to let me add it to the comments, so you\&#8217;ll just have to trust me, or try it for yourself.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Victor Escobar</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-8223</link>
		<author>Victor Escobar</author>
		<pubDate>Sun, 20 May 2007 22:44:11 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-8223</guid>
					<description>When I used your code, I got the following error:

Object cannot be cast from DBNull to other types. 
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.InvalidCastException: Object cannot be cast from DBNull to other types.

Source Error: 


Line 40: 				HeaderText="Phone Number" SortExpression="strPhoneNumber"&#62;
Line 41: 					
Line 42: 						
Line 43: 					
Line 44: 					
 

Source File: C:\inetpub\XXX\YYY\ZZZ.aspx    Line: 42 

Stack Trace: 


[InvalidCastException: Object cannot be cast from DBNull to other types.]
   System.DBNull.System.IConvertible.ToInt64(IFormatProvider provider) +54
   System.Convert.ToInt64(Object value) +25
   ASP.admin_admin_evaluator_aspx.__DataBind__control16(Object sender, EventArgs e) in C:\inetpub\XXX\YYY\ZZZ.aspx:42
   System.Web.UI.Control.OnDataBinding(EventArgs e) +99
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +206
   System.Web.UI.Control.DataBind() +12
   System.Web.UI.Control.DataBindChildren() +216
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +216
   System.Web.UI.Control.DataBind() +12
   System.Web.UI.Control.DataBindChildren() +216
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +216
   System.Web.UI.Control.DataBind() +12
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +221
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3004
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +59
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +11
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +111
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +29
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
   System.Web.UI.WebControls.GridView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +69
   System.Web.UI.Control.EnsureChildControls() +87
   System.Web.UI.Control.PreRenderRecursiveInternal() +41
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210</description>
		<content:encoded><![CDATA[<p>When I used your code, I got the following error:</p>
<p>Object cannot be cast from DBNull to other types.<br />
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. </p>
<p>Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.</p>
<p>Source Error: </p>
<p>Line 40: 				HeaderText=&#8221;Phone Number&#8221; SortExpression=&#8221;strPhoneNumber&#8221;&gt;<br />
Line 41:<br />
Line 42:<br />
Line 43:<br />
Line 44: 					</p>
<p>Source File: C:\inetpub\XXX\YYY\ZZZ.aspx    Line: 42 </p>
<p>Stack Trace: </p>
<p>[InvalidCastException: Object cannot be cast from DBNull to other types.]<br />
   System.DBNull.System.IConvertible.ToInt64(IFormatProvider provider) +54<br />
   System.Convert.ToInt64(Object value) +25<br />
   ASP.admin_admin_evaluator_aspx.__DataBind__control16(Object sender, EventArgs e) in C:\inetpub\XXX\YYY\ZZZ.aspx:42<br />
   System.Web.UI.Control.OnDataBinding(EventArgs e) +99<br />
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +206<br />
   System.Web.UI.Control.DataBind() +12<br />
   System.Web.UI.Control.DataBindChildren() +216<br />
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +216<br />
   System.Web.UI.Control.DataBind() +12<br />
   System.Web.UI.Control.DataBindChildren() +216<br />
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +216<br />
   System.Web.UI.Control.DataBind() +12<br />
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +221<br />
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3004<br />
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +59<br />
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +11<br />
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +111<br />
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +29<br />
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149<br />
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70<br />
   System.Web.UI.WebControls.GridView.DataBind() +4<br />
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82<br />
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +69<br />
   System.Web.UI.Control.EnsureChildControls() +87<br />
   System.Web.UI.Control.PreRenderRecursiveInternal() +41<br />
   System.Web.UI.Control.PreRenderRecursiveInternal() +161<br />
   System.Web.UI.Control.PreRenderRecursiveInternal() +161<br />
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-8300</link>
		<author>Jason</author>
		<pubDate>Tue, 22 May 2007 16:02:34 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-8300</guid>
					<description>Victor, can you email me the source file so I can see what else is going on?  I'll try to put an update on demos tonight...</description>
		<content:encoded><![CDATA[<p>Victor, can you email me the source file so I can see what else is going on?  I&#8217;ll try to put an update on demos tonight&#8230;</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Kathleen Anderson</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-10076</link>
		<author>Kathleen Anderson</author>
		<pubDate>Sun, 08 Jul 2007 02:00:23 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-10076</guid>
					<description>Thanks for this - I have been Googling for the last 24 hours for something like this. I did find your syntax was off just a tad - this is what worked for me: 

'</description>
		<content:encoded><![CDATA[<p>Thanks for this - I have been Googling for the last 24 hours for something like this. I did find your syntax was off just a tad - this is what worked for me: </p>
<p>&#8216;</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Kathleen Anderson</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-10077</link>
		<author>Kathleen Anderson</author>
		<pubDate>Sun, 08 Jul 2007 02:05:39 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-10077</guid>
					<description>String.Format("{0:(###) ###-####}", Convert.ToInt64(DataBinder.Eval(Container.DataItem,"homephone")))</description>
		<content:encoded><![CDATA[<p>String.Format(&#8221;{0:(###) ###-####}&#8221;, Convert.ToInt64(DataBinder.Eval(Container.DataItem,&#8221;homephone&#8221;)))</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Cetin Basoz</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-12222</link>
		<author>Cetin Basoz</author>
		<pubDate>Sat, 18 Aug 2007 15:29:22 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-12222</guid>
					<description>Pardon me but telephone numbers are not made up of digits all the time. Wouldn't something like this cause a parsing error?

800VERIZON

or (formatted):

+1 (555) 1234567 [1234]
+1 555 1234567 - 1234
+1 555 123 45 67 - 1234
00 1 (555) 1234567
444 0 MSI

etc.

Maybe storing it as end users wrote is the way. People are accustomed to type in such data in the format they are familiar most.
To me it's neither the job of data layer nor presentation. Humans can easily interpret those numbers.</description>
		<content:encoded><![CDATA[<p>Pardon me but telephone numbers are not made up of digits all the time. Wouldn&#8217;t something like this cause a parsing error?</p>
<p>800VERIZON</p>
<p>or (formatted):</p>
<p>+1 (555) 1234567 [1234]<br />
+1 555 1234567 - 1234<br />
+1 555 123 45 67 - 1234<br />
00 1 (555) 1234567<br />
444 0 MSI</p>
<p>etc.</p>
<p>Maybe storing it as end users wrote is the way. People are accustomed to type in such data in the format they are familiar most.<br />
To me it&#8217;s neither the job of data layer nor presentation. Humans can easily interpret those numbers.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: ASP.NET Applications</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-17643</link>
		<author>ASP.NET Applications</author>
		<pubDate>Tue, 09 Oct 2007 08:02:35 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-17643</guid>
					<description>Good info. But I agree with the Harsh- I prefer the client side to do all the ugly work...</description>
		<content:encoded><![CDATA[<p>Good info. But I agree with the Harsh- I prefer the client side to do all the ugly work&#8230;</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: ASP.NET Applications</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-19902</link>
		<author>ASP.NET Applications</author>
		<pubDate>Wed, 24 Oct 2007 12:09:07 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-19902</guid>
					<description>Great post
I agree with Cetin Basoz.
I believe that storing it as the users do-  is the way.</description>
		<content:encoded><![CDATA[<p>Great post<br />
I agree with Cetin Basoz.<br />
I believe that storing it as the users do-  is the way.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Jeremy</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-24697</link>
		<author>Jeremy</author>
		<pubDate>Wed, 28 Nov 2007 17:57:21 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-24697</guid>
					<description>Actually, it would be best to parse the digits out when accepting the data using regex. Storing various formats of the same style is a nightmare to maintain.  Taking a we'll-just-deal-with-whatever-they-input approach will eventually cause a lot of problems (especially on the security side).</description>
		<content:encoded><![CDATA[<p>Actually, it would be best to parse the digits out when accepting the data using regex. Storing various formats of the same style is a nightmare to maintain.  Taking a we&#8217;ll-just-deal-with-whatever-they-input approach will eventually cause a lot of problems (especially on the security side).</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Haniel Croitoru</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-26252</link>
		<author>Haniel Croitoru</author>
		<pubDate>Wed, 26 Dec 2007 04:43:39 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-26252</guid>
					<description>Hi all,

This post is very useful.  One additional question I have on this is, how do I deal with the situation where the phone number may be blank?  The browser throws an exception at that point.</description>
		<content:encoded><![CDATA[<p>Hi all,</p>
<p>This post is very useful.  One additional question I have on this is, how do I deal with the situation where the phone number may be blank?  The browser throws an exception at that point.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Andy</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-26421</link>
		<author>Andy</author>
		<pubDate>Fri, 28 Dec 2007 20:59:49 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-26421</guid>
					<description>public static string FormatPhoneNumber(string pNum)
		{
			pNum = RemoveNonIntegers(pNum);
			if (pNum.Length == 10)
				return String.Format("{0:(###) ###-####}", Int64.Parse(pNum));
			else if (pNum.Length == 7)
				return String.Format("{0:###-####}", Int64.Parse(pNum));
			else return "Invalid string format";
		}</description>
		<content:encoded><![CDATA[<p>public static string FormatPhoneNumber(string pNum)<br />
		{<br />
			pNum = RemoveNonIntegers(pNum);<br />
			if (pNum.Length == 10)<br />
				return String.Format(&#8221;{0:(###) ###-####}&#8221;, Int64.Parse(pNum));<br />
			else if (pNum.Length == 7)<br />
				return String.Format(&#8221;{0:###-####}&#8221;, Int64.Parse(pNum));<br />
			else return &#8220;Invalid string format&#8221;;<br />
		}</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-26663</link>
		<author>Jason</author>
		<pubDate>Tue, 01 Jan 2008 16:42:15 +0000</pubDate>
		<guid>http://www.thrustlabs.com/blog/2005/12/08/formatting-a-phone-number-in-aspnet-data-bound-controls/#comment-26663</guid>
					<description>Haniel, there's not a lot of error handling (i.e. none) in this technique - a helper function like Andy provided would be a better approach here.</description>
		<content:encoded><![CDATA[<p>Haniel, there&#8217;s not a lot of error handling (i.e. none) in this technique - a helper function like Andy provided would be a better approach here.</p>
]]></content:encoded>
				</item>
</channel>
</rss>
