<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Useful scripts &#124; php, asp, Dot Net, Java, Perl, JavaScript and other Basic and Advanced scripts</title>
	<atom:link href="http://usefulscripts.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://usefulscripts.wordpress.com</link>
	<description>the scripts which are found rarely on the internet but needed often</description>
	<lastBuildDate>Tue, 28 Oct 2008 10:13:59 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='usefulscripts.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/4f253242b0f11e6ef38580c9b254bc7d?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Useful scripts &#124; php, asp, Dot Net, Java, Perl, JavaScript and other Basic and Advanced scripts</title>
		<link>http://usefulscripts.wordpress.com</link>
	</image>
			<item>
		<title>Difference Between two dates in Javascript</title>
		<link>http://usefulscripts.wordpress.com/2008/10/28/difference-between-two-dats-javascript/</link>
		<comments>http://usefulscripts.wordpress.com/2008/10/28/difference-between-two-dats-javascript/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 10:13:59 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[Basic Scripts]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[check date]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[date difference]]></category>
		<category><![CDATA[getTime]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[setTime]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=85</guid>
		<description><![CDATA[Hello.
Some times we came around a problem where we needed to know the difference between 2 dates in javascript. Also, in some cases we need to validate a date against another date. In these cases we search in the internet for a suitable function which takes 2 dates as arguments and finally let us know [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&blog=1889208&post=85&subd=usefulscripts&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Hello.</p>
<p>Some times we came around a problem where we needed to know the difference between 2 dates in javascript. Also, in some cases we need to validate a date against another date. In these cases we search in the internet for a suitable function which takes 2 dates as arguments and finally let us know the difference between those.</p>
<p>Here, I have found so many scripts, but some of which are not working and some I do not understand and some does not provide us a simple result as we want. So, I have taken reference of those scripts and marged them into a suitable one to produce a new script which is giving us output as I wanted it to do.</p>
<p>Here it is:</p>
<p>First, we need a html form and 2 input type text fileds to enter 2 dates. Here some may wanted to add the time also.</p>
<p>&lt;form onSubmit=&#8221;return checkDate(this);&#8221;&gt;<br />
&lt;table&gt;<br />
 &lt;tr&gt;<br />
  &lt;td&gt;<br />
   &lt;pre&gt;<br />
   First Date:&lt;input type=&#8221;text&#8221; name=&#8221;firstdate&#8221; value=&#8221;" size=10 maxlength=10&gt; (MM/DD/YYYY format)&lt;br/&gt;<br />
   Time: &lt;input type=&#8221;text&#8221; name=&#8221;firsttime&#8221; value=&#8221;" size=10 maxlength=10&gt; (HH:MM:SS format)&lt;br/&gt;<br />
   <br />
   Second Date: Date: &lt;input type=&#8221;text&#8221; name=&#8221;seconddate&#8221; value=&#8221;" size=10 maxlength=10&gt; (MM/DD/YYYY format)&lt;br/&gt;<br />
   Time: &lt;input type=&#8221;text&#8221; name=&#8221;secondtime&#8221; value=&#8221;" size=10 maxlength=10&gt; (HH:MM:SS format)&lt;br/&gt;<br />
   <br />
   &lt;center&gt;&lt;input type=&#8221;submit&#8221; value=&#8221;Get Difference!&#8221;&gt;&lt;/center&gt;<br />
   &lt;/pre&gt;<br />
  &lt;/td&gt;<br />
 &lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;/form&gt;</p>
<p>So, this form has 2 text fileds for dates and 2 other for times, and a button to get the difference.</p>
<p>This form takes date in &#8220;MM/DD/YYYY&#8221; format and time in &#8220;HH:MM:SS&#8221; format.</p>
<p>We can add a function to validate these dates or we can simply specify the date text fileds as readonly and add a suitable javascript calendar to input correct dates. (Like I have used in : <a href="http://www.hotelsagorika.com/reservation.html">http://www.hotelsagorika.com/reservation.html</a>)</p>
<p>Now after clicking the submit button we need to add suitable code to check the dates:</p>
<p>So, we will put this script into the HEAD section of the HTML.</p>
<p>&lt;script type=&#8221;text/javascript&#8221;&gt;</p>
<p>function isValidDate(dateStr) {<br />
 // Date validation Function <br />
 // Checks For the following valid Date formats:<br />
 // MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY<br />
 <br />
 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit Year<br />
 <br />
 var matchArray = dateStr.match(datePat); // Is the format ok?<br />
 if (matchArray == Null) {<br />
  alert(dateStr + &#8221; Date is not in a valid format.&#8221;)<br />
  return false;<br />
 }<br />
 <br />
 Month = matchArray[1]; // parse Date into variables<br />
 Day = matchArray[3];<br />
 Year = matchArray[4];<br />
 <br />
 if (Month &lt; 1 || Month &gt; 12) { // check Month range<br />
  alert(&#8220;Month must be between 1 and 12.&#8221;);<br />
  return false;<br />
 }<br />
 <br />
 if (Day &lt; 1 || Day &gt; 31) {<br />
  alert(&#8220;Day must be between 1 and 31.&#8221;);<br />
  return false;<br />
 }<br />
 <br />
 if ((Month==4 || Month==6 || Month==9 || Month==11) &amp;&amp; Day==31) {<br />
  alert(&#8220;Month &#8220;+Month+&#8221; doesn&#8217;t have 31 days!&#8221;)<br />
  return false;<br />
 }<br />
 <br />
 if (Month == 2) { // check For february 29th<br />
  var isleap = (Year % 4 == 0 &amp;&amp; (Year % 100 != 0 || Year % 400 == 0));<br />
  if (Day&gt;29 || (Day==29 &amp;&amp; !isleap)) {<br />
   alert(&#8220;February &#8221; + Year + &#8221; doesn&#8217;t have &#8221; + Day + &#8221; days!&#8221;);<br />
   return false;<br />
  }<br />
 }<br />
 return true;<br />
}</p>
<p>function isValidTime(timeStr) {<br />
 // Checks if time Is In HH:MM:SS AM/PM format.<br />
 // The seconds And AM/PM are optional.<br />
 <br />
 var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;<br />
 <br />
 var matchArray = timeStr.match(timePat);<br />
 if (matchArray == Null) {<br />
  alert(&#8220;Time is not in a valid format.&#8221;);<br />
  return false;<br />
 }<br />
 <br />
 Hour = matchArray[1];<br />
 Minute = matchArray[2];<br />
 Second = matchArray[4];<br />
 ampm = matchArray[6];<br />
 <br />
 if (Second==&#8221;") { Second = null; }<br />
 if (ampm==&#8221;") { ampm = Null }<br />
 <br />
 if (Hour &lt; 0 || Hour &gt; 23) {<br />
  alert(&#8220;Hour must be between 1 and 12. (or 0 and 23 for military time)&#8221;);<br />
  return false;<br />
 }<br />
 if (Hour &lt;= 12 &amp;&amp; ampm == Null) {<br />
  if (confirm(&#8220;Please indicate which time format you are using. OK = Standard Time, CANCEL = Military Time&#8221;)) {<br />
   alert(&#8220;You must specify AM or PM.&#8221;);<br />
   return false;<br />
  }<br />
 }<br />
 if (Hour &gt; 12 &amp;&amp; ampm != Null) {<br />
  alert(&#8220;You can&#8217;t specify AM or PM for military time.&#8221;);<br />
  return false;<br />
 }<br />
 if (Minute &lt; 0 || Minute &gt; 59) {<br />
  alert (&#8220;Minute must be between 0 and 59.&#8221;);<br />
  return false;<br />
 }<br />
 if (Second != Null &amp;&amp; (Second &lt; 0 || Second &gt; 59)) {<br />
  alert (&#8220;Second must be between 0 and 59.&#8221;);<br />
  return false;<br />
 }<br />
 return true;<br />
}</p>
<p>function checkDate(dateform) {<br />
 date1 = new Date();<br />
 date2 = new Date();<br />
 diff = new Date();<br />
 <br />
 if (isValidDate(dateform.firstdate.value) &amp;&amp; isValidTime(dateform.firsttime.value)) { // Validates first Date<br />
  date1temp = new Date(dateform.firstdate.value + &#8221; &#8221; + dateform.firsttime.value);<br />
  date1.setTime(date1temp.getTime());<br />
 }<br />
 else return false; // otherwise exits<br />
 <br />
 if (isValidDate(dateform.seconddate.value) &amp;&amp; isValidTime(dateform.secondtime.value)) { // Validates Second Date<br />
  date2temp = new Date(dateform.seconddate.value + &#8221; &#8221; + dateform.secondtime.value);<br />
  date2.setTime(date2temp.getTime());<br />
 }<br />
 else return false; // otherwise exits<br />
 <br />
 // sets difference Date To difference of first Date And Second Date<br />
 <br />
 diff.setTime(Math.abs(date1.getTime() &#8211; date2.getTime()));<br />
 <br />
 timediff = diff.getTime();<br />
 <br />
 weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));<br />
 timediff -= weeks * (1000 * 60 * 60 * 24 * 7);<br />
 <br />
 days = Math.floor(timediff / (1000 * 60 * 60 * 24));<br />
 timediff -= days * (1000 * 60 * 60 * 24);<br />
 <br />
 hours = Math.floor(timediff / (1000 * 60 * 60));<br />
 timediff -= hours * (1000 * 60 * 60);<br />
 <br />
 mins = Math.floor(timediff / (1000 * 60));<br />
 timediff -= mins * (1000 * 60);<br />
 <br />
 secs = Math.floor(timediff / 1000);<br />
 timediff -= secs * 1000;<br />
 <br />
 alert(&#8220;Difference = &#8221; + weeks + &#8221; weeks, &#8221; + days + &#8221; days, &#8221; + hours + &#8221; hours, &#8221; + mins + &#8221; minutes, and &#8221; + secs + &#8221; seconds&#8221;);</p>
<p> return false; // form should never submit, returns False<br />
}</p>
<p>&lt;/script&gt;</p>
<p>If you have specified the text fileds as read only and added a calendar, then you do not need the validattion methods, then the script will look like:</p>
<p>&lt;script type=&#8221;text/javascript&#8221;&gt;</p>
<p>function checkDate(dateform) {<br />
 date1 = new Date();<br />
 date2 = new Date();<br />
 diff = new Date();<br />
 <br />
 date1temp = new Date(dateform.firstdate.value + &#8221; &#8221; + dateform.firsttime.value);</p>
<p>//If you do not need the time then you can replace the above line with this<br />
// date1temp = new Date(dateform.firstdate.value + &#8221; 00:00:00&#8243;);</p>
<p> date1.setTime(date1temp.getTime());<br />
  <br />
 date2temp = new Date(dateform.seconddate.value + &#8221; &#8221; + dateform.secondtime.value);</p>
<p>//If you do not need the time then you can replace the above line with this<br />
// date2temp = new Date(dateform.seconddate.value + &#8221; 00:00:00&#8243;);</p>
<p> date2.setTime(date2temp.getTime());<br />
 <br />
 // sets difference Date To difference of first Date And Second Date<br />
 <br />
 diff.setTime(Math.abs(date1.getTime() &#8211; date2.getTime()));<br />
 <br />
 timediff = diff.getTime();<br />
 <br />
 weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));<br />
 timediff -= weeks * (1000 * 60 * 60 * 24 * 7);<br />
 <br />
 days = Math.floor(timediff / (1000 * 60 * 60 * 24));<br />
 timediff -= days * (1000 * 60 * 60 * 24);<br />
 <br />
 hours = Math.floor(timediff / (1000 * 60 * 60));<br />
 timediff -= hours * (1000 * 60 * 60);<br />
 <br />
 mins = Math.floor(timediff / (1000 * 60));<br />
 timediff -= mins * (1000 * 60);<br />
 <br />
 secs = Math.floor(timediff / 1000);<br />
 timediff -= secs * 1000;<br />
 <br />
 alert(&#8220;Difference = &#8221; + weeks + &#8221; weeks, &#8221; + days + &#8221; days, &#8221; + hours + &#8221; hours, &#8221; + mins + &#8221; minutes, and &#8221; + secs + &#8221; seconds&#8221;);</p>
<p> return false; // form should never submit, returns False<br />
}</p>
<p>&lt;/script&gt;</p>
<p>Now, suppose we want to know if the second date is after the first date or vice versa, i.e. if the first date is 30 th October, 2008 and seconda date is 02 nd November, 2008 then it will accept the dates otherwise, like if the 1st date is 28 th October 2008 and the second is 25th October 2008, it will not accept.</p>
<p>So, wee need to modify the above function slightly:</p>
<p>function checkDate(dateform) {<br />
 date1 = new Date();<br />
 date2 = new Date();<br />
 diff = new Date();<br />
 <br />
 if (isValidDate(dateform.firstdate.value) &amp;&amp; isValidTime(dateform.firsttime.value)) { // Validates first Date<br />
  date1temp = new Date(dateform.firstdate.value + &#8221; &#8221; + dateform.firsttime.value);<br />
  date1.setTime(date1temp.getTime());<br />
 }<br />
 else return false; // otherwise exits<br />
 <br />
 if (isValidDate(dateform.seconddate.value) &amp;&amp; isValidTime(dateform.secondtime.value)) { // Validates Second Date<br />
  date2temp = new Date(dateform.seconddate.value + &#8221; &#8221; + dateform.secondtime.value);<br />
  date2.setTime(date2temp.getTime());<br />
 }<br />
 else return false; // otherwise exits<br />
 <br />
 // sets difference Date To difference of first Date And Second Date<br />
 <br />
 var dif = date2-date1;<br />
 <br />
 if(dif &gt;=0){ // 2nd date is after the 1st date<br />
  return true;<br />
 }else{<br />
  return false;<br />
 }</p>
<p>}</p>
<p>Hope this article will help you understanding the date related issues better.</p>
Posted in Basic Scripts, javascript, Scripts Tagged: check date, date, date difference, getTime, javascript, parsing, setTime <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/85/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&blog=1889208&post=85&subd=usefulscripts&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2008/10/28/difference-between-two-dats-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>Integer Parsing in JavaScript</title>
		<link>http://usefulscripts.wordpress.com/2008/10/02/integer-parsing-in-javascript/</link>
		<comments>http://usefulscripts.wordpress.com/2008/10/02/integer-parsing-in-javascript/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 11:48:41 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[Basic Scripts]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[digit]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[isdigit]]></category>
		<category><![CDATA[isempty]]></category>
		<category><![CDATA[isinteger]]></category>
		<category><![CDATA[isNaN]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[parse integer]]></category>
		<category><![CDATA[parseInt]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[tryparse]]></category>
		<category><![CDATA[undefined]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=80</guid>
		<description><![CDATA[Integer Parsing in JavaScript. Some utility methods and tips
First Question will be like: How do I convert strings to numbers in JavaScript?
Answer: To convert a string to a number, use the JavaScript function parseFloat (for conversion to a floating-point number) or parseInt (for conversion to an integer). 
parseFloat syntax: parseFloat('string') 
How it works:

The argument of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&blog=1889208&post=80&subd=usefulscripts&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h1>Integer Parsing in JavaScript. Some utility methods and tips</h1>
<p class="MsoNormal"><strong><span style="font-family:Verdana;">First Question will be like:</span></strong><span style="font-family:Verdana;"> How do I convert strings to numbers in JavaScript?</span></p>
<p><strong><span style="font-family:Verdana;">Answer:</span></strong><span style="font-family:Verdana;"> To convert a string to a number, use the JavaScript function </span><code><span style="text-decoration:underline;"><span style="color:#0000ff;font-family:Verdana;">parseFloat</span></span></code><span style="font-family:Verdana;"> (for conversion to a floating-point number) or </span><code><span style="text-decoration:underline;"><span style="color:#0000ff;font-family:Verdana;">parseInt</span></span></code><span style="font-family:Verdana;"> (for conversion to an integer). </span></p>
<p><strong><em><span style="font-family:Verdana;">parseFloat</span></em></strong><em><span style="font-family:Verdana;"> syntax:</span></em><span style="font-family:Verdana;"> </span><code><span style="font-family:Verdana;">parseFloat('string')</span></code><span style="font-family:Verdana;"> </span></p>
<p style="line-height:125%;"><strong><em><span style="font-family:Verdana;">How it works:<br />
</span></em></strong><span style="font-family:Verdana;"><br />
The argument of </span><code><span style="font-family:Verdana;">parseFloat</span></code><span style="font-family:Verdana;"> must be a string or a string expression. The result of </span><code><span style="font-family:Verdana;">parseFloat</span></code><span style="font-family:Verdana;"> is the number whose decimal representation was contained in that string (or the number found in the beginning of the string). If the string argument cannot be parsed as a decimal number, the results will be different in different browsers (either </span><code><span style="font-family:Verdana;">0</span></code><span style="font-family:Verdana;"> or </span><code><strong><span style="font-family:Verdana;">NaN</span></strong></code><span style="font-family:Verdana;">). </span></p>
<p><em><span style="font-family:Verdana;">Examples</span></em><span style="font-family:Verdana;"> (comments in each line give the conversion results): </span></p>
<p><span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8216;1.45kg&#8217;)</strong><span>  </span>// 1.45</span><br />
<span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8216;77.3&#8242;)</strong><span>    </span>// 77.3</span><br />
<span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8216;077.3&#8242;)</strong><span>   </span>// 77.3</span><br />
<span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8216;0&#215;77.3&#8242;)</strong><span>  </span>// 0</span><br />
<span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8216;.3&#8242;)</strong><span>      </span>// 0.3</span><br />
<span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8216;0.1e6&#8242;)</strong><span>   </span>// 100000 </span>
</p>
<p style="margin:0 0 .0001pt;"><strong><em><span style="font-family:Verdana;">parseInt</span></em></strong><em><span style="font-family:Verdana;"> syntax:</span></em><span style="font-family:Verdana;"> </span><code><span style="font-family:Verdana;">parseInt( 'string' [, base] ) </span></code></p>
<p style="margin:0 0 .0001pt;"><strong><em><span style="font-family:Verdana;">How it works:</span></em></strong></p>
<p style="line-height:125%;margin:0 0 .0001pt;"><span style="font-family:Verdana;">The first argument of </span><code><span style="font-family:Verdana;">parseInt</span></code><span style="font-family:Verdana;"> must be a string or a string expression. The result returned by </span><code><span style="font-family:Verdana;">parseInt</span></code><span style="font-family:Verdana;"> is an <em>integer</em> whose representation was contained in that string (or the integer found in the beginning of the string). The second argument (</span><code><span style="font-family:Verdana;">base</span></code><span style="font-family:Verdana;">), if present, specifies the base (radix) of the number whose string representation is contained in the </span><code><span style="font-family:Verdana;">string</span></code><span style="font-family:Verdana;">. The </span><code><span style="font-family:Verdana;">base</span></code><span style="font-family:Verdana;"> argument can be any integer from 2 to 36. </span></p>
<p style="line-height:125%;margin:0 0 .0001pt;"> </p>
<p style="line-height:125%;margin:0 0 .0001pt;"><span style="font-family:Verdana;">If there is only one argument, the number base is detected according to the general JavaScript syntax for numbers. Strings that begin with </span><code><span style="font-family:Verdana;">0x</span></code><span style="font-family:Verdana;"> or </span><code><span style="font-family:Verdana;">-0x</span></code><span style="font-family:Verdana;"> are parsed as hexadecimals; strings that begin with </span><code><span style="font-family:Verdana;">0</span></code><span style="font-family:Verdana;"> or </span><code><span style="font-family:Verdana;">-0</span></code><span style="font-family:Verdana;"> are parsed as octal numbers. All other strings are parsed as decimal numbers.</span></p>
<p style="line-height:125%;margin:0 0 .0001pt;"><span style="font-family:Verdana;">If the string argument cannot be parsed as an integer, the results will be different in different browsers (either </span><code><span style="font-family:Verdana;">0</span></code><span style="font-family:Verdana;"> or </span><code><span style="font-family:Verdana;">NaN</span></code><span style="font-family:Verdana;">). </span></p>
<p style="line-height:125%;margin:0 0 .0001pt;"> </p>
<p style="line-height:125%;margin:0 0 .0001pt;"><em><span style="font-family:Verdana;">Examples</span></em><span style="font-family:Verdana;"> (comments in each line give the conversion results): </span></p>
<p style="line-height:125%;margin:0 0 .0001pt;"><span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;123.45&#8242;)<span>  </span>// 123</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;77&#8242;)<span>      </span>// 77</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;077&#8242;,10)<span>  </span>// 77</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;77&#8242;,8)<span>    </span>// 63<span>  </span>(= 7 + 7*8)</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;077&#8242;)<span>     </span>// 63<span>  </span>(= 7 + 7*8)</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;77&#8242;,16)<span>   </span>// 119 (= 7 + 7*16)</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;0&#215;77&#8242;)<span>    </span>// 119 (= 7 + 7*16)</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;099&#8242;)<span>     </span>// 0 (9 is not an octal digit)</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;99&#8242;,8)<span>    </span>// 0 or NaN, depending on the platform</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;0.1e6&#8242;)<span>   </span>// 0</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;ZZ&#8217;,36)<span>   </span>// 1295 (= 35 + 35*36)</span>
</p>
<p class="MsoNormal"><span style="font-family:Verdana;">So, if you try with “08” or “09” in <strong>parseInt(“value”)</strong>, you may notice a strange thing: it treats &#8220;08&#8243; or &#8220;09&#8243; as 0. </span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">This happens for all invalid inputs. The answer to this question lies in above examples. </span></p>
<p class="MsoNormal" style="line-height:125%;"><span style="font-family:Verdana;">An integer can be represented in three different ways: in base 8 (octal), base 10 (decimal) and base 16 (hexadecimal). A starting zero indicates an octal number, which can be anything from 0 to 7 but not 8 or 9. Similarly, a starting “0x” followed by a number that can indicates a hexadecimal number which can be anything from 0 to F.</span></p>
<p>The solution to the problem is very simple. Since &#8220;08&#8243; means an octal number because of a starting 0, parseInt returns a zero for 8. Now, parseInt accepts a second optional parameter, which is the base of the number. You can forcefully tell it to treat the number as a decimal one by sending a second parameter 10.</p>
<p class="MsoNormal"><strong><span style="font-family:Verdana;">Var _num = “08”; //or you can take input from a Form.</span></strong></p>
<p class="MsoNormal"><strong><span style="font-family:Verdana;">var theNumber = parseInt (_num, 10);</span></strong></p>
<p class="MsoNormal"><strong><span style="text-decoration:underline;"><span style="font-size:14pt;font-family:Verdana;">A utility method to check if the value is integer or not</span></span></strong><strong><span style="font-family:Verdana;"><br />
[like </span></strong><em><span style="font-family:Verdana;">int.TryParse </span></em><em><span style="font-style:normal;font-family:Verdana;">Method in .NET]</span></em>
</p>
<p class="MsoNormal"><span style="font-family:Verdana;">It makes it really convenient when evaluating a string value as an <span class="hilite1">integer</span>. But, </span><span class="hilite2"><span style="font-family:Verdana;">JavaScript</span></span><span style="font-family:Verdana;"> has no equivalent so you need to do your own evaluation every time.</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">Here&#8217;s a simple <span class="hilite2">JavaScript</span> method, which I found in the Internet:</span></p>
<table style="border:medium none;border-collapse:collapse;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width:6.15in;border:windowtext .5pt solid;padding:0 5.4pt;" width="590" valign="top">
<p class="MsoNormal"><em><span style="font-family:Verdana;">function <strong>TryParseInt</strong>(str,defaultValue){</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">var retValue = defaultValue;</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">if(typeof str != &#8216;undefined&#8217; &amp;&amp; str!=null &amp;&amp; str.length&gt;0){</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">        if (!isNaN(str)){</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">               retValue = parseInt(str);</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">         }<span> </span></span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">}</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">return retValue;</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">}</span></em></p>
</td>
</tr>
</tbody>
</table>
<p style="margin:0 0 .0001pt;"><span style="font-family:Verdana;">The first parameter of the TryParseInt method is the string you want to evaluate, and the second parameter is the default value to return if the string cannot be evaluated to an <span class="hilite1">integer</span>.</span></p>
<p><span style="font-family:Verdana;">Here are some example usages:</span></p>
<table style="border:medium none;border-collapse:collapse;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width:6.15in;border:windowtext .5pt solid;padding:0 5.4pt;" width="590" valign="top">
<p class="MsoNormal"><span style="font-family:Verdana;">//This will return 7</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">var a = TryParseInt(&#8220;7&#8243;, 0);</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">alert(a);</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">//This will return 0</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">var b = TryParseInt(&#8220;6c&#8221;, 0);</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">alert(b);</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">//This will return null</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">var c = TryParseInt(&#8220;abcd&#8221;, null);</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">alert(c);</span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><strong><span style="text-decoration:underline;"><span style="font-family:Verdana;">An alternative to this above TryParseInt method will be :</span></span></strong></p>
<table style="border:medium none;border-collapse:collapse;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width:6.15in;border:windowtext .5pt solid;padding:0 5.4pt;" width="590" valign="top"><strong><span style="font-size:12pt;font-family:Verdana;">// Returns true if the argument is a valid Integer, </span></strong></p>
<p><strong><span style="font-size:12pt;font-family:Verdana;">//else returns false.<br />
</span></strong></p>
<p><span style="font-size:12pt;font-family:Verdana;">function <strong>isInteger</strong> (value) </span><span style="font-size:12pt;font-family:Verdana;"><span>   </span>{</span> <span style="font-size:12pt;font-family:Verdana;"><span>     <br />
</span><br />
var i;</span> <span style="font-size:12pt;font-family:Verdana;"><span><br />
</span><br />
if (isEmpty(value)) return false;</span> </p>
<p><span style="font-size:12pt;font-family:Verdana;">for (i = 0; i &lt; value.length; i++)</span> <span style="font-size:12pt;font-family:Verdana;"><span>     <br />
</span>{</span><br />
<span style="font-size:12pt;font-family:Verdana;"><span>         </span>var c = value.charAt(i);</span> <br />
<span style="font-size:12pt;font-family:Verdana;"><span>         </span>if (false == isDigit(c))<br />
                  return false;</span> <span style="font-size:12pt;font-family:Verdana;"><span>     <br />
</span></span><span style="font-size:12pt;font-family:Verdana;"> }</span><br />
<span style="font-size:12pt;font-family:Verdana;"> return true;</span> <span style="font-size:12pt;font-family:Verdana;"><span>  <br />
</span>}</span> </p>
<p> <strong><span style="font-size:12pt;font-family:Verdana;">//Checks if the input string is empty or not</span></strong></p>
<p><span style="font-size:12pt;font-family:Verdana;">function <strong>isEmpty</strong>(value)</span> <span style="font-size:12pt;font-family:Verdana;"><span>  <br />
</span>{</span> <span style="font-size:12pt;font-family:Verdana;"><span>     <br />
</span></span><span style="font-size:12pt;font-family:Verdana;">     return ((typeof value == &#8216;undefined&#8217;) || (value == null) || (value.length == 0));</span> <span style="font-size:12pt;font-family:Verdana;"><span>  <br />
</span>}</span></p>
<p><strong><span style="font-size:12pt;font-family:Verdana;">//Checks if the input character is a digit or not</span></strong> </p>
<p><span style="font-size:12pt;font-family:Verdana;">function <strong>isDigit</strong> (c)</span> <span style="font-size:12pt;font-family:Verdana;"><span>   </span>{</span> <span style="font-size:12pt;font-family:Verdana;"><span>     <br />
</span>     return ((c &gt;= &#8220;0&#8243;) &amp;&amp; (c &lt;= &#8220;9&#8243;));</span> <span style="font-size:12pt;font-family:Verdana;"><span>  </span></span></p>
<p><span style="font-size:12pt;font-family:Verdana;"><span> </span>}</span></td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Verdana;">Hope this article helps you [:D]</span></p>
Posted in Basic Scripts, javascript, Scripts Tagged: digit, integer, isdigit, isempty, isinteger, isNaN, javascript, number, parse, parse integer, parseInt, parsing, tryparse, undefined <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/80/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&blog=1889208&post=80&subd=usefulscripts&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2008/10/02/integer-parsing-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>Sending Email in ASP.NET 2.0 with attachments</title>
		<link>http://usefulscripts.wordpress.com/2008/09/29/sending-email-in-aspnet-20-with-attachments/</link>
		<comments>http://usefulscripts.wordpress.com/2008/09/29/sending-email-in-aspnet-20-with-attachments/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 14:27:29 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[attachment]]></category>
		<category><![CDATA[delivery]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[mailmessage]]></category>
		<category><![CDATA[net.mail]]></category>
		<category><![CDATA[SMTP]]></category>
		<category><![CDATA[SmtpClient]]></category>
		<category><![CDATA[System.Web.Mail]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=70</guid>
		<description><![CDATA[Sending Email in ASP.NET 2.0
(Taken from: http://aspnet.4guysfromrolla.com/articles/072606-1.aspx)
Introduction
Email serves as a ubiquitous, asynchronous notification and information distribution system. Not surprisingly, there are many web development scenarios where server-side code needs to generate an email and scuttle it off to the intended recipient. The email may be destined for a user of the website, informing them of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&blog=1889208&post=70&subd=usefulscripts&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p class="MsoNormal"><strong><span style="font-size:18pt;">Sending Email in ASP.NET 2.0</span></strong></p>
<p class="MsoNormal"><span style="font-size:11pt;">(Taken from: <a href="http://aspnet.4guysfromrolla.com/articles/072606-1.aspx" target="_blank">http://aspnet.4guysfromrolla.com/articles/072606-1.aspx</a>)</span></p>
<p><strong>Introduction</strong><br />
Email serves as a ubiquitous, asynchronous notification and information distribution system. Not surprisingly, there are many web development scenarios where server-side code needs to generate an email and scuttle it off to the intended recipient. The email may be destined for a user of the website, informing them of their newly created user account, reminding them of their forgotten password, or emailing them an invoice. Or it may be destined for a web developer or site administrator, providing information of an unhandled exception that just transpired or user feedback.</p>
<p>Fortunately, ASP.NET makes sending email a breeze. The .NET Framework version 1.x included a number of classes in the <code><strong><span style="font-size:10pt;">System.Web.Mail</span></strong></code> class that allowed programmatically sending an email with a few scant lines of code. While this namespace and these classes still exist in the .NET Framework version 2.0, they have been deprecated in favor of new mail-related classes found in the <a href="http://msdn2.microsoft.com/en-us/library/system.net.mail.aspx"><code><span style="font-size:10pt;">System.Net.Mail</span></code> namespace</a>. (For an article on sending email in ASP.NET version 1.x, see <a href="http://www.4guysfromrolla.com/webtech/080801-1.shtml">Sending Email from an ASP.NET 1.x Web Page</a> or consult <a href="http://www.systemwebmail.com/">www.SystemWebMail.com</a>.)</p>
<p>In this article we&#8217;ll look at the classes in the <code><strong><span style="font-size:10pt;">System.Net.Mail</span></strong></code> namespace and see how to send an email from an ASP.NET 2.0 page&#8217;s code-behind class. We&#8217;ll also look at specifying relay server information in <code><strong><span style="font-size:10pt;">Web.config</span></strong></code> and how this information can be used in some of the built-in ASP.NET server controls for sending emails (such as when a user creates an account or needs a password reminder/reset). Read on to learn more!</p>
<p><strong>Exploring the Classes in the </strong><code><strong><span style="font-size:10pt;">System.Net.Mail</span></strong></code><strong> Namespace</strong><br />
There are 16 different classes in the <code><span style="font-size:10pt;">System.Net.Mail</span></code> namespace, all related to send email to a specified <a href="http://en.wikipedia.org/wiki/SMTP">Simple Mail Transfer Protocol (SMTP)</a> server for delivery. The two core classes in this namespace are:</p>
<ul type="disc">
<li class="MsoNormal"><a href="http://msdn2.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx"><code><span style="font-size:10pt;font-family:&quot;">MailMessage</span></code></a> &#8211; represents an email message; has properties like <code><span style="font-size:10pt;font-family:&quot;">From</span></code>,      <code><span style="font-size:10pt;font-family:&quot;">To</span></code>,      <code><span style="font-size:10pt;font-family:&quot;">Subject</span></code>,      <code><span style="font-size:10pt;font-family:&quot;">Body</span></code>,      and so on.</li>
<li class="MsoNormal"><a href="http://msdn2.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx"><code><span style="font-size:10pt;font-family:&quot;">SmtpClient</span></code></a> &#8211; sends a specified <code><span style="font-size:10pt;font-family:&quot;">MailMessage</span></code> instance to a specified SMTP server.</li>
</ul>
<p class="MsoNormal">When sending an email from an ASP.NET 2.0 page you will, typically:</p>
<ol type="1">
<li class="MsoNormal">Create a <code><span style="font-size:10pt;font-family:&quot;">MailMessage</span></code> object</li>
<li class="MsoNormal">Assign its properties</li>
<li class="MsoNormal">Create an instance of the <code><span style="font-size:10pt;font-family:&quot;">SmtpClient</span></code> class</li>
<li class="MsoNormal">Specify details about the      SMTP server to use (if they&#8217;re not already specified within <code><span style="font-size:10pt;font-family:&quot;">Web.config</span></code>)</li>
<li class="MsoNormal">Send the <code><span style="font-size:10pt;font-family:&quot;">MailMessage</span></code> via the <code><span style="font-size:10pt;font-family:&quot;">SmtpClient</span></code> object&#8217;s <code><span style="font-size:10pt;font-family:&quot;">Send</span></code> method</li>
</ol>
<p class="MsoNormal">
<p class="MsoNormal">Steps 1 and 2 may be bypassed as the <code><span style="font-size:10pt;font-family:&quot;">SmtpClient</span></code> class&#8217;s <code><span style="font-size:10pt;font-family:&quot;">Send</span></code> method can accept either a <code><span style="font-size:10pt;font-family:&quot;">MailMessage</span></code> object or four strings, representing the from, to, subject, and body contents of the email message.</p>
<p style="margin:0 0 .0001pt;">The <code><span style="font-size:10pt;">System.Net.Mail</span></code> namespace&#8217;s other classes allow for more advanced email functionality. There are classes that can be used to add attachments to an email message, to embed objects within an email, to specify SMTP server authentication information, and <code><span style="font-size:10pt;">Exception</span></code>-derived classes for handling SMTP-specific exceptions. We&#8217;ll examine using some of these other classes for more advanced scenarios in future articles.</p>
<p style="margin:0 0 .0001pt;"><strong>Providing the SMTP Server&#8217;s Details</strong><br />
When sending an email to a friend from Outlook or GMail, the email program establishes a connection with a relay server and sends the contents of the email message, along with information such as the date the email was composed, the email body&#8217;s format (text or HTML, for example), the recipient(s), and so on. The relay server accepts the message and then connects to the recipient&#8217;s SMTP server and sends the message. Once the message has been delivered, the recipient can, at some later point in time, pull down the message using a different protocol (such as <a href="http://en.wikipedia.org/wiki/Internet_Message_Access_Protocol">IMAP</a> or <a href="http://en.wikipedia.org/wiki/POP3">POP3</a>).
</p>
<p style="margin:0 0 .0001pt;">Therefore, to send an email from an ASP.NET page we need to provide the <code><span style="font-size:10pt;">SmtpClient</span></code> class with information about the relay server. Along with the hostname of the relay server, you can specify the port (typically port 25 is used), whether or not to use SSL when communicating your email message contents to the relay server, and authentication credentials (if necessary). Alternatively, if you have a local SMTP service installed on your web server, it may periodically monitor a particular &#8220;drop-off&#8221; directory, sending any messages that appear in that directory. You can configure whether the <code><span style="font-size:10pt;">SmtpClient</span></code> class relays its email messages to a separate relay server or if it drops it off in a specified pickup directory through the <a href="http://msdn2.microsoft.com/en-us/library/system.net.mail.smtpclient.deliverymethod.aspx"><code><span style="font-size:10pt;">DeliveryMethod</span></code> property</a>.</p>
<p style="margin:0 0 .0001pt;">The relay server information used by the <code><span style="font-size:10pt;">SmtpClient</span></code> class can be specified programmatically, through the <code><span style="font-size:10pt;">SmtpClient</span></code> class&#8217;s properties, or can be centralized in <code><span style="font-size:10pt;">Web.config</span></code>. To use the <code><span style="font-size:10pt;">Web.config</span></code> approach, add a <code><span style="font-size:10pt;">&lt;system.net&gt;</span></code> element within the <code><span style="font-size:10pt;">&lt;configuration&gt;</span></code> element. Then, add a <a href="http://msdn2.microsoft.com/en-us/library/w355a94k.aspx"><code><span style="font-size:10pt;">&lt;mailSettings&gt;</span></code></a> element that contains an <a href="http://msdn2.microsoft.com/en-us/library/ms164240.aspx"><code><span style="font-size:10pt;">&lt;smtp&gt;</span></code></a> element whose settings are specified within its <a href="http://msdn2.microsoft.com/en-us/library/ms164242.aspx"><code><span style="font-size:10pt;">&lt;network&gt;</span></code></a> child element, like so:</p>
<table style="width:95%;" border="0" cellpadding="0" width="95%">
<tbody>
<tr>
<td style="background:#cccccc none repeat scroll 0 0;width:100%;padding:.75pt;" width="100%">
<pre>&lt;configuration&gt;
<span>  </span>&lt;!-- Add the email settings to the &lt;system.net&gt; element --&gt;
<strong><span>  </span>&lt;system.net&gt;</strong>
<strong><span>    </span>&lt;mailSettings&gt;</strong>
<strong><span>      </span>&lt;smtp&gt;</strong>
<strong><span>        </span>&lt;network </strong>
<strong><span>             </span>host="<em>relayServerHostname</em>" </strong>
<strong><span>             </span>port="<em>portNumber</em>"</strong>
<strong><span>             </span>userName="<em>username</em>"</strong>
<strong><span>             </span>password="<em>password</em>" /&gt;</strong>
<strong><span>      </span>&lt;/smtp&gt;</strong>
<strong><span>    </span>&lt;/mailSettings&gt;</strong>
<strong><span>  </span>&lt;/system.net&gt;</strong>
<!--[if !supportEmptyParas]--> <!--[endif]-->
<span>  </span>&lt;system.web&gt;
<span>    </span>...
<span>  </span>&lt;/system.web&gt;
&lt;/configuration&gt;</pre>
</td>
</tr>
</tbody>
</table>
<p style="margin:0 0 .0001pt;">The <code><span style="font-size:10pt;">host</span></code> attribute contains the <em>relayServerHostname</em>. If you are using an external relay server, the <em>relayServerHostname</em> might be something like <code><span style="font-size:10pt;">smtp.yourisp.com</span></code>. If the relay server&#8217;s port number is something other than the typical port 25, specify it through the <code><span style="font-size:10pt;">port</span></code> attribute. Most external relay servers require authentication of some sort (in order to prevent anonymous spammers from sending their garbage through the relay). The <code><span style="font-size:10pt;">userName</span></code> and <code><span style="font-size:10pt;">password</span></code> attributes can be provided in the case where username/password authentication is needed.</p>
<p style="margin:0 0 .0001pt;">Only a subset of the <code><span style="font-size:10pt;">SmtpClient</span></code> properties can be specified through settings in <code><span style="font-size:10pt;">Web.config</span></code>. To customize the other <code><span style="font-size:10pt;">SmtpClient</span></code> properties &#8211; <code><span style="font-size:10pt;">EnableSsl</span></code>, <code><span style="font-size:10pt;">Timeout</span></code>, and so on &#8211; set them programmatically when sending the email (step 4 from the list of five steps examined earlier in this article).</p>
<p style="margin:0 0 .0001pt;"><strong>Sending an Administrator Email Through a Feedback Web Page</strong><br />
To illustrate sending an email using the <code><span style="font-size:10pt;">MailMessage</span></code> and <code><span style="font-size:10pt;">SmtpClient</span></code> classes, I&#8217;ve created a simple feedback page example. In this page the user is prompted for their email address, the subject of their feedback, and their feedback.</p>
<table style="width:95%;" border="0" cellpadding="0" width="95%">
<tbody>
<tr>
<td style="background:#cccccc none repeat scroll 0 0;width:100%;padding:.75pt;" width="100%">
<p class="MsoNormal"><code><span style="font-size:10pt;font-family:&quot;">&lt;table   border="0"&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td&gt;&lt;b&gt;Your   Email:&lt;/b&gt;&lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td&gt;&lt;asp:TextBox   runat="server" ID="UsersEmail"   Columns="30"&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td&gt;&lt;b&gt;Subject:&lt;/b&gt;&lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td&gt;&lt;asp:TextBox   runat="server" ID="Subject"   Columns="30"&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td   colspan="2"&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;b&gt;Body:&lt;/b&gt;&lt;br   /&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;asp:TextBox   runat="server" ID="Body" TextMode="MultiLine"   Columns="55" Rows="10"&gt;&lt;/asp:TextBox&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td   colspan="2" align="center"&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;asp:Button   runat="server" ID="SendEmail" Text="Send   Feedback" /&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">&lt;/table&gt; </span></code></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal" style="text-align:center;" align="center"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal" align="center"><!--[if gte vml 1]&gt;                    &lt;![endif]--><!--[if !vml]--><img style="border:0 none;" src="http://usefulscripts.files.wordpress.com/2008/09/email1.gif" border="0" alt="The feedback form." /><!--[endif]--></p>
<p style="margin:0 0 .0001pt;"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p style="margin:0 0 .0001pt;">Once the user has supplied the feedback information and clicked the &#8220;Send Feedback&#8221; button, a postback ensues and the Button&#8217;s <code><span style="font-size:10pt;">Click</span></code> event fires. Inside the event handler, a <code><span style="font-size:10pt;">MailMessage</span></code> object is created and its <code><span style="font-size:10pt;">To</span></code>, <code><span style="font-size:10pt;">From</span></code>, <code><span style="font-size:10pt;">Subject</span></code>, and <code><span style="font-size:10pt;">Body</span></code> properties are set according to the information provided by the user. With the <code><span style="font-size:10pt;">MailMessage</span></code> object created and its properties populated, the email is then sent through the <code><span style="font-size:10pt;">SmtpClient</span></code> class&#8217;s <code><span style="font-size:10pt;">Send</span></code> method.</p>
<p style="margin:0 0 .0001pt;"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<table style="width:95%;" border="0" cellpadding="0" width="95%">
<tbody>
<tr>
<td style="background:#cccccc none repeat scroll 0 0;width:100%;padding:.75pt;" width="100%">
<p class="MsoNormal"><code><span style="font-size:10pt;font-family:&quot;">Protected   Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs)   Handles SendEmail.Click</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> '<strong><span style="color:red;">!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS</span></strong></span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> Const   ToAddress As String = "<em><span style="color:red;">you@youremail.com</span></em>"</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;"> '(1)   Create the MailMessage instance</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> Dim   mm As New MailMessage(UsersEmail.Text, ToAddress)</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;"> '(2)   Assign the MailMessage's properties</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> mm.Subject   = Subject.Text</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> mm.Body   = Body.Text</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> mm.IsBodyHtml   = False</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;"> '(3)   Create the SmtpClient object</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> Dim   smtp As New SmtpClient</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;"> '(4)   Send the MailMessage (will use the Web.config settings)</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> smtp.Send(mm)</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">End   Sub </span></code></p>
</td>
</tr>
</tbody>
</table>
<p style="margin:0 0 .0001pt;">We didn&#8217;t need to set any of the <code><span style="font-size:10pt;">SmtpClient</span></code> class&#8217;s properties here in code because they have been specified in <code><span style="font-size:10pt;">Web.config</span></code> (download the complete code at the end of this article to run this application on your computer).</p>
<p style="margin:0 0 .0001pt;"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal" align="center"><!--[if gte vml 1]&gt;  &lt;![endif]--><!--[if !vml]--><img style="border:0 none;" src="http://usefulscripts.files.wordpress.com/2008/09/email2.gif" border="0" alt="The received email." /><!--[endif]--></p>
<p style="margin:0 0 .0001pt;"><strong><!--[if !supportEmptyParas]--> <!--[endif]--></strong></p>
<p style="margin:0 0 .0001pt;"><strong>Conclusion</strong><br />
Along with a plethora of other improvements from ASP.NET 1.x, the email sending capabilities in ASP.NET 2.0 have been updated and moved to a new namespace, <code><span style="font-size:10pt;">System.Net.Mail</span></code>. In 2.0 the relay server settings can easily be decoupled from the ASP.NET code and moved into the <code><span style="font-size:10pt;">Web.config</span></code> file, as we saw in this example. Moreover, there&#8217;s better support for relay server authentication. Future articles will explore more advanced email scenarios, such as: crafting HTML-formatted emails, sending attachments, embedding objects within the email body, handling SMTP/relay server-related exceptions, and so on.</p>
<p><strong><a href="http://aspnet.4guysfromrolla.com/code/EmailDemos.zip">Download Demo</a></strong></p>
<p><strong><span style="font-size:18pt;">Sending Email in ASP.NET 2.0: HTML-Formatted Emails, Attachments, and Gracefully Handling SMTP Exceptions</span></strong></p>
<p><span style="font-size:11pt;">(Taken from: <a href="http://aspnet.4guysfromrolla.com/articles/080206-1.aspx" target="_blank">http://aspnet.4guysfromrolla.com/articles/080206-1.aspx</a>)</span></p>
<p><strong>Introduction</strong><br />
The .NET Framework version 2.0 includes a new namespace (<code><span style="font-size:10pt;">System.Net.Mail</span></code>) and new classes for sending email. (The namespace (<code><span style="font-size:10pt;">System.Web.Mail</span></code>) and classes used in the .NET Framework version 1.x still exist for backwards compatibility.) Last week we examined how to use the <code><span style="font-size:10pt;">MailMessage</span></code> and <code><span style="font-size:10pt;">SmtpClient</span></code> classes in the <code><span style="font-size:10pt;">System.Net.Mail</span></code> namespace for sending simple, plain-text email messages.</p>
<p>This article looks at the more advanced email-related options. We&#8217;ll see how to send HTML-formatted emails, how to include attachments, and how to gracefully handle SMTP exceptions when sending an email (such as invalid relay server credentials or if the relay server is offline). Read on to learn more!</p>
<p><strong>Sending HTML-Formatted Emails</strong><br />
In <em>Sending Email in ASP.NET 2.0</em> we saw how to send plain-text emails by assigning the contents of the email to the <code><span style="font-size:10pt;">MailMessage</span></code> class&#8217;s <a href="http://msdn2.microsoft.com/en-us/library/system.net.mail.mailmessage.body.aspx"><code><span style="font-size:10pt;text-decoration:none;color:#000000;">Body</span></code> property</a>. To send HTML-formatted emails, simply set the <code><span style="font-size:10pt;">Body</span></code> property to the HTML content to send, and then mark the <code><span style="font-size:10pt;">MailMessage</span></code> class&#8217;s <a href="http://msdn2.microsoft.com/en-us/library/system.net.mail.mailmessage.isbodyhtml.aspx"><code><span style="font-size:10pt;text-decoration:none;color:#000000;">IsBodyHtml</span></code> property</a> to True.</p>
<p>To demonstrate sending an HTML-formatted message, I created a sample named <code><span style="font-size:10pt;">HtmlEmail.aspx</span></code> available for download at the end of this article. The germane code follows:</p>
<table style="width:95%;" border="0" cellpadding="0" width="95%">
<tbody>
<tr>
<td style="background:#cccccc none repeat scroll 0 0;width:100%;padding:.75pt;" width="100%">
<p class="MsoNormal"><code><span style="font-size:10pt;font-family:&quot;">'(1)   Create the MailMessage instance</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">Dim   mm As New MailMessage(<em>FromEmailAddress</em>, <em>ToEmailAddress</em>)</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;">'(2)   Assign the MailMessage's properties</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">mm.Subject   = "HTML-Formatted Email Demo Using the IsBodyHtml Property"</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">mm.Body   = "&lt;h2&gt;This is an HTML-Formatted Email Send Using the   &lt;code&gt;IsBodyHtml&lt;/code&gt; Property&lt;/h2&gt;&lt;p&gt;Isn't HTML   &lt;em&gt;neat&lt;/em&gt;?&lt;/p&gt;&lt;p&gt;You can make all sorts of   &lt;span style=""color:red;font-weight:bold;""&gt;pretty   colors!!&lt;/span&gt;.&lt;/p&gt;"</span></code><span style="font-size:10pt;"><br />
</span><code><strong><span style="font-size:10pt;font-family:&quot;">mm.IsBodyHtml   = True</span></strong></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;">'(3)   Create the SmtpClient object</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">Dim   smtp As New SmtpClient</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;">'(4)   Send the MailMessage (will use the Web.config settings)</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">smtp.Send(mm) </span></code></p>
</td>
</tr>
</tbody>
</table>
<p>As you can see, simply set the <code><span style="font-size:10pt;">Body</span></code> property to the HTML content to send and the <code><span style="font-size:10pt;">IsBodyHtml</span></code> property to True, and you&#8217;re done! The actual email content that gets sent to the relay server (and eventually down to the recipient&#8217;s email client), looks something like the following:
</p>
<p class="MsoNormal" style="margin-right:.5in;margin-left:.5in;"><code><span style="font-size:10pt;font-family:&quot;">x-sender: <em>ToEmailAddress</em></span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">x-receiver: <em>FromEmailAddress</em></span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">mime-version: 1.0</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">from: <em>FromEmailAddress</em></span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">to: <em>ToEmailAddress</em></span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">date: 25 Jul 2006 15:06:44 -0700</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">subject: HTML-Formatted Email Demo Using the IsBodyHtml Property</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">content-type: text/html; charset=us-ascii</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">content-transfer-encoding: quoted-printable</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;">&lt;h2&gt;This is an HTML-Formatted Email Send Using the &lt;code&gt;IsBodyHtml&lt;/code&gt;=</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">Property&lt;/h2&gt;&lt;p&gt;Isn't HTML &lt;em&gt;neat&lt;/em&gt;?&lt;/p&gt;&lt;p&gt;You can make all sorts=</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">of &lt;span style=3D"color:red;font-weight:bold;"&gt;pretty colors!!&lt;/span&gt;.&lt;/p&gt;</span></code></p>
<div>
<table style="border:.75pt solid #333333;background:lightyellow none repeat scroll 0 0;width:88%;" border="1" cellpadding="0" width="88%" bgcolor="lightyellow">
<tbody>
<tr>
<td style="border:medium none;padding:5pt;">
<p class="MsoNormal" style="text-align:center;" align="center"><strong>Viewing the   Email Content Sent to the Relay Server</strong><strong></strong></p>
</td>
</tr>
<tr>
<td style="border:medium none;padding:5pt;">
<p class="MsoNormal">Interested in viewing the actual content sent to the relay   server by the <code><span style="font-size:10pt;font-family:&quot;">SmtpClient</span></code> class (like the content shown above)? In <em><span>Sending Email in ASP.NET 2.0</span></em> we discussed how   the <code><span style="font-size:10pt;font-family:&quot;">SmtpClient</span></code> class can be configured to send the email to a relay server <em>or</em> have it   dropped off in a specified directory. Using the latter option, we can explore   the actual email content that would otherwise have been sent to the relay   server. Check out the <code><span style="font-size:10pt;font-family:&quot;">Web.config</span></code> file in the code available for download at the end of this article &#8211; there&#8217;s   a commented out <code><span style="font-size:10pt;font-family:&quot;">&lt;smtp&gt;</span></code> element that shows how to configure the <code><span style="font-size:10pt;font-family:&quot;">SmtpClient</span></code> class to dump the   email&#8217;s contents to a specified directory.</p>
</td>
</tr>
</tbody>
</table>
</div>
<p style="margin:0 0 .0001pt;">The email client &#8211; assuming it supports HTML-formatted emails &#8211; will display the HTML content within the email.</p>
<p class="MsoNormal" align="center"><!--[if gte vml 1]&gt;  &lt;![endif]--><!--[if !vml]--><img style="border:0 none;" src="http://usefulscripts.files.wordpress.com/2008/09/email3.gif" border="0" alt="The HTML-formatted email." /><!--[endif]--></p>
<p class="MsoNormal" style="text-align:center;" align="center"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<div>
<table style="border:.75pt solid #333333;background:lightyellow none repeat scroll 0 0;width:88%;" border="1" cellpadding="0" width="88%" bgcolor="lightyellow">
<tbody>
<tr>
<td style="border:medium none;padding:5pt;">
<p class="MsoNormal" style="text-align:center;" align="center"><strong>Caveats on   Sending HTML-Formatted Emails</strong><strong></strong></p>
</td>
</tr>
<tr>
<td style="border:medium none;padding:5pt;">
<p class="MsoNormal">When sending HTML-formatted emails, understand that the   HTML you see on your screen may differ quite a bit from what your recipients   see. Most all email clients strip out potentially dangerous HTML content   (ActiveX controls and the like), many prevent JavaScript from running, and   most handle external styles poorly. For a more thorough discussion on   potential problems with sending HTML-formatted emails, check out <a href="http://www.mailchimp.com/resources/top10_html_email_mistakes.phtml">Top   10 HTML Email Mistakes</a> and <a href="http://www.alistapart.com/articles/cssemail/">CSS and Email, Kissing in   a Tree</a>.</p>
</td>
</tr>
</tbody>
</table>
</div>
<p style="margin:0 0 .0001pt;"><strong><br />
Including Attachments</strong></p>
<p>The <code><span style="font-size:10pt;">MailMessage</span></code> class has an <a href="http://msdn2.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx"><code><span style="font-size:10pt;text-decoration:none;color:#000000;">Attachments</span></code> property</a> that is a collection of <a href="http://msdn2.microsoft.com/en-us/library/system.net.mail.attachment.aspx"><code><span style="font-size:10pt;text-decoration:none;color:#000000;">Attachment</span></code> class</a> instances. You can attach an existing file on the web server to the email message or base the content&#8217;s attachment on a Stream. To illustrate sending emails will attachments, I created a demo where the visitor can fill out a feedback-like form to have an email sent to administrator. However, this feedback form allows the visitor to pick a file from <em>their</em> computer to be attached to the email sent from the web page (much like how the web-based email web applications &#8211; Hotmail, GMail, etc. &#8211; allow you to attach a file from your computer when sending an email).
</p>
<p style="margin:0 0 .0001pt;">To allow the visitor to attach a file from their computer, we need to allow the user to upload a file from their machine to the web server. This can be easily accomplished using the <a href="http://asp.net/QuickStart/aspnet/doc/ctrlref/standard/fileupload.aspx">FileUpload control</a> (which is new to ASP.NET 2.0). Let&#8217;s look at the declarative syntax used for creating the user interface for this demo:</p>
<table style="width:95%;" border="0" cellpadding="0" width="95%">
<tbody>
<tr>
<td style="background:#cccccc none repeat scroll 0 0;width:100%;padding:.75pt;" width="100%">
<p class="MsoNormal"><code><span style="font-size:10pt;font-family:&quot;">&lt;table   border="0"&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td&gt;&lt;b&gt;Your   Email:&lt;/b&gt;&lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td&gt;&lt;asp:TextBox   runat="server" ID="UsersEmail"   Columns="30"&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td&gt;&lt;b&gt;File   to Send:&lt;/b&gt;&lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> <strong>&lt;asp:FileUpload   ID="AttachmentFile" runat="server" /&gt;</strong></span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td   colspan="2"&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;b&gt;Body:&lt;/b&gt;&lt;br   /&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;asp:TextBox   runat="server" ID="Body" TextMode="MultiLine"   Columns="55" Rows="10"&gt;&lt;/asp:TextBox&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;td   colspan="2" align="center"&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;asp:Button   runat="server" ID="SendEmail" Text="Send   Feedback" /&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/td&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> &lt;/tr&gt;</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">&lt;/table&gt; </span></code></p>
</td>
</tr>
</tbody>
</table>
<p style="margin:0 0 .0001pt;">The FileUpload control renders as a <code><span style="font-size:10pt;">&lt;input type="file" ... /&gt;</span></code> HTML element, which, in the browser, is displayed as a textbox with a Browse button. When clicked, a dialog box is opened from which the user can pick a file from their computer.</p>
<p style="margin:0 0 .0001pt;"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal" align="center"><!--[if gte vml 1]&gt;  &lt;![endif]--><!--[if !vml]--><img style="border:0 none;" src="http://usefulscripts.files.wordpress.com/2008/09/email4.gif" border="0" alt="The user is prompted for a file to upload." /><!--[endif]--></p>
<p style="margin:0 0 .0001pt;"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p style="margin:0 0 .0001pt;">After filling in the feedback form, selecting a file to upload, and clicking the &#8220;Send Feedback&#8221; button, a postback occurs, sending the contents of the selected file up to the web server. In the &#8220;Send Feedback&#8221; Button&#8217;s <code><span style="font-size:10pt;">Click</span></code> event handler, a <code><span style="font-size:10pt;">MailMessage</span></code> object is created and an attachment is added. Since the FileUpload provides a Stream to the uploaded data, we can simply point the new <code><span style="font-size:10pt;">Attachment</span></code> object at this Stream. There&#8217;s no need to save the uploaded file to the web server&#8217;s file system.</p>
<table style="width:95%;" border="0" cellpadding="0" width="95%">
<tbody>
<tr>
<td style="background:#cccccc none repeat scroll 0 0;width:100%;padding:.75pt;" width="100%">
<p class="MsoNormal"><code><span style="font-size:10pt;font-family:&quot;">'Make   sure a file has been uploaded</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">If   String.IsNullOrEmpty(AttachmentFile.FileName) OrElse   AttachmentFile.PostedFile Is Nothing Then</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> Throw   New ApplicationException("Egad, a file wasn't uploaded... you should   probably use more graceful error handling than this, though...")</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">End   If</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;">'(1)   Create the MailMessage instance</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">Dim   mm As New MailMessage(<em>FromEmailAddress</em>, <em>ToEmailAddress</em>)</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;">'(2)   Assign the MailMessage's properties</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">mm.Subject   = "Emailing an Uploaded File as an Attachment Demo"</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">mm.Body   = Body.Text</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">mm.IsBodyHtml   = False</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;">'Attach   the file</span></code><span style="font-size:10pt;"><br />
</span><code><strong><span style="font-size:10pt;font-family:&quot;">mm.Attachments.Add(New   Attachment(AttachmentFile.PostedFile.InputStream, AttachmentFile.FileName))</span></strong></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;">'(3)   Create the SmtpClient object</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">Dim   smtp As New SmtpClient</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;">'(4)   Send the MailMessage (will use the Web.config settings)</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;">smtp.Send(mm) </span></code></p>
</td>
</tr>
</tbody>
</table>
<p style="margin:0 0 .0001pt;">The <code><span style="font-size:10pt;">Attachment</span></code> constructor overload used in the code sample above expects two inputs: a reference to the Stream that contains the data to attach, and the name to use for the attachment. The FileUpload&#8217;s <code><span style="font-size:10pt;">InputStream</span></code> and <code><span style="font-size:10pt;">FileName</span></code> properties are used for these two values.</p>
<p class="MsoNormal" align="center"><!--[if gte vml 1]&gt;  &lt;![endif]--><!--[if !vml]--><img style="border:0 none;" src="http://usefulscripts.files.wordpress.com/2008/09/email5.gif" border="0" alt="The email, which includes the attachment." /><!--[endif]--></p>
<p style="margin:0 0 .0001pt;"><strong><!--[if !supportEmptyParas]--> <!--[endif]--></strong></p>
<p style="margin:0 0 .0001pt;"><strong>Handling SMTP Exceptions</strong><br />
When sending an email from an ASP.NET page, what happens if the relay server is down, or if the authentication information used is invalid? In the face of an SMTP error, the <code><span style="font-size:10pt;">SmtpClient</span></code> class will throw an <a href="http://msdn2.microsoft.com/en-us/library/system.net.mail.smtpexception.aspx"><code><span style="font-size:10pt;text-decoration:none;color:#000000;">SmtpException</span></code> exception</a>. To gracefully handle such problems, we can add exception handling code around the code that sends the email. If there&#8217;s an <code><span style="font-size:10pt;">SmtpException</span></code> we can then display a more friendly and informative error message (or, perhaps, write the email&#8217;s contents to a file to be sent later).
</p>
<p style="margin:0 0 .0001pt;">In the download at the end of this article I&#8217;ve included a demo that allows the visitor to specify the relay server to use, along with authentication information. If there&#8217;s an error in attempting to send an email, a client-side alert box is displayed, explaining the problem. To test this out, enter an invalid relay server hostname or invalid credentials for a relay server that requires authentication.</p>
<table style="width:95%;" border="0" cellpadding="0" width="95%">
<tbody>
<tr>
<td style="background:#cccccc none repeat scroll 0 0;width:100%;padding:.75pt;" width="100%">
<p class="MsoNormal"><code><span style="font-size:10pt;font-family:&quot;">Try</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> '(1)   Create the MailMessage instance</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> Dim   mm As New MailMessage(<em>FromEmailAddress</em>, <em>ToEmailAddress</em>)</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;"> '(2)   Assign the MailMessage's properties</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> mm.Subject   = "Test Email... DO NOT PANIC!!!1!!!111!!"</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> mm.Body   = "This is a test message..."</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> mm.IsBodyHtml   = False</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;"> '(3)   Create the SmtpClient object</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> Dim   smtp As New SmtpClient</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;"> 'Set   the SMTP settings...</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> smtp.Host   = Hostname.Text</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> If   Not String.IsNullOrEmpty(Port.Text) Then</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> smtp.Port   = Convert.ToInt32(Port.Text)</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> End   If</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;"> If   Not String.IsNullOrEmpty(Username.Text) Then</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> smtp.Credentials   = New NetworkCredential(Username.Text, Password.Text)</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> End   If</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;"> '(4)   Send the MailMessage (will use the Web.config settings)</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> smtp.Send(mm)</span></code></p>
<p><code><span style="font-size:10pt;font-family:&quot;"> 'Display   a client-side popup, explaining that the email has been sent</span></code><span style="font-size:10pt;"><br />
</span><code><span style="font-size:10pt;font-family:&quot;"> ClientScript.RegisterStartupScript(Me.GetType(),   "HiMom!", String.Format("alert('An test email has successfully   been sent to {0}');", ToAddress.Replace("'", "\'")),   True)</span></code></p>
<p><code><strong><span style="font-size:10pt;font-family:&quot;">Catch   smtpEx As SmtpException</span></strong></code><strong><span style="font-size:10pt;"><br />
</span></strong><code><strong><span style="font-size:10pt;font-family:&quot;"> 'A   problem occurred when sending the email message</span></strong></code><strong><span style="font-size:10pt;"><br />
</span></strong><code><strong><span style="font-size:10pt;font-family:&quot;"> ClientScript.RegisterStartupScript(Me.GetType(),   "OhCrap", String.Format("alert('There was a problem in sending   the email: {0}');", smtpEx.Message.Replace("'",   "\'")), True)</span></strong></code><strong></strong></p>
<p><strong></strong><code><strong><span style="font-size:10pt;font-family:&quot;">Catch   generalEx As Exception</span></strong></code><strong><span style="font-size:10pt;"><br />
</span></strong><code><strong><span style="font-size:10pt;font-family:&quot;"> 'Some   other problem occurred</span></strong></code><strong><span style="font-size:10pt;"><br />
</span></strong><code><strong><span style="font-size:10pt;font-family:&quot;"> ClientScript.RegisterStartupScript(Me.GetType(),   "OhCrap", String.Format("alert('There was a general problem:   {0}');", generalEx.Message.Replace("'", "\'")),   True)</span></strong></code><strong><span style="font-size:10pt;"><br />
</span></strong><code><span style="font-size:10pt;font-family:&quot;">End   Try </span></code></p>
</td>
</tr>
</tbody>
</table>
<p style="margin:0 0 .0001pt;">This code catches both SMTP-specific error messages and general ones (such as assigning invalid email addresses to the <code><span style="font-size:10pt;">MailMessage</span></code> object&#8217;s <code><span style="font-size:10pt;">To</span></code> or <code><span style="font-size:10pt;">From</span></code> properties). In either case, a client-side alert box is displayed informing the user of the details of the error.</p>
<p class="MsoNormal" align="center"><!--[if gte vml 1]&gt;  &lt;![endif]--><!--[if !vml]--><img style="border:0 none;" src="http://usefulscripts.files.wordpress.com/2008/09/email6.gif" border="0" alt="An error message is displayed in a client-side alert box." /><!--[endif]--></p>
<p style="margin:0 0 .0001pt;"><strong><!--[if !supportEmptyParas]--> <!--[endif]--></strong></p>
<p style="margin:0 0 .0001pt;"><strong>Conclusion</strong><br />
In this article we saw how to send HTML-formatted emails, send emails with attachments, and gracefully handle exceptions arising from sending an email message. Sending an HTML-formatted email is as simple as specifying the HTML content in the <code><span style="font-size:10pt;">Body</span></code> property and setting the <code><span style="font-size:10pt;">IsBodyHtml</span></code> property to True. The real challenge comes in making sure the HTML content used is rendered as expected by the popular email clients. To add an attachment to an email, simply add an <code><span style="font-size:10pt;">Attachment</span></code> object to the <code><span style="font-size:10pt;">MailMessage</span></code>&#8217;s <code><span style="font-size:10pt;">Attachments</span></code> collection. The data for the attachment can come from a file on the web server or from a Stream. Finally, to handle SMTP-level exceptions, add exception handling code that catches the <code><span style="font-size:10pt;">SmtpException</span></code> thrown by the <code><span style="font-size:10pt;">SmtpClient</span></code> class when unable to transport the message to the relay server.</p>
<p><a href="http://aspnet.4guysfromrolla.com/code/EmailDemos.zip">Download Code</a></p>
Posted in Scripts Tagged: asp.net, attachment, delivery, email, mail, mailmessage, net.mail, SMTP, SmtpClient, System.Web.Mail <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/70/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&blog=1889208&post=70&subd=usefulscripts&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2008/09/29/sending-email-in-aspnet-20-with-attachments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>

		<media:content url="http://usefulscripts.files.wordpress.com/2008/09/email1.gif" medium="image">
			<media:title type="html">The feedback form.</media:title>
		</media:content>

		<media:content url="http://usefulscripts.files.wordpress.com/2008/09/email2.gif" medium="image">
			<media:title type="html">The received email.</media:title>
		</media:content>

		<media:content url="http://usefulscripts.files.wordpress.com/2008/09/email3.gif" medium="image">
			<media:title type="html">The HTML-formatted email.</media:title>
		</media:content>

		<media:content url="http://usefulscripts.files.wordpress.com/2008/09/email4.gif" medium="image">
			<media:title type="html">The user is prompted for a file to upload.</media:title>
		</media:content>

		<media:content url="http://usefulscripts.files.wordpress.com/2008/09/email5.gif" medium="image">
			<media:title type="html">The email, which includes the attachment.</media:title>
		</media:content>

		<media:content url="http://usefulscripts.files.wordpress.com/2008/09/email6.gif" medium="image">
			<media:title type="html">An error message is displayed in a client-side alert box.</media:title>
		</media:content>
	</item>
		<item>
		<title>How to get connection strings to various databases in ASP.NET 2.0</title>
		<link>http://usefulscripts.wordpress.com/2008/09/09/how-to-get-connection-strings-to-various-databases-in-aspnet-20/</link>
		<comments>http://usefulscripts.wordpress.com/2008/09/09/how-to-get-connection-strings-to-various-databases-in-aspnet-20/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 14:11:12 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[C# Script]]></category>
		<category><![CDATA[Microsoft Track]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[AppSettings]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[ASP.NET 2.0]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[ConfigurationManager]]></category>
		<category><![CDATA[connection]]></category>
		<category><![CDATA[connection string]]></category>
		<category><![CDATA[ConnectionString]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[provider]]></category>
		<category><![CDATA[SQL Serevr 2005]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[SqlConnection]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=63</guid>
		<description><![CDATA[How to use connection strings to various Databases in ASP.NET 2.0
Some of the values and strings are used across all the pages of a website. It is a tedious process to code those values and strings in each page of the website.
The connection string will be used in many pages of the web site if [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&blog=1889208&post=63&subd=usefulscripts&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong><span style="font-size:14pt;font-family:Verdana;">How to use connection strings to various Databases in ASP.NET 2.0</span></strong></p>
<p style="line-height:125%;"><span style="font-family:Verdana;">Some of the values and strings are used across all the pages of a website. It is a tedious process to code those values and strings in each page of the website.</span></p>
<p style="line-height:125%;"><span style="font-family:Verdana;">The connection string will be used in many pages of the web site if you are developing a database driven website. Let us say that you have hard coded the connection string in all the pages, around 30 pages. Now if the value of the connection string changes, you have a headache of changing all the values of the connection string in all the 30 pages. This can’t be done easily and if you are to change the string again after about one month, it is going to be a tedious task. </span></p>
<p style="line-height:125%;"><span style="font-family:Verdana;">Hence in such scenarios it is a practice to store those values in a central repository from where you can retrieve it and use it in all the pages. For example if you store the connection string in a central place like a <strong>web.config</strong> file you can retrieve those connection strings from the <strong>web.config</strong> file and use it. Previous to ASP.Net 2.0 the connection strings were stored <strong>in web.config</strong> file as given below:</span></p>
<p><strong><span style="font-family:Verdana;">&lt;configuration&gt;<br />
&lt;appSettings&gt;<br />
&lt;add key=&#8221;connectionString&#8221; value=&#8221;connection_string_value_here&#8221; /&gt;<br />
&lt;/appSettings&gt;</span></strong></p>
<p><strong><span style="font-family:Verdana;">&lt;system.web&gt;<br />
&#8230;<br />
&lt;/system.web&gt;<br />
&lt;/configuration&gt;</span></strong></p>
<p><span style="font-family:Verdana;">The connection string is stored as key and value pair in the &lt;appSettings&gt; element of the <strong>web.config</strong> file. This value can be retrieved by writing code like:</span></p>
<p><strong><span style="font-family:Verdana;">ConfigurationSettings.AppSettings(&#8220;connectionString&#8221;)</span></strong></p>
<p><span style="font-family:Verdana;">Instead of using the <strong>&lt;appSettings&gt;</strong> section you can also add your own sections in the <strong>web.config</strong> file to have all your string value that you will be using in your application. </span></p>
<p><span style="text-decoration:underline;"><strong><span style="font-family:Verdana;">ASP.Net 2.0 provides with a special class for retrieving those customized string values</span></strong><span style="font-family:Verdana;">. </span></span></p>
<p><span style="font-family:Verdana;">This class is called the <strong>ConfigurationManager </strong>class. </span></p>
<p><span style="font-family:Verdana;">Using this class you will be retrieving values by writing the following code for the above config file.</span></p>
<p><strong><span style="font-family:Verdana;">ConfigurationManager.AppSettings["connectionString"]</span></strong></p>
<p><span style="font-family:Verdana;">To open the ConnectionStrings section of the web.config file, you can use code like</span></p>
<p><strong><span style="font-family:&quot;">ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString</span></strong></p>
<p><span style="font-family:Verdana;">The above code is used to retrieve ConnectionStrings from the web.config file as given below:</span></p>
<p><strong><span style="font-family:&quot;">&lt;connectionStrings&gt;<br />
&lt;add name=&#8221;connectionString&#8221; connectionString=&#8221;Data Source=.;Initial Catalog=db_products;Integrated Security=True&#8221; providerName=&#8221;System.Data.SqlClient&#8221; /&gt;<br />
&lt;/connectionStrings&gt;</span></strong></p>
<p><span style="font-family:Verdana;">This code comes under the <strong>&lt;configuration&gt;</strong> section of the web.config file.</span></p>
<p><span style="font-family:Verdana;">Here you can use the </span><strong><span style="font-family:Verdana;">connectionStrings</span></strong><span style="font-family:Verdana;"> tag to store multiple Connection string to multiple databases and use it at your might.</span></p>
<p><span style="font-family:Verdana;">Like:</span></p>
<p><strong><span style="font-family:&quot;">&lt;connectionStrings&gt;<br />
&lt;add name=&#8221;connectionString&#8221; connectionString=&#8221;Connect_String_Value&#8221; providerName=&#8221;Connection_Provider&#8221; /&gt;</span></strong></p>
<p><strong>&lt;add name=&#8221;myCString&#8221; connectionString=&#8221;Connect_String_Value&#8221; providerName=&#8221;Connection_Provider&#8221; /&gt;</strong></p>
<p><strong>&lt;add name=&#8221;newCString&#8221; connectionString=&#8221;Connect_String_Value&#8221; providerName=&#8221;Connection_Provider&#8221; /&gt;<br />
&lt;/connectionStrings&gt;</strong></p>
<p><span style="font-family:Verdana;">You can use it in you code like :</span></p>
<p><span style="font-family:Verdana;">1. </span><strong><span style="font-family:Verdana;">ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString</span></strong></p>
<p><span style="font-family:Verdana;">2. </span><strong><span style="font-family:Verdana;">ConfigurationManager.ConnectionStrings["myCString"].ConnectionString</span></strong></p>
<p><span style="font-family:Verdana;">3. </span><strong><span style="font-family:Verdana;">ConfigurationManager.ConnectionStrings["newCString"].ConnectionString</span></strong></p>
<p><span style="font-family:Verdana;"><br />
Happy Connecting to database <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/usefulscripts.wordpress.com/63/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/usefulscripts.wordpress.com/63/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&blog=1889208&post=63&subd=usefulscripts&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2008/09/09/how-to-get-connection-strings-to-various-databases-in-aspnet-20/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>Decimals handling in PHP</title>
		<link>http://usefulscripts.wordpress.com/2008/09/08/decimals-handling-in-php/</link>
		<comments>http://usefulscripts.wordpress.com/2008/09/08/decimals-handling-in-php/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 19:34:35 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[Basic Scripts]]></category>
		<category><![CDATA[Php-MySQL]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[decimal]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[number_format]]></category>
		<category><![CDATA[round]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=58</guid>
		<description><![CDATA[

PHP Round Off Decimal or Convert to Decimal Point
Round Off Decimal
Round a number up or down or Round off a floating decimal point number using PHP&#8217;s functions round(). 
Example. round();


 
 

&#60;?php
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&blog=1889208&post=58&subd=usefulscripts&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div></div>
<p><span style="font-size:9pt;font-family:&quot;"></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><strong><span style="font-size:14pt;line-height:115%;"><span style="font-family:Calibri;">PHP Round Off Decimal or Convert to Decimal Point</span></span></strong></p>
<p><strong><span style="font-size:9pt;font-family:&quot;">Round Off Decimal</span></strong></p>
<p><span style="font-size:9pt;font-family:&quot;">Round a number up or down or Round off a floating decimal point number using PHP&#8217;s functions round(). </span></p>
<p><span style="font-size:9pt;font-family:&quot;">Example. round();</span></p>
<div></div>
<p><span style="font-family:&quot;"></p>
<div><span style="font-family:&quot;"> </span></div>
<p> </p>
<p></span></span></p>
<div><span style="font-size:9pt;font-family:&quot;"><span style="color:#339900;">&lt;?php<br />
echo round(3.4); // 3<br />
echo round(3.5); // 4<br />
echo round(3.6); // 4<br />
echo round(3.6, 0); // 4<br />
echo round(1.95583, 2); // 1.96<br />
echo round(1241757, -3); // 1242000<br />
echo round(5.045, 2); // 5.05<br />
echo round(5.055, 2); // 5.06<br />
?&gt;</span></span></div>
<p><strong><span style="font-size:9pt;font-family:&quot;">Convert to Decimal Point</span></strong></p>
<p><span style="font-size:9pt;font-family:&quot;">Example. number_format();</span></p>
<div><span style="font-size:9pt;font-family:&quot;"><br />
<span style="color:#339900;">&lt;?php<br />
$number = &#8220;28&#8243;;<br />
$number = number_format($number, 2);<br />
echo $number; // equals &#8220;28.00&#8243;<br />
?&gt;</span></span></div>
<div><span style="font-size:9pt;font-family:&quot;"> </span></div>
<div><span style="font-size:9pt;font-family:&quot;"> </span></div>
<p><span style="font-size:9pt;font-family:&quot;"> </p>
<p></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 6pt;"><strong><span style="font-size:13pt;color:#4c4c4c;font-family:&quot;">PHP number formatting for decimal places</span></strong></p>
<div><span style="font-size:9pt;color:#000000;line-height:115%;font-family:&quot;">PHP has powerful math functions. </span></div>
<div><span style="font-size:9pt;color:#000000;line-height:115%;font-family:&quot;">Formatting a number is required while displaying or preparing for some other processing. </span></div>
<div><span style="font-size:9pt;color:#000000;line-height:115%;font-family:&quot;">One common requirement is displaying values after a division with fractions. </span></div>
<div><span style="font-size:9pt;color:#000000;line-height:115%;font-family:&quot;">One way is to round off the number or we can format the number with specifying whether to display the digits with decimal or without decimal or number of digits to display after the decimal. </span></div>
<div><span style="font-size:9pt;color:#000000;line-height:115%;font-family:&quot;">We can place comma ( ,) after a group of thousands. We can use other symbols in place of comma (,) for thousands and we also can replace (.) dot with symbol of our choice to be used as decimal. </span></div>
<div><span style="font-size:9pt;color:#000000;line-height:115%;font-family:&quot;">Using PHP <strong>number_format</strong> function we can do all these formatting. Here are some examples for number formatting.</span></div>
<div></div>
<p><span style="font-size:9pt;color:#000000;line-height:115%;font-family:&quot;"></p>
<div><span style="background:#f1f1f1;">$number=2512589.66785;</span></div>
<div></div>
<div><span style="background:#f1f1f1;">echo &#8220;Number = $number &#8220;;</span></div>
<div><span style="background:#f1f1f1;">echo &#8221; The value after formating = &#8220;.number_format($number);</span></div>
<div><span style="background:#f1f1f1;"><br />
// The above line will display 2,512,590 ( without decimal and rounded)</span></div>
<p><span style="background:#f1f1f1;">echo &#8220;&lt;br&gt; The value after formating = &#8220;.number_format($number,3);<br />
// Will display 2,512,589.668 ( with , after each thousand and 3 digits after decimal<br />
// rounded )</p>
<p>echo &#8220;&lt;br&gt; The value after formating = &#8220;.number_format($number,3,&#8221;#&#8221;,&#8221;:&#8221;);<br />
// Will display 2:512:589#668 ( with : after each thousand and # as decimal symbol )</p>
<p>Happy Coding <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p> </p>
<p></span></span></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/usefulscripts.wordpress.com/58/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/usefulscripts.wordpress.com/58/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/58/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&blog=1889208&post=58&subd=usefulscripts&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2008/09/08/decimals-handling-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
	</channel>
</rss>