<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://codeforeternity.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>code for eternity !!!</title><link>http://codeforeternity.com/blogs/</link><description>community website for .net freaks ;-)</description><dc:language>en-US</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>LINQ ElementAt and ElementAtOrDefault</title><link>http://codeforeternity.com/blogs/technology/archive/2008/08/13/linq-elementat-and-elementatordefault.aspx</link><pubDate>Wed, 13 Aug 2008 14:02:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:253</guid><dc:creator>raj</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;We can use the ElementAt extension method of LINQ to return the element at a specified index (zero based) in a sequence. However the ElementAt extension method would throw the System.ArguementOutOfRangeException when the specified index is a negative value or not less than the size of the sequence. In such a scenario, we can use the ElementAtOrDefault extenion method which would return the default value of a type instead of throwing the System.ArguementOutOfRangeException.&lt;/p&gt;
&lt;p&gt;Code example (both in C# and VB.NET) with comments below:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Create a new generic list of ints&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; List&amp;lt;int&amp;gt; l = new List&amp;lt;int&amp;gt;();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(1); &lt;font color="#008000"&gt;// Add 1 to the list&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(5); &lt;font color="#008000"&gt;// Add 5 to the list&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(3); &lt;font color="#008000"&gt;// Add 3 to the list&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns 1 as 1 exists at index 0&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int value = l.ElementAt(0);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns 3 as 3 exists at index 2&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.ElementAt(2);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns the default value of int which is 0 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since no element in the list exists at index 3&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.ElementAtOrDefault(3);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Throws System.ArguementOutOfRangeException &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since no element in the list exists at index 3&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.ElementAt(3);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB.NET:&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Create a new generic list of ints&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim l As New List(Of Integer)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(1) &lt;font color="#008000"&gt;&amp;#39; Add 1 to the list&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(5) &lt;font color="#008000"&gt;&amp;#39; Add 5 to the list&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(3) &lt;font color="#008000"&gt;&amp;#39; Add 3 to the list&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns 1 as 1 exists at index 0&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim value As Integer = l.ElementAt(0)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns 3 as 3 exists at index 2&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.ElementAt(2)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns the default value of int which is 0 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since no element in the list exists at index 3 &lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.ElementAtOrDefault(3)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Throws System.ArguementOutOfRangeException&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since no element in the list exists at index 3 &lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.ElementAt(3)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=253" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category></item><item><title>T-SQL Function to Get Int From Varchar</title><link>http://codeforeternity.com/blogs/technology/archive/2008/05/20/t-sql-function-to-get-int-from-varchar.aspx</link><pubDate>Tue, 20 May 2008 10:48:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:195</guid><dc:creator>raj</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;I recently had to write a T-SQL function which cleans up all non numeric characters from a varchar variable and returns the int value of the remaining numeric characters. If no numeric characters exist, the function should return zero. I thought it would be nice to share this with you and I hope you find this useful ;-) Here is the function:&lt;/p&gt;
&lt;div style="OVERFLOW:auto;WIDTH:100%;HEIGHT:100%;"&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CREATE FUNCTION [dbo].[GetIntFromVarchar]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @originalString varchar(50)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; )&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; RETURNS int&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AS&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- declare local variable to hold numeric characters&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- and assign default value of empty string&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DECLARE @retVal varchar(50)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET @retVal = &amp;#39;&amp;#39;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- declare local variable to loop through @originalString&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- and assign initial value of 1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DECLARE @loop int&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET @loop = 1&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- loop through every character in @originalString&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHILE @loop &amp;lt;= LEN(@originalString) &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IF ISNUMERIC(SUBSTRING(@originalString, @loop, 1)) = 1&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- if numeric character found, append it to @retVal&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET @retVal = @retVal + SUBSTRING(@originalString, @loop, 1)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET @loop = @loop + 1 -- increment value of @loop by 1&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- set @retVal to &amp;#39;0&amp;#39; if no numeric characters found&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IF @retVal = &amp;#39;&amp;#39;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET @retVal = &amp;#39;0&amp;#39;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; END&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- return int value of numeric characters found &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- by casting @retVal to int&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RETURN CAST(@retVal as int)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; END&lt;/font&gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Once you have created this function, execute below T-SQL statements to verify the same:&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PRINT dbo.GetIntFromVarchar(&amp;#39;R1A2J3&amp;#39;) -- returns 123&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PRINT dbo.GetIntFromVarchar(&amp;#39;COOL456CODER&amp;#39;) -- returns 456&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PRINT dbo.GetIntFromVarchar(&amp;#39;789&amp;#39;) -- returns 789&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PRINT dbo.GetIntFromVarchar(&amp;#39;GEEK&amp;#39;) -- returns 0&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=195" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Developers are Born Brave</title><link>http://codeforeternity.com/blogs/technology/archive/2008/05/16/developers-are-born-brave.aspx</link><pubDate>Fri, 16 May 2008 16:15:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:186</guid><dc:creator>raj</dc:creator><slash:comments>9</slash:comments><description>&lt;p&gt;Reality about software development. Below image says it all ;-) Cheers to every coder reading this blog post&amp;nbsp;:-) Please scroll to the right if you cannot see the entire image at 1 glance or click&amp;nbsp;&lt;a class="" title="Developers are Born Brave" href="http://codeforeternity.com/blogs/technology/DevelopersAreBornBrave_Small.jpg" target="_blank"&gt;here&lt;/a&gt;&amp;nbsp;to view / download the image in a seperate window.&lt;/p&gt;
&lt;div style="OVERFLOW:auto;WIDTH:100%;HEIGHT:400px;"&gt;&lt;img height="393" alt="Developers are Born Brave" src="http://codeforeternity.com/blogs/technology/DevelopersAreBornBrave_Small.jpg" width="840" border="0" /&gt;&lt;/div&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=186" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Management/default.aspx">Management</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Humor/default.aspx">Humor</category></item><item><title>Coders versus Project Managers</title><link>http://codeforeternity.com/blogs/technology/archive/2008/05/14/coders-versus-project-managers.aspx</link><pubDate>Wed, 14 May 2008 10:15:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:177</guid><dc:creator>raj</dc:creator><slash:comments>4</slash:comments><description>&lt;p&gt;Few months back, I got below joke emailed to me by my friend who is also a passionate coder. In my personal (and humble) opinion, below joke hits jackpot with the current state of 9 out of 10 IT companies in India, which are headed by people who have zero or very little technical background. And perhaps this explains why Indian IT companies still have to market themselves as low cost outsourcing destinations to win business any why we still dont have too many Product based IT companies in India :-) Is the story same in your country too?&amp;nbsp;Let me know, I would love to hear :-)&lt;/p&gt;
&lt;p&gt;Here is the joke, enjoy and forward this to all your coder friends ;-)&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;A woman in a hot air balloon realized she was lost.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;She reduced altitude and spotted a man below. She descended a bit more and shouted, &amp;quot;Excuse me, can you help me? I promised a friend I would meet him an hour ago but I don&amp;#39;t know where I am.&amp;quot;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;The man below replied, &amp;quot;You&amp;#39;re in a hot air balloon hovering approximately 30 feet above the ground. You&amp;#39;re between 40 and 41 degrees north latitude and between 59 and 60 degrees west longitude.&amp;quot;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;quot;You must be an engineer&amp;quot; said the balloonist.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;quot;I am&amp;quot;, replied the man. &amp;quot;How did you know?&amp;quot;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;quot;Well&amp;quot;, answered the balloonist, &amp;quot;everything you told me is technically correct, but I&amp;#39;ve no idea what to make of your information, and the fact is I&amp;#39;m still lost. Frankly, you&amp;#39;ve not been much help at all. If anything you&amp;#39;ve delayed my trip even more.&amp;quot;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;The man below responded, &amp;quot;You must be in management.&amp;quot;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;quot;I am,&amp;quot; replied the balloonist, &amp;quot;but how did you know?&amp;quot;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;quot;Well&amp;quot;, said the man, &amp;quot;You don&amp;#39;t know where you are or where you&amp;#39;re going. You have risen to where you are due to a large quantity of hot air. You made a promise which you&amp;#39;ve no idea how to keep, and you expect people beneath you to solve your problems&amp;quot;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=177" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Management/default.aspx">Management</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Humor/default.aspx">Humor</category></item><item><title>LINQ Single and SingleOrDefault</title><link>http://codeforeternity.com/blogs/technology/archive/2008/05/10/linq-single-and-singleordefault.aspx</link><pubDate>Sat, 10 May 2008 13:27:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:167</guid><dc:creator>raj</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;We can use the Single extension method of LINQ to return the only element in a sequence that satisfies a specified condition. However the Single extension method would throw the System.InvalidOperationException when no element in a sequence satisfies a specified condition. In such a scenario, we can use the SingleOrDefault extenion method which would return the default value of a type instead of throwing the System.InvalidOperationException.&lt;/p&gt;
&lt;p&gt;Also, both Single as well as SingleOrDefault&amp;nbsp;extension methods would throw the System.InvalidOperationException when more than one element in a sequence satisfy a specified condition. We can use&amp;nbsp;&lt;a class="" title="LINQ First and FirstOrDefault" href="http://codeforeternity.com/blogs/technology/archive/2008/04/25/linq-first-and-firstordefault-methods.aspx" target="_blank"&gt;First&lt;/a&gt; or&amp;nbsp;&lt;a class="" title="LINQ Last and LastOrDefault" href="http://codeforeternity.com/blogs/technology/archive/2008/04/27/linq-last-and-lastordefault-methods.aspx" target="_blank"&gt;Last&lt;/a&gt; extensions methods of LINQ in such scenarios.&lt;/p&gt;
&lt;p&gt;Code example (both in C# and VB.NET) with comments below:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Create a new generic list of ints&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; List&amp;lt;int&amp;gt; l = new List&amp;lt;int&amp;gt;();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(1); &lt;font color="#008000"&gt;// Add 1 to the list&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(5); &lt;font color="#008000"&gt;// Add 5 to the list&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(3); &lt;font color="#008000"&gt;// Add 3 to the list&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns 1 as only 1 satisfies the condition&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int value = l.Single(i =&amp;gt; i == 1);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns the default value of int which is 0 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since no element in the list equals 4&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.SingleOrDefault(i =&amp;gt; i == 4);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Throws System.InvalidOperationException &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since no element in the list equals 4 &lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.Single(i =&amp;gt; i == 4);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Throws System.InvalidOperationException&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since both 5 and 3 are greater than 1 &lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.Single(i =&amp;gt; i &amp;gt; 1);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.SingleOrDefault(i =&amp;gt; i &amp;gt; 1);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB.NET:&amp;nbsp;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Create a new generic list of ints&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim l As New List(Of Integer)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(1) &lt;font color="#008000"&gt;&amp;#39; Add 1 to the list&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(5) &lt;font color="#008000"&gt;&amp;#39; Add 5 to the list&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(3) &lt;font color="#008000"&gt;&amp;#39; Add 3 to the list&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns 1 as only 1 satisfies the condition&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim value As Integer = l.Single(Function(i) i = 1)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns the default value of int which is 0 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since no element in the list equals 4&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.SingleOrDefault(Function(i) i = 4)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Throws System.InvalidOperationException &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since no element in the list equals 4&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.Single(Function(i) i = 4)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Throws System.InvalidOperationException&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since both 5 and 3 are greater than 1 &lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.Single(Function(i) i &amp;gt; 1)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.SingleOrDefault(Function(i) i &amp;gt; 1)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=167" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category></item><item><title>AddItem Extension Method for BulletedList, CheckBoxList, DropDownList, ListBox, RadioButtonList</title><link>http://codeforeternity.com/blogs/technology/archive/2008/05/09/additem-extension-method-for-bulletedlist-checkboxlist-dropdownlist-listbox-radiobuttonlist.aspx</link><pubDate>Fri, 09 May 2008 15:03:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:164</guid><dc:creator>raj</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;We would have to write the below code to add a new item (with value) to any of these 5 controls (BulletedList, CheckBoxList, DropDownList, ListBox, RadioButtonList)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; bulletedList.Items.Add(new ListItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; checkBoxList.Items.Add(new ListItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dropDownList.Items.Add(new ListItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; listBox.Items.Add(new ListItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; radioButtonList.Items.Add(new ListItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;));&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; bulletedList.Items.Add(New ListItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; checkBoxList.Items.Add(New ListItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dropDownList.Items.Add(New ListItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; listBox.Items.Add(New ListItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; radioButtonList.Items.Add(New ListItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;))&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I find it quite painful to write so much code just to add a new item (with value) to these controls and so I created a new AddItem extension method for the ListControl class (since all these 5 controls inherit from the ListControl class)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void AddItem(this ListControl lc, string text, string value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lc.Items.Add(new ListItem(text, value));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Extension()&amp;gt; _&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Sub AddItem(ByVal lc As ListControl, ByVal text As String, ByVal value As String)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lc.Items.Add(New ListItem(text, value))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Now I can simply write the above code in a much simpler and cleaner way like below:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; bulletedList.AddItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; checkBoxList.AddItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dropDownList.AddItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; listBox.AddItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; radioButtonList.AddItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; bulletedList.AddItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; checkBoxList.AddItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dropDownList.AddItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; listBox.AddItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; radioButtonList.AddItem(&amp;quot;text&amp;quot;, &amp;quot;value&amp;quot;)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Just 1 extension method which works for 5 controls to solve the problem :-) Cheers to object inheritance and cheers to Extension Methods ;-)&lt;/p&gt;
&lt;p&gt;Note: You can find a list of other cool and useful Extension Methods coded by me &lt;a class="" href="http://codeforeternity.com/tags/Extension+Methods/default.aspx" target="_blank"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=164" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Code+Better/default.aspx">Code Better</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Extension+Methods/default.aspx">Extension Methods</category></item><item><title>Using Initial Capacity Constructor of StringBuilder for Extreme Performace</title><link>http://codeforeternity.com/blogs/technology/archive/2008/05/08/using-initial-capacity-constructor-of-stringbuilder-for-extreme-performace.aspx</link><pubDate>Thu, 08 May 2008 12:13:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:161</guid><dc:creator>raj</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;You must have come across plenty of articles on the internet which talk about using the StringBuilder class when computing large strings for performance gains. Nothing wrong with that. However I have not seen many coders using the Initial Capacity constructor of the StringBuilder class which can further&amp;nbsp;improve performace.&lt;/p&gt;
&lt;p&gt;Lets take a real world example. Suppose we had to&amp;nbsp;compute a csv (comma seperated values) file for a table named Users which had the following structure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;UserID int&lt;/li&gt;
&lt;li&gt;FirstName nvarchar(20)&lt;/li&gt;
&lt;li&gt;LastName nvarchar(20)&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Our csv file would look like (for 2 records):&lt;/p&gt;
&lt;p&gt;1,Paul,Graham&lt;br /&gt;2,Scott,Guthrie&lt;/p&gt;
&lt;p&gt;Most coders would write the&amp;nbsp;below code to compute the csv file:&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Populate Users DataTable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataTable dtUsers = SomeFunctionWhichReturnsUsersDataTable();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Declare new StringBuilder&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Text.StringBuilder sb = new System.Text.StringBuilder();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Loop through Users DataTable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int&amp;nbsp;j = 0;&amp;nbsp;j &amp;lt; dtUsers.Rows.Count; j++) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Append(dtUsers.Rows[j][&amp;quot;UserID&amp;quot;].ToString()); &lt;font color="#008000"&gt;// Append UserID&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Append(&amp;quot;,&amp;quot;); &lt;font color="#008000"&gt;// Append comma&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Append(dtUsers.Rows[j][&amp;quot;FirstName&amp;quot;].ToString()); &lt;font color="#008000"&gt;// Append FirstName&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Append(&amp;quot;,&amp;quot;); &lt;font color="#008000"&gt;// Append comma&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Append(dtUsers.Rows[j][&amp;quot;LastName&amp;quot;].ToString()); &lt;font color="#008000"&gt;// Append LastName&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.AppendLine(); &lt;font color="#008000"&gt;// Append new line&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return sb.ToString(); &lt;font color="#008000"&gt;// Return StringBuilder contents&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Now lets write the above code more intelligently by using the Initial Capacity constructor of the StringBuilder class. We can actually guess the approximate length of the csv file before hand by considering below points:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Max length of UserID can be 10 characters, since max value of UserID can be 2147483647, since its datatype is int&lt;/li&gt;
&lt;li&gt;Max length of FirstName can be 20 characters, since its datatype is nvarchar(20)&lt;/li&gt;
&lt;li&gt;Max length of LastName can be 20 characters, since its datatype is nvarchar(20)&lt;/li&gt;
&lt;li&gt;Also each csv file record has 2 comma characters which act as seperators, and 1 new line character&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Therefore the max length per record can be 54 characters (10[UserID] + 20[FirstName] + 20[LastName] + 2[2 comma characters] + 2[1 new line character]). So&amp;nbsp;now we are absolutely sure that our csv file would have a max length of (54 * Number of Records) characters.&lt;/p&gt;
&lt;p&gt;If you are hell bent on having just one and only one memory allocation for the StringBuilder class, go ahead and set the Initial Capacity of the StringBuilder class&amp;nbsp;to (54 * Number of Rows). However doing this would more often than not result in a lot of memory wastage as not all records would have their UserID set to 10 digit integers or their FirstName and LastName set to 20 character long strings. Therefore I usually follow the divide by 2 rule where I divide the max length per record value by 2. This way I am sure there wont ever be more than 2 memory (re)allocations and more often than not just a single memory allocation would do the job. Below is the intelligent version of above code:&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Populate Users DataTable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataTable dtUsers = SomeFunctionWhichReturnsUsersDataTable();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Set a value of 54 to maxLengthPerRecord&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int maxLengthPerRecord = 54;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Apply divide by 2 rule&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; maxLengthPerRecord = maxLengthPerRecord / 2;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Compute initialCapacity value&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int initialCapacity = dtUsers.Rows.Count * maxLengthPerRecord; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Declare new StringBuilder using the Initial Capacity constructor&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Text.StringBuilder sb = new System.Text.StringBuilder(initialCapacity);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Loop through Users DataTable&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int&amp;nbsp;j = 0;&amp;nbsp;j &amp;lt; dtUsers.Rows.Count; j++) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Append(dtUsers.Rows[j][&amp;quot;UserID&amp;quot;].ToString()); &lt;font color="#008000"&gt;// Append UserID&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Append(&amp;quot;,&amp;quot;); &lt;font color="#008000"&gt;// Append comma&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Append(dtUsers.Rows[j][&amp;quot;FirstName&amp;quot;].ToString()); &lt;font color="#008000"&gt;// Append FirstName&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Append(&amp;quot;,&amp;quot;); &lt;font color="#008000"&gt;// Append comma&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Append(dtUsers.Rows[j][&amp;quot;LastName&amp;quot;].ToString()); &lt;font color="#008000"&gt;// Append LastName&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.AppendLine(); &lt;font color="#008000"&gt;// Append new line&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return sb.ToString(); &lt;font color="#008000"&gt;// Return StringBuilder contents&lt;/font&gt;&lt;/font&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You might be thinking this is&amp;nbsp;too much of an effort to save on a few memory reallocations. But the geek in me tries to visualize the performance gains and number of memory reallocations (read garbage collection cycles which are so expensive) which can be saved if we had to do the same task for a table with many columns and tens of thousands of records :-)&lt;/p&gt;
&lt;p&gt;Note: You can also consider dividing the max length per record by 3 or even 4, it all depends on your data structures and data patterns. Also the above code example uses a DataTable. However you can apply the same logic on a generic list or a DataReader&amp;nbsp;as well.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=161" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Code+Better/default.aspx">Code Better</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Performance/default.aspx">Performance</category></item><item><title>T-SQL Query to Find the Second Lowest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/04/30/t-sql-query-to-find-the-second-lowest-column-value-in-a-table.aspx</link><pubDate>Wed, 30 Apr 2008 08:41:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:146</guid><dc:creator>raj</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;After reading my earlier post &lt;a class="" href="http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx" target="_blank"&gt;T-SQL Query to Find the Second Highest Column Value in a Table&lt;/a&gt;&amp;nbsp;a lot of users asked me to also help&amp;nbsp;them with writing a query&amp;nbsp;to find the second lowest column value in a table. So here it is:&amp;nbsp;If we had a table named Employee which had a column named Salary and we had to find the second&amp;nbsp;lowest Salary in the Employee table, the query for the same would be:&lt;/p&gt;&lt;font color="#0000ff" size="2"&gt;
&lt;p&gt;SELECT&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;TOP&lt;/font&gt;&lt;font size="2"&gt; 1 Salary &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;FROM&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;SELECT&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;TOP&lt;/font&gt;&lt;font size="2"&gt; 2 Salary &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;FROM&lt;/font&gt;&lt;font size="2"&gt; Employee &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;ORDER&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;BY&lt;/font&gt;&lt;font size="2"&gt; Salary &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;ASC&lt;/font&gt;&lt;font color="#808080" size="2"&gt;)&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;AS&lt;/font&gt;&lt;font size="2"&gt; E &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;ORDER&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;BY&lt;/font&gt;&lt;font size="2"&gt; Salary &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;DESC&lt;/p&gt;&lt;/font&gt;
&lt;p&gt;If we ran the above query against the Employee table which had the following 5 rows:&lt;/p&gt;
&lt;p&gt;5000&lt;br /&gt;4000&lt;br /&gt;3000&lt;br /&gt;2000&lt;br /&gt;1000&lt;/p&gt;
&lt;p&gt;The subquery or the inner query would return the top 2 rows in ascending Salary order which would be:&lt;/p&gt;
&lt;p&gt;1000&lt;br /&gt;2000&lt;/p&gt;
&lt;p&gt;The outer query would then select the top 1 row from the subquery results in descending Salary order which would be:&lt;/p&gt;
&lt;p&gt;2000&lt;/p&gt;
&lt;p&gt;Note that if we had to get the fourth lowest Salary, we could do so by simply changing the subquery from TOP 2 to TOP 4&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=146" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>LINQ Last and LastOrDefault</title><link>http://codeforeternity.com/blogs/technology/archive/2008/04/27/linq-last-and-lastordefault-methods.aspx</link><pubDate>Sun, 27 Apr 2008 08:31:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:138</guid><dc:creator>raj</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;We can use the Last extension method of LINQ to return the last element in a sequence that satisfies a specified condition. However the Last extension method would throw the System.InvalidOperationException when no element in a sequence satisfies a specified condition. In such a scenario, we can use the LastOrDefault extenion method which would return the default value of a type instead of throwing the System.InvalidOperationException.&lt;/p&gt;
&lt;p&gt;Code example (both in C# and VB.NET) with comments below:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Create a new generic list of ints&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; List&amp;lt;int&amp;gt; l = new List&amp;lt;int&amp;gt;();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(1); &lt;font color="#008000"&gt;// Add 1 to the list&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(5); &lt;font color="#008000"&gt;// Add 5 to the list&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(3); &lt;font color="#008000"&gt;// Add 3 to the list&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns 1 as only 1 satisfies the condition&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int value = l.Last(i =&amp;gt; i == 1);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns 3 although both 5 and 3 are greater than 1 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since 3 appears last in the list&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.Last(i =&amp;gt; i &amp;gt; 1);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns the default value of int which is 0 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since no element in the list equals 4&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.LastOrDefault(i =&amp;gt; i == 4);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Throws System.InvalidOperationException &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since no element in the list equals 4 &lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.Last(i =&amp;gt; i == 4);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB.NET:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Create a new generic list of ints&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim l As New List(Of Integer)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(1) &lt;font color="#008000"&gt;&amp;#39; Add 1 to the list&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(5) &lt;font color="#008000"&gt;&amp;#39; Add 5 to the list&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(3) &lt;font color="#008000"&gt;&amp;#39; Add 3 to the list&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns 1 as only 1 satisfies the condition&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim value As Integer = l.Last(Function(i) i = 1)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns 3 although both 5 and 3 are greater than 1 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since 3 appears last in the list&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.Last(Function(i) i &amp;gt; 1)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns the default value of int which is 0 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since no element in the list equals 4&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.LastOrDefault(Function(i) i = 4)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Throws System.InvalidOperationException &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since no element in the list equals 4 &lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.Last(Function(i) i = 4)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=138" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category></item><item><title>LINQ First and FirstOrDefault</title><link>http://codeforeternity.com/blogs/technology/archive/2008/04/25/linq-first-and-firstordefault-methods.aspx</link><pubDate>Fri, 25 Apr 2008 17:22:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:136</guid><dc:creator>raj</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;We can use the First extension method of LINQ to return the first element in a sequence that satisfies a specified condition. However the First extension method would throw the System.InvalidOperationException when no element in a sequence satisfies a specified condition. In such a scenario, we can use the FirstOrDefault extenion method which would return the default value of a type instead of throwing the System.InvalidOperationException.&lt;/p&gt;
&lt;p&gt;Code example&amp;nbsp;(both in C# and VB.NET)&amp;nbsp;with comments below:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Create a new generic list of ints&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; List&amp;lt;int&amp;gt; l = new List&amp;lt;int&amp;gt;(); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(1); &lt;font color="#008000"&gt;// Add 1 to the list&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(5); &lt;font color="#008000"&gt;// Add 5 to the list&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(3); &lt;font color="#008000"&gt;// Add 3 to the list&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns 1 as only 1 satisfies the condition&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int value = l.First(i =&amp;gt; i == 1);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns 5 although both 5 and 3 are greater than 1 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since 5 appears first in the list&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.First(i =&amp;gt; i &amp;gt; 1); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Returns the default value of int which is 0 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since no element in the list equals 4&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.FirstOrDefault(i =&amp;gt; i == 4); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Throws System.InvalidOperationException &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // since no element in the list equals 4 &lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.First(i =&amp;gt; i == 4); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB.NET:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Create a new generic list of ints&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim l As New List(Of Integer)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(1) &lt;font color="#008000"&gt;&amp;#39; Add 1 to the list&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(5) &lt;font color="#008000"&gt;&amp;#39; Add 5 to the list&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; l.Add(3) &lt;font color="#008000"&gt;&amp;#39; Add 3 to the list&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns 1 as only 1 satisfies the condition&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim value As Integer = l.First(Function(i) i = 1)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns 5 although both 5 and 3 are greater than 1 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since 5 appears first in the list&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.First(Function(i) i &amp;gt; 1)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Returns the default value of int which is 0 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since no element in the list equals 4&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.FirstOrDefault(Function(i) i = 4)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Throws System.InvalidOperationException &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; since no element in the list equals 4 &lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = l.First(Function(i) i = 4)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=136" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category></item><item><title>Blackle - Energy Saving Search</title><link>http://codeforeternity.com/blogs/technology/archive/2008/04/24/blackle-energy-saving-search.aspx</link><pubDate>Thu, 24 Apr 2008 19:45:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:134</guid><dc:creator>raj</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;Google is the second brain for many coders hungry for information. I as a coder cannot imagine a single day at work without Google. Few days back (22 April to be precise), it was Earth&amp;#39;s day and I was wondering how we as coders can do our bit for our planet and fight against Global Warming.&amp;nbsp;So I thought I would blog about Blackle :-)&lt;/p&gt;
&lt;p&gt;The idea behind Blackle: Google uses white background which consumes more power, and considering the huge number of queries&amp;nbsp;Google gets (about 200 million each day), according to calculations, a black version of Google would save 750 mega watts / hour per year.&lt;/p&gt;
&lt;p&gt;So next time you want to search for information, try &lt;a class="" href="http://www.blackle.com/" target="_blank"&gt;http://www.blackle.com&lt;/a&gt; :-) For more info on Blackle, visit &lt;a class="" href="http://www.blackle.com/about" target="_blank"&gt;http://www.blackle.com/about&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Also&amp;nbsp;help spread&amp;nbsp;the word about Blackle&amp;nbsp;by telling your coder friends about it, ask them to blog about&amp;nbsp;Blackle incase they have a blog&amp;nbsp;;-)&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=134" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Google/default.aspx">Google</category></item><item><title>Cool Extension Methods for IDataReader Interface</title><link>http://codeforeternity.com/blogs/technology/archive/2008/04/15/cool-extension-methods-for-idatareader-interface.aspx</link><pubDate>Tue, 15 Apr 2008 22:34:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:128</guid><dc:creator>raj</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;I find it really annoying to use the GetOrdinal method of the DataReader class everytime I have to access data in a strongly typed manner using any of the Get[DataType] methods of the DataReader class. Have a look at below code:&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int employeeID = dr.GetInt32(dr.GetOrdinal(&amp;quot;EmployeeID&amp;quot;));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string firstName = dr.GetString(dr.GetOrdinal(&amp;quot;FirstName&amp;quot;));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool enabled = dr.GetBoolean(dr.GetOrdinal(&amp;quot;Enabled&amp;quot;));&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;It would be so cool if I could directly specify the table column names like below:&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int employeeID = dr.GetInt32(&amp;quot;EmployeeID&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string firstName = dr.GetString(&amp;quot;FirstName&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool enabled = dr.GetBoolean(&amp;quot;Enabled&amp;quot;);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This would save me so much typing and make my code look so much cleaner. The power of Extension Methods make this possible :-) I created a new class named IDataReaderHelper which has 18 Extension Methods in all. You can simply pass the table column names instead of table column indexes to all these 18 Extension Methods, and these 18 Extension Methods would take care of the rest by calling the GetOrdinal method of the DataReader class internally :-). Have a look at the screen shot below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codeforeternity.com/blogs/technology/IDataReader_01.JPG"&gt;&lt;img src="http://codeforeternity.com/blogs/technology/IDataReader_01.JPG" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Note: For readability purpose, above screen shot just highlights 3 of the 18 Extension Methods present in the IDataReaderHelper class. The 18 Extension Methods present in the IDataReaderHelper class are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div&gt;GetBoolean&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetByte&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetBytes&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetChar&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetChars&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetData&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetDataTypeName&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetDateTime&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetDecimal&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetDouble&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetFieldType&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetFloat&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetGuid&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetInt16&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetInt32&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetInt64&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetString&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;GetValue&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;You can find&amp;nbsp;the entire source code for the IDataReaderHelper class as an attachment with this post&amp;nbsp;available for download. To see all my posts on Extension Methods click &lt;a class="" title="Extension Methods" href="http://codeforeternity.com/blogs/technology/archive/tags/Extension+Methods/default.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Guys please dont forget to check out the Chris&amp;#39;s comments (2nd from top)&amp;nbsp;regarding performance issues when using the&amp;nbsp;GetOrdinal method of the DataReader class inside a loop. Thanks for your excellent feedback Chris :-)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Guys please dont forget to check out programatik&amp;#39;s post&amp;nbsp;&lt;a class="" href="http://programatik.wordpress.com/2008/05/12/benchmarking-do-datareadergetordinal/" target="_blank"&gt;here&lt;/a&gt;&amp;nbsp;where he / she has compared performance (using&amp;nbsp;GetOrdinal versus using int inside a loop). His / her post is in Portuguese&amp;nbsp;so you can go to &lt;a class="" href="http://www.google.com/translate_t" target="_blank"&gt;http://www.google.com/translate_t&lt;/a&gt;&amp;nbsp;and translate programatik&amp;#39;s post&amp;nbsp;from Portuguese to English. It took me 30 minutes to figure out the language of this post ;-) Great work programatik ;-)&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=128" width="1" height="1"&gt;</description><enclosure url="http://codeforeternity.com/blogs/technology/attachment/128.ashx" length="638" type="application/x-zip-compressed" /><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Code+Better/default.aspx">Code Better</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Extension+Methods/default.aspx">Extension Methods</category></item><item><title>System.Data.DataSetExtensions Assembly Error in Visual Studio 2008 RTM</title><link>http://codeforeternity.com/blogs/technology/archive/2008/04/05/system-data-datasetextensions-assembly-error-in-visual-studio-2008-rtm.aspx</link><pubDate>Sat, 05 Apr 2008 13:27:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:124</guid><dc:creator>raj</dc:creator><slash:comments>3</slash:comments><description>&lt;p&gt;After upgrading from Visual Studio 2008 Beta 2 to Visual Studio 2008 RTM, I got the following error when I tried to build my ASP.NET 3.5 project which was initially coded using Visual Studio 2008 Beta 2:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Could not load file or assembly &amp;#39;System.Data.DataSetExtensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&amp;#39; or one of its dependencies. The system cannot find the file specified.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This error occurs because with Visual Studio 2008 RTM, a newer version (3.5) of the System.Data.DataSetExtensions assembly is available and the older version (2.0) of the System.Data.DataSetExtensions assembly&amp;nbsp;no longer exists.&lt;/p&gt;
&lt;p&gt;To resolve this error, simply change your web.config file from:&lt;/p&gt;
&lt;p&gt;&amp;lt;add assembly=&amp;quot;System.Data.DataSetExtensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;to:&lt;/p&gt;
&lt;p&gt;&amp;lt;add assembly=&amp;quot;System.Data.DataSetExtensions, &lt;strong&gt;Version=3.5.0.0&lt;/strong&gt;, Culture=neutral, PublicKeyToken=B77A5C561934E089&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=124" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>Tip/Trick: Making Non Themeable Properties of ASP.NET Controls Themeable</title><link>http://codeforeternity.com/blogs/technology/archive/2008/03/29/tip-trick-making-non-themeable-properties-of-asp-net-controls-themeable.aspx</link><pubDate>Sat, 29 Mar 2008 13:56:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:114</guid><dc:creator>raj</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;If you have been using ASP.NET themes feature in your ASP.NET projects, you surely must have come across situations where you wished you could set certain properties of controls via skins. For example, in all my ASP.NET projects, I always want to set the Display property of any validator control to Dynamic. Also, I simply hate the idea of setting the ValidationExpression property for every instance of the RegularExpressionValidator control I use in aspx / ascx files where I have to validate an email address. When I tried to set these two properties of the RegularExpressionValidator control via skin, I got the following errors:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &amp;#39;Display&amp;#39; property of a control type System.Web.UI.WebControls.RegularExpressionValidator cannot be applied through a control skin.&lt;/li&gt;
&lt;li&gt;The &amp;#39;ValidationExpression&amp;#39; property of a control type System.Web.UI.WebControls.RegularExpressionValidator cannot be applied through a control skin.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;This happens because the Display as well as the ValidationExpression properties of the RegularExpressionValidator control are not marked as Themeable. To overcome this, I found a really cool and simple solution (object inheritance to the rescue !!!). I created a new class called NewRegularExpressionValidator which inherits from the RegularExpressionValidator class, and provides a new implementation of these two properties by marking them as Themeable. I created this new class in a new class library project (named ClassLibrary1) and also added the System.Web reference to this class library project. Below is the code for the NewRegularExpressionValidator class:&lt;/p&gt;
&lt;p&gt;&lt;img height="429" alt="" src="http://codeforeternity.com/blogs/technology/Themeable/01.JPG" width="606" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Next I added a reference to ClassLibrary1 project from my ASP.NET project and defined two skins (Default skin and Email skin) for the NewRegularExpressionValidator control. Have a look at the skin definitions below:&lt;/p&gt;
&lt;p&gt;&lt;img height="403" alt="" src="http://codeforeternity.com/blogs/technology/Themeable/02.JPG" width="579" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Now I no longer have to explicitly set the Display and ValidationExpression property of each and every RegularExpressionValidator control which validates an email address. Instead, I can simply set their SkinID as Email ;-). See below screen shot on how to use the Email skin in&amp;nbsp;aspx /&amp;nbsp;ascx files:&lt;/p&gt;
&lt;p&gt;&lt;img height="193" alt="" src="http://codeforeternity.com/blogs/technology/Themeable/03.JPG" width="668" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;This also makes it super easy for me to change the ValidationExpression property for a particular type of validation if need be. For example, if a new business rule disallows all hotmail email addresses, I can simply change the ValidationExpression property in the Email skin instead of changing it in multiple aspx / ascx files where I am validating email addresses ;-)&lt;/p&gt;
&lt;p&gt;Also note that the Default skin makes sure that every instance of the NewRegularExpressionValidator control in my ASP.NET project (which does not specify a particular SkinID) will have its Display property automatically set to Dynamic ;-)&lt;/p&gt;
&lt;p&gt;Note: You can use the same above trick with many other controls to make their Non Themeable properties Themeable. One good example (which I have been using since years) is to make the MaxLength property of a TextBox control Themeable. I find it quite useful to set the MaxLength property of a TextBox through a skin for common inputs which are required in multiple pages of a website (like Email, Password, etc).&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=114" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Code+Better/default.aspx">Code Better</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Tips+And+Tricks/default.aspx">Tips And Tricks</category></item><item><title>Cool TrimText Extension Method for TextBox</title><link>http://codeforeternity.com/blogs/technology/archive/2008/03/03/cool-trimtext-extension-method-for-textbox.aspx</link><pubDate>Mon, 03 Mar 2008 08:01:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:87</guid><dc:creator>raj</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;Extension Methods are one of the most powerful features of .NET 3.5 and they can add a lot of flexibility to your code and help in cutting down your code size significantly if used wisely. I have been using a lot of Extension Methods in my code lately. One of them is the TrimText Extension Method for TextBox. While taking user input on any webform or winform (for example when users enter their First Name / Last Name), often, users accidentally add an extra space in the end. If you are a good coder, you would handle such common user mistakes by making sure to trim these input values before adding them to the database. Prior to .NET 3.5, we would have to write the below code to trim the TextBox text:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;TextBox1.Text = TextBox1.Text.Trim();&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;TextBox1.Text = TextBox1.Text.Trim()&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Now lets see how Extension Methods can help cut down the size of our above code. We create a simple TrimText Extension Method in a static class (C#) or module (VB) like below:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;public static class ExtensionMethods&lt;br /&gt;{&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void TrimText(this TextBox t)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;t.Text = t.Text.Trim();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;}&lt;/font&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;Imports System.Runtime.CompilerServices&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;Public Module ExtensionMethods&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Extension()&amp;gt; _&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Sub TrimText(ByVal t As TextBox)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.Text = t.Text.Trim()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;End Module&lt;/font&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Once we have our TrimText Extension Method in place, we can achieve the same task by simply calling the TrimText Extension Method from any TextBox object:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;TextBox1.TrimText();&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;TextBox1.TrimText()&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Notice how we have cut down the code and the code looks so much neater now ;-) I will post a few more examples of useful Extension Methods which I am using over the coming weeks. For those new to .NET 3.5, click&amp;nbsp;&lt;a class="" href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx" target="_blank"&gt;here&lt;/a&gt; to know more about Extension Methods.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=87" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/WinForms/default.aspx">WinForms</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Extension+Methods/default.aspx">Extension Methods</category></item><item><title>Creating Self Signed SSL Certificates on IIS 6.0 and Windows Server 2003</title><link>http://codeforeternity.com/blogs/technology/archive/2008/02/15/creating-self-signed-ssl-certificates-on-iis-6-0-and-windows-server-2003.aspx</link><pubDate>Fri, 15 Feb 2008 08:48:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:42</guid><dc:creator>raj</dc:creator><slash:comments>16</slash:comments><description>&lt;p&gt;If you need to deploy and test your code in SSL environment on IIS 6.0 and Windows Server 2003, but you do not have a valid SSL certificate on your development / test server issued by a trusted third party Certificate Authority (since it costs money), you can easily do so by creating a self signed test certificate using a tool called SelfSSL which comes with&amp;nbsp;IIS 6.0 Resource Kit Tools. SelfSSL is a console line application which is free to use :-). You can download&amp;nbsp;IIS 6.0 Resource Kit Tools from &lt;a class="" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=56fc92ee-a71a-4c73-b628-ade629c89499&amp;amp;displaylang=en" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;After downloading&amp;nbsp;and executing this kit,&amp;nbsp;make sure you either choose Complete installation option or if you choose&amp;nbsp;Custom installation option, make sure you have selected the SelfSSL feature. See below step by step screen shots for the Custom installation option.&lt;/p&gt;
&lt;p&gt;Step 1: Click Next.&lt;/p&gt;
&lt;p&gt;&lt;img height="379" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/01.JPG" width="504" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Step 2: Choose I Agree and click Next.&lt;/p&gt;
&lt;p&gt;&lt;img height="379" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/02.JPG" width="504" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Step 3: Enter appropriate details and click Next.&lt;/p&gt;
&lt;p&gt;&lt;img height="379" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/03.JPG" width="504" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Step 4: Choose Custom installation option if you just want to install SelfSSL&amp;nbsp;else choose Complete installation option to install all features and click Next.&lt;/p&gt;
&lt;p&gt;&lt;img height="379" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/04.JPG" width="504" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Step 5:&amp;nbsp;Enter the installation location and click Next.&lt;/p&gt;
&lt;p&gt;&lt;img height="379" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/05.JPG" width="504" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Step 6: Select&amp;nbsp;SelfSSL and click Next.&lt;/p&gt;
&lt;p&gt;&lt;img height="379" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/06.JPG" width="504" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Step 7: Review settings and click Next.&lt;/p&gt;
&lt;p&gt;&lt;img height="1" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/07.JPG" width="1" border="0" /&gt;&lt;img height="379" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/07.JPG" width="504" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Step 8: Click Finish.&lt;/p&gt;
&lt;p&gt;&lt;img height="379" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/08.JPG" width="504" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;Once you have successfully installed, click on Start &amp;gt; All Programs &amp;gt; IIS Resources &amp;gt; SelfSSL &amp;gt; SelfSSL to run the SelfSSL utility. On doing so, you should see the command prompt along with help instructions (see below screen shot).&lt;/p&gt;
&lt;p&gt;&lt;img height="331" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/09.JPG" width="668" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;If you simply type selfssl.exe and press enter, it would use the default settings to install the SSL certificate which are equivalent to:&lt;/p&gt;
&lt;p&gt;/N:CN=&amp;lt;YOUR COMPUTER NAME&amp;gt; (common name of the certificate)&lt;br /&gt;/K:1024 (key length of the certificate)&lt;br /&gt;/V:7 (validity of the certificate in days)&lt;br /&gt;/S:1 (ID of the site to which the certificate needs to be installed)&lt;br /&gt;/P:443 (SSL port)&lt;/p&gt;
&lt;p&gt;Type selfssl.exe and press enter, then type&amp;nbsp;y and press enter again to confirm the installation (see screen shot below).&lt;/p&gt;
&lt;p&gt;&lt;img height="331" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/10.JPG" width="668" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;The most important option here is the site id parameter and SelfSSL uses the site id 1 by default which maps to &amp;quot;Default Web Site&amp;quot;. &lt;/p&gt;
&lt;p&gt;To find the site id for any website in IIS 6.0 you can simply execute iisweb.vbs /query &amp;quot;&amp;lt;NAME OF THE WEBSITE&amp;gt;&amp;quot; from command prompt (see below screen shot).&lt;/p&gt;
&lt;p&gt;&lt;img height="331" alt="" src="http://codeforeternity.com/blogs/technology/SelfSSL/11.JPG" width="668" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;In the above screen shot, you can clearly see that I executed iisweb.vbs /query &amp;quot;Default Web Site&amp;quot; to find the site id for &amp;quot;Default Web Site&amp;quot; which is &amp;quot;W3SVC/1&amp;quot;, however we are only interested in the text which follows &amp;quot;W3SVC/&amp;quot; which is &amp;quot;1&amp;quot;.&lt;/p&gt;
&lt;p&gt;Suppose you had another website by the name &amp;quot;RajTest&amp;quot; and you wanted to install a test SSL certificate having common name &amp;quot;RajTestCertificate&amp;quot; valid for 10 days to &amp;quot;RajTest&amp;quot; on port 444, you would first find the site id for &amp;quot;RajTest&amp;quot; by executing the following command at command prompt: &lt;strong&gt;iisweb.vbs /query &amp;quot;RajTest&amp;quot;&lt;/strong&gt;. Once you know the site id for &amp;quot;RajTest&amp;quot; (lets assume it was &amp;quot;1234567&amp;quot;) you would execute the following command at the SelfSSL command prompt: &lt;strong&gt;selfssl.exe /N:CN=RajTestCertificate /V:10 / S:1234567 /P:444&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=42" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/IIS/default.aspx">IIS</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Windows+Server/default.aspx">Windows Server</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Security/default.aspx">Security</category></item><item><title>Building a Web 2.0 Portal with ASP.NET 3.5</title><link>http://codeforeternity.com/blogs/technology/archive/2008/02/13/building-a-web-2-0-portal-with-asp-net-3-5.aspx</link><pubDate>Wed, 13 Feb 2008 15:40:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:39</guid><dc:creator>raj</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;I recently purchased this great new book named &lt;strong&gt;Building a Web 2.0 Portal with ASP.NET 3.5&lt;/strong&gt; by Omar Al Zabir and I would highly recommend this book to any&amp;nbsp;architect or developer who is interested in learning how to design and develop world class highly scalable ASP.NET web portals using the latest and coolest Microsoft.NET technologies like ASP.NET 3.5, LINQ, Windows Workflow Foundation (WWF), ASP.NET AJAX, etc.&lt;/p&gt;
&lt;p&gt;To me, this is one of the best books ever written on ASP.NET because unlike most other books, this book deals with real world problems which architects and developers face with heavy duty ASP.NET websites and provides really simple tried and tested solutions to solve the same.&amp;nbsp;This book is less&amp;nbsp;of theory and more of real world code and ASP.NET tips and tricks. Great job by Omar.&lt;/p&gt;
&lt;p&gt;Click&amp;nbsp;&lt;a class="" href="http://www.oreilly.com/catalog/9780596510503" target="_blank"&gt;here&lt;/a&gt; for more info on this book or even better, buy this book right away from&amp;nbsp;Amazon&amp;nbsp;&lt;a class="" href="http://www.amazon.com/Building-Web-2-0-Portal-ASP-NET/dp/0596510500" target="_blank"&gt;here&lt;/a&gt;&amp;nbsp;:-)&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=39" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/.NET+3.5/default.aspx">.NET 3.5</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/AJAX/default.aspx">AJAX</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/WWF/default.aspx">WWF</category></item><item><title>Using sp_depends to find all Stored Procedures which reference / use a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/20/using-sp-depends-to-find-all-stored-procedures-which-reference-use-a-table.aspx</link><pubDate>Sun, 20 Jan 2008 14:10:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:26</guid><dc:creator>raj</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;We can use the &lt;strong&gt;sp_depends&lt;/strong&gt; system stored procedure to find all stored procedures which reference / use a particular table. The following T-SQL command will return a list of all database objects which are referencing / using the Products table:&lt;/p&gt;&lt;font color="#0000ff" size="2"&gt;
&lt;p&gt;EXEC&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#800000" size="2"&gt;sp_depends&lt;/font&gt;&lt;font size="2"&gt; Products&lt;/p&gt;&lt;/font&gt;
&lt;p&gt;Since the result set(s) returned by executing the above T-SQL command consists of all database objects (and not only stored procedures) which depend upon the Products table, we need to only look at the records which have type as &amp;quot;stored procedure&amp;quot; to know of all stored procedures which reference / use the Product table. Similarly, we need to only look at the records which have type as &amp;quot;view&amp;quot; to know of all views which reference / use the Products table.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=26" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Using sp_fkeys to find all Foreign Keys pointing to a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/20/using-sp-fkeys-to-find-all-foreign-keys-pointing-to-a-table.aspx</link><pubDate>Sun, 20 Jan 2008 12:50:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:25</guid><dc:creator>raj</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;We can use the &lt;strong&gt;sp_fkeys&lt;/strong&gt; system stored procedure to find all foreign keys which point to a particular table. The following T-SQL command will return a list of all tables which are referencing the primary key of the Products table:&lt;/p&gt;&lt;font color="#0000ff" size="2"&gt;
&lt;p&gt;EXEC&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#800000" size="2"&gt;sp_fkeys&lt;/font&gt;&lt;font size="2"&gt; Products&lt;/p&gt;&lt;/font&gt;
&lt;p&gt;The &lt;strong&gt;sp_fkeys&lt;/strong&gt; system stored procedure comes very handy when dealing with large&amp;nbsp;databases which have hundreds of tables.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=25" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx</link><pubDate>Thu, 10 Jan 2008 13:53:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:21</guid><dc:creator>raj</dc:creator><slash:comments>50</slash:comments><description>&lt;p&gt;One of my favorite technical interview question on SQL Server is &amp;quot;What query would you write to find the second highest column value in a table?&amp;quot; The question seems easy, however most developers fail to answer to this. If we had a table named Employee which had a column named Salary and we had to find the second highest Salary in the Employee table, the query for the same would be:&lt;/p&gt;
&lt;p&gt;SELECT TOP 1 Salary FROM (SELECT TOP 2 Salary FROM Employee ORDER BY Salary DESC) AS E ORDER BY Salary ASC&lt;/p&gt;
&lt;p&gt;If we ran the above query against the Employee table which had the following 5 rows:&lt;/p&gt;
&lt;p&gt;1000&lt;br /&gt;2000&lt;br /&gt;3000&lt;br /&gt;4000&lt;br /&gt;5000&lt;/p&gt;
&lt;p&gt;The subquery or the inner query would return the top 2 rows in descending Salary order which would be:&lt;/p&gt;
&lt;p&gt;5000&lt;br /&gt;4000&lt;/p&gt;
&lt;p&gt;The outer query would then select the top 1 row from the subquery results in ascending Salary order which would be:&lt;/p&gt;
&lt;p&gt;4000&lt;/p&gt;
&lt;p&gt;Note that if we had to get the fourth highest Salary, we could do so by simply changing the subquery from TOP 2 to TOP 4&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;br /&gt;&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=21" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Finding Database Size using sp_spaceused Stored Procedure</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/02/finding-database-size-using-sp-spaceused-stored-procedure.aspx</link><pubDate>Wed, 02 Jan 2008 14:33:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:16</guid><dc:creator>raj</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;We can use the &lt;strong&gt;sp_spaceused&lt;/strong&gt; stored procedure to find out exactly how much disk space is currently being used by a database. If we simply execute this stored procedure without passing any parameters, it returns the following 2 result sets:&lt;/p&gt;
&lt;p&gt;Result Set 1:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;database_name:&lt;/strong&gt; Name of the current database.&lt;br /&gt;&lt;strong&gt;database_size:&lt;/strong&gt; Size of the current database in megabytes. database_size includes both data and log files.&lt;br /&gt;&lt;strong&gt;unallocated space:&lt;/strong&gt; Space in the database that has not been reserved for database objects.&lt;/p&gt;
&lt;p&gt;Result Set 2:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;reserved:&lt;/strong&gt; Total amount of space allocated by objects in the database.&lt;br /&gt;&lt;strong&gt;data:&lt;/strong&gt; Total amount of space used by data.&lt;br /&gt;&lt;strong&gt;index_size:&lt;/strong&gt; Total amount of space used by indexes.&lt;br /&gt;&lt;strong&gt;unused:&lt;/strong&gt; Total amount of space reserved for objects in the database, but not yet used.&lt;/p&gt;
&lt;p&gt;code for etetnity !!! is hosted with GoDaddy Shared Hosting Plan which only allows upto 200 MB of database disk space which is really very limited and Community Server can reach this limit in no time. Therefore I have to keep monitoring the database disk space usage regularly. However, since GoDaddy does not provide Remote Desktop or Enterprise Manager / Management Studio access to the database server, the only way to know this is by executing the &lt;strong&gt;sp_spaceused&lt;/strong&gt; stored procedure through GoDaddy&amp;#39;s web based Query Analyzer or through a custom built ASP.NET web page which executes this stored procedure using ADO.NET. You can use the same technique if you face&amp;nbsp;similar restrictions by&amp;nbsp;your hosting provider.&lt;/p&gt;
&lt;p&gt;For more info on the &lt;strong&gt;sp_spaceused&lt;/strong&gt; stored procedure, click &lt;a class="" href="http://msdn2.microsoft.com/en-us/library/ms188776.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=16" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/GoDaddy/default.aspx">GoDaddy</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Community+Server/default.aspx">Community Server</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Installing Community Server 2007 with GoDaddy Shared Hosting Plan</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/02/installing-community-server-2007-with-godaddy-shared-hosting-plan.aspx</link><pubDate>Wed, 02 Jan 2008 12:12:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:15</guid><dc:creator>raj</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;Planning to launch a new Community Server 2007 website with GoDaddy Shared Hosting Plan? Or facing problems setting up the same? If you wish to save yourself all the hassles, download and read this excellent CS 2007 Installation Guide for GoDaddy Shared Hosting Plan by Dave Stokes &lt;a class="" href="http://davestokes.net/files/folders/2007_installation_guides/entry273.aspx" target="_blank"&gt;here&lt;/a&gt;. I had a hard time setting up my CS 2007 website @ code for eternity !!!, but Dave&amp;#39;s Installation Guide came to the rescue. Thanks for the excellent work Dave.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=15" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/GoDaddy/default.aspx">GoDaddy</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Community+Server/default.aspx">Community Server</category></item><item><title>Developing Facebook Applications using Facebook Developer Toolkit for .NET</title><link>http://codeforeternity.com/blogs/technology/archive/2007/12/26/developing-facebook-applications-using-facebook-developer-toolkit-for-net.aspx</link><pubDate>Wed, 26 Dec 2007 13:33:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:13</guid><dc:creator>raj</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;You can now develop windows based or web based Facebook Applications using your favorite .NET language, be it C# or VB.NET. All you need is a Facebook account, Visual Studio Express and the Facebook Developer Toolkit for .NET. Information about the Facebook Developer Toolkit can be found &lt;a class="" href="http://msdn2.microsoft.com/en-us/express/bb510381.aspx" target="_blank"&gt;here&lt;/a&gt;. The tooIkit makes it incredibly easy to develop Facebook Applications based on the Facebook Platform. The toolkit also includes WPF and LINQ examples. I would strongly recommend you to check out the following links before going ahead and downloading the toolkit:&lt;/p&gt;
&lt;p&gt;1) &lt;a class="" href="http://soapbox.msn.com/video.aspx?vid=ab8ba22e-3d53-4eb3-853d-311e06f5ed99" target="_blank"&gt;Video: Facebook Developer Toolkit Walkthrough (Windows Forms)&lt;/a&gt;&lt;br /&gt;2) &lt;a class="" href="http://soapbox.msn.com/video.aspx?vid=5a2b8ffc-31a1-4041-97d0-44461f1fd764" target="_blank"&gt;Video: Facebook Developer Toolkit Walkthrough (ASP.NET)&lt;/a&gt;&lt;br /&gt;3) &lt;a class="" href="http://msdn2.microsoft.com/en-us/express/bb510383.aspx" target="_blank"&gt;Facebook Quickstarts&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Detailed information on the Facebook Platform can be found &lt;a class="" href="http://developers.facebook.com/" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=13" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/WPF/default.aspx">WPF</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/WinForms/default.aspx">WinForms</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Facebook/default.aspx">Facebook</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>Handling ASP.NET Session Variables Efficiently</title><link>http://codeforeternity.com/blogs/technology/archive/2007/12/19/handling-asp-net-session-variables-efficiently.aspx</link><pubDate>Wed, 19 Dec 2007 16:32:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:12</guid><dc:creator>raj</dc:creator><slash:comments>51</slash:comments><description>&lt;p&gt;One of the most common mistakes ASP.NET developers make is while accessing session variables. Have a look at the code below:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Writing to session variable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Session[&amp;quot;UserCountry&amp;quot;] = ddlUserCountry.SelectedValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Reading from session variable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string userCountry = Session[&amp;quot;UserCountry&amp;quot;].ToString();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Writing to session variable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Session(&amp;quot;UserCountry&amp;quot;) = ddlUserCountry.SelectedValue&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Reading from session variable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim userCountry As String = Session(&amp;quot;UserCountry&amp;quot;).ToString()&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I have seen such code across multiple aspx and ascx code behind pages and it is a disaster waiting to happen and has many drawbacks as follows:&lt;/p&gt;
&lt;p&gt;1) If a session variable is read before it has been assigned or if the current session times out, it will result in NullReferenceException as no null checking happens before reading from session variable.&lt;/p&gt;
&lt;p&gt;2) A simple spelling mistake of the key name used to identify the session variable (&amp;quot;UserCountry&amp;quot; in above example) will result in NullReferenceException (if no null checking is being done) or incorrect data (if null checking is being done).&lt;/p&gt;
&lt;p&gt;3) In large ASP.NET projects where many developers are coding at the same time, very often two developers end up using the same key names for two different session variables meant for two different purposes. I have seen developers use the wildest key names for session variables like &amp;quot;Name&amp;quot; which is so common, it can be used by one developer to save the user&amp;#39;s first name, and by another developer to save the user&amp;#39;s user name. When such a situation arises, one of the session variable&amp;#39;s key name would have to be changed across multiple aspx and ascx code behind pages, making sure that the other session variable sharing the same key name but meant for a different purpose is not changed by accident. This will result in complete chaos, and debugging and testing for such changes becomes a nightmare.&lt;/p&gt;
&lt;p&gt;4) There is no easy way to track the usage of session variables, their key names, their data types, the approximate memory being used per session, etc.&lt;/p&gt;
&lt;p&gt;However, with just a few extra lines of code and effort, we can get rid of above problems. To address above problems, we can create a single static / shared class (lets call it SessionHandler) which exposes all session variables through strongly typed static / shared properties. No aspx and ascx code behind pages should ever access session variables directly but instead access the strogly typed static / shared properties of the SessionHandler class. All the code to access session variables should exist ONLY in the SessionHandler class. Also, the SessionHandler class should use string variables to save key names of different session variables it exposes in order to avoid spelling mistakes. Have a look at the code below:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Static / shared class for handling session variables&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static class SessionHandler&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Declare a string variable to hold the key name of the session variable&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // and use this string variable instead of typing the key name&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // in order to avoid spelling mistakes&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private static string _userCountryKey = &amp;quot;UserCountry&amp;quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Declare a static / shared strongly typed property to expose session variable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static string UserCountry&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Check for null first&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (HttpContext.Current.Session[SessionHandler._userCountryKey] == null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Return an empty string if session variable is null&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return string.Empty;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return HttpContext.Current.Session[SessionHandler._userCountryKey].ToString();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpContext.Current.Session[SessionHandler._userCountryKey] = value;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Static / shared class for handling session variables&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Class SessionHandler&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Declare a string variable to hold the key name of the session variable&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; and use this string variable instead of typing the key name&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; in order to avoid spelling mistakes&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private Shared _userCountryKey As String = &amp;quot;UserCountry&amp;quot;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Declare a static / shared strongly typed property to expose session variable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Shared Property UserCountry() As String&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Get&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Check for null first&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If (HttpContext.Current.Session(SessionHandler._userCountryKey) Is Nothing) Then&lt;br /&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Return an empty string if session variable is null&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Return String.Empty&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Return HttpContext.Current.Session(SessionHandler._userCountryKey).ToString()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Get&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Set(ByVal value As String)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpContext.Current.Session(SessionHandler._userCountryKey) = value&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Set&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Property&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Class&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Once we have the SessionHandler class in place, all aspx and ascx code behind pages can access session variables through this class like below. Notice that we dont access session variables directly anymore and let the SessionHandler class do all the required work to access session variables.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C#:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Writing to session variable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SessionHandler.UserCountry = ddlCountry.SelectedValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Reading from session variable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string userCountry = SessionHandler.UserCountry;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;VB:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Writing to session variable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SessionHandler.UserCountry = ddlUserCountry.SelectedValue&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;&lt;font color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39; Reading from session variable&lt;br /&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim userCountry As String = SessionHandler.UserCountry&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Now since all the code to access session variables exists in one single class, it becomes really easy to track their usage, their key names, their data types, the approximate memory being used per session, etc. Also notice how easy it would be to change the key name of a session variable if required. All you would have to do is change the value of one private static / shared variable (&amp;quot;_userCountryKey&amp;quot; in above example). Also, we can be 100% sure that we are not using duplicate key names for multiple session variables by simply checking the value of all existing private static / shared string variables being used to save key names by the SessionHandler class.&lt;/p&gt;
&lt;p&gt;Note: We can use the same above architecture (with slight changes to code) to access data from ASP.NET Cache as well as Application Settings. I will blog about it in a future post.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=12" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Code+Better/default.aspx">Code Better</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Architecture/default.aspx">Architecture</category></item><item><title>Difference between Silverlight Version 1.0 and Silverlight Version 1.1</title><link>http://codeforeternity.com/blogs/technology/archive/2007/12/18/difference-between-silverlight-1-0-and-silverlight-1-1.aspx</link><pubDate>Tue, 18 Dec 2007 13:03:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:11</guid><dc:creator>raj</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;A lot of my friends who want to get started with Silverlight development keep asking me what&amp;#39;s the difference between Silverlight 1.0 and Silverlight 1.1? Which version should they use? Which version supports what?&lt;/p&gt;
&lt;p&gt;The primary difference between the two versions is: you can only use JavaScript to create Silverlight applications in version 1.0, however you can use your favorite language, be it C#, VB, IronRuby or IronPython to create Silverlight applications in version 1.1.&lt;/p&gt;
&lt;p&gt;For detailed information on the features of Silverlight versions 1.0 and 1.1, click &lt;a class="" href="http://silverlight.net/GetStarted/overview.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;br /&gt;Raj&lt;/p&gt;
&lt;p&gt;~~~ CODING FOR ETERNITY !!! ~~~&lt;/p&gt;&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=11" width="1" height="1"&gt;</description><category domain="http://codeforeternity.com/blogs/technology/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://codeforeternity.com/blogs/technology/archive/tags/Silverlight/default.aspx">Silverlight</category></item></channel></rss>