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

<channel>
	<title>Coding Mode</title>
	<atom:link href="http://codingmode.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://codingmode.com</link>
	<description>blog by Harish Mathanan</description>
	<lastBuildDate>Thu, 17 Jan 2013 01:19:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='codingmode.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/05ebde8aebcc8e0baed528679010bbbf?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Coding Mode</title>
		<link>http://codingmode.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://codingmode.com/osd.xml" title="Coding Mode" />
	<atom:link rel='hub' href='http://codingmode.com/?pushpress=hub'/>
		<item>
		<title>Search Service Administration Component Issue</title>
		<link>http://codingmode.com/2013/01/17/search-service-administration-component-issue/</link>
		<comments>http://codingmode.com/2013/01/17/search-service-administration-component-issue/#comments</comments>
		<pubDate>Thu, 17 Jan 2013 01:19:01 +0000</pubDate>
		<dc:creator>Harish Mathanan</dc:creator>
				<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[Configuration]]></category>

		<guid isPermaLink="false">https://harishmathanan.wordpress.com/?p=500</guid>
		<description><![CDATA[This is a SharePoint 2010 problem a client was facing. Essentially they had installed SharePoint and were in the process of manually configuring their services. They had setup the Search Service Application, but when the went into the Search Administration, the got the error below. The search service is not able to connect to the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=500&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is a SharePoint 2010 problem a client was facing. Essentially they had installed SharePoint and were in the process of manually configuring their services. They had setup the Search Service Application, but when the went into the Search Administration, the got the error below.</p>
<p><em>The search service is not able to connect to the machine that hosts the administration component. Verify that the administration component &#8216;c200182e-0520-4f00-bd2c-50321fc377e8&#8242; in search application &#8216;Search Service Application&#8217; is in a good state and try again.</em>
<p>There are various posts out on the Internet describing a resolution for this. But prior to attempting any of those, please check to see if your SharePoint Foundation Search is up an running. If it is not, then do start it and try again.
<p>Always remember to lookout for the obvious prior to digging deeper, and it will save you time and grief.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/harishmathanan.wordpress.com/500/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/harishmathanan.wordpress.com/500/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=500&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingmode.com/2013/01/17/search-service-administration-component-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dfc3cebd7faac25d5f7ca66dfc04ea9d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">harishmathanan</media:title>
		</media:content>
	</item>
		<item>
		<title>Configuration Wizard KeyNotFoundException Error</title>
		<link>http://codingmode.com/2013/01/17/configuration-wizard-keynotfoundexception-error/</link>
		<comments>http://codingmode.com/2013/01/17/configuration-wizard-keynotfoundexception-error/#comments</comments>
		<pubDate>Thu, 17 Jan 2013 01:12:26 +0000</pubDate>
		<dc:creator>Harish Mathanan</dc:creator>
				<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[Configuration]]></category>

		<guid isPermaLink="false">https://harishmathanan.wordpress.com/?p=499</guid>
		<description><![CDATA[This is a SharePoint error that occurred on a SharePoint 2010 environment post-install. Essentially, the user tried running the SharePoint Configuration Wizard to get things going but got the error message below. In order to resolve this, you will need to give the service account READ access to all Authenticated Users. Basically, on Active Directory [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=499&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is a SharePoint error that occurred on a SharePoint 2010 environment post-install. Essentially, the user tried running the SharePoint Configuration Wizard to get things going but got the error message below.</p>
<p><a href="http://harishmathanan.files.wordpress.com/2013/01/err001.png"><img style="border-bottom:0;border-left:0;display:block;float:none;margin-left:auto;border-top:0;margin-right:auto;border-right:0;" title="err001" border="0" alt="err001" src="http://harishmathanan.files.wordpress.com/2013/01/err001_thumb.png?w=563&#038;h=347" width="563" height="347"></a>
<p>In order to resolve this, you will need to give the service account READ access to all Authenticated Users.
<p>Basically, on Active Directory Users and Computers, ensure that Advance Features is selected from the View menu. Then, open the property window of the desired account, select the Security tab and find Authenticated Users. Finally, select Authenticated Users and give them READ access.
<p><a href="http://harishmathanan.files.wordpress.com/2013/01/err002.png"><img style="border-bottom:0;border-left:0;display:block;float:none;margin-left:auto;border-top:0;margin-right:auto;border-right:0;" title="err002" border="0" alt="err002" src="http://harishmathanan.files.wordpress.com/2013/01/err002_thumb.png?w=419&#038;h=549" width="419" height="549"></a>
<p>Microsoft has release a knowledge base article for this, see <a href="http://support.microsoft.com/kb/2463865">KB2463865</a> for full information.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/harishmathanan.wordpress.com/499/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/harishmathanan.wordpress.com/499/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=499&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingmode.com/2013/01/17/configuration-wizard-keynotfoundexception-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dfc3cebd7faac25d5f7ca66dfc04ea9d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">harishmathanan</media:title>
		</media:content>

		<media:content url="http://harishmathanan.files.wordpress.com/2013/01/err001_thumb.png" medium="image">
			<media:title type="html">err001</media:title>
		</media:content>

		<media:content url="http://harishmathanan.files.wordpress.com/2013/01/err002_thumb.png" medium="image">
			<media:title type="html">err002</media:title>
		</media:content>
	</item>
		<item>
		<title>NUnit System.IO.FileNotFoundException Error</title>
		<link>http://codingmode.com/2012/08/20/nunit-system-io-filenotfoundexception-error/</link>
		<comments>http://codingmode.com/2012/08/20/nunit-system-io-filenotfoundexception-error/#comments</comments>
		<pubDate>Mon, 20 Aug 2012 08:03:53 +0000</pubDate>
		<dc:creator>Harish Mathanan</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[Error]]></category>
		<category><![CDATA[NUnit]]></category>

		<guid isPermaLink="false">https://harishmathanan.wordpress.com/?p=485</guid>
		<description><![CDATA[I keep getting this error when I try to open my assembly System.IO.FileNotFoundException Could not load file or assembly or one of its dependencies. The system cannot find the file specified. There was nothing wrong with the assembly nor the location of it’s files. I can actually build and run the solution with no errors [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=485&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I keep getting this error when I try to open my assembly <strong>System.IO.FileNotFoundException Could not load file or assembly or one of its dependencies. The system cannot find the file specified</strong>. </p>
<p>There was nothing wrong with the assembly nor the location of it’s files. I can actually build and run the solution with no errors at all. </p>
<p>I eventually discovered that that the .NET runtime set on the NUnit config file by default is for .NET version 2.0 and my solution was built on .NET 4.0. </p>
<p>In order to get this going, navigate to your NUnit bin directory location, and open up the <strong>nunit.exe.config</strong> file with a text editor. Look for element as shown below:</p>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;margin:20px 0 10px;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;margin:0;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> &lt;startup useLegacyV2RuntimeActivationPolicy=<span style="color:#006080;">"true"</span>&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;margin:0;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span>   &lt;!-- Comment <span style="color:#0000ff;">out</span> the next line to force use of .NET 4.0 --&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;margin:0;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>   &lt;supportedRuntime version=<span style="color:#006080;">"v2.0.50727"</span> /&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;margin:0;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>   &lt;supportedRuntime version=<span style="color:#006080;">"v4.0.30319"</span> /&gt;</pre>
<p><!--CRLF-->
<pre style="text-align:left;line-height:12pt;background-color:white;margin:0;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span> &lt;/startup&gt;</pre>
<p><!--CRLF--></div>
</div>
<p>As the comment line indicates, just comment out the version of .NET runtime that you’re not going to be using. And now we can begin Unit Testing <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/harishmathanan.wordpress.com/485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/harishmathanan.wordpress.com/485/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=485&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingmode.com/2012/08/20/nunit-system-io-filenotfoundexception-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dfc3cebd7faac25d5f7ca66dfc04ea9d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">harishmathanan</media:title>
		</media:content>
	</item>
		<item>
		<title>Data Binding Inline Evaluation</title>
		<link>http://codingmode.com/2012/08/17/data-binding-inline-evaluation/</link>
		<comments>http://codingmode.com/2012/08/17/data-binding-inline-evaluation/#comments</comments>
		<pubDate>Fri, 17 Aug 2012 03:52:03 +0000</pubDate>
		<dc:creator>Harish Mathanan</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">https://harishmathanan.wordpress.com/?p=483</guid>
		<description><![CDATA[There are time in where you may like to display a certain value inline, depending on the Eval() value that is data bound. At times like this, the ternary operator helps a great deal. For example, I have a datetime object (“DatePaid”) that is being retrieved from the database using Linq to Sql. And every [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=483&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>There are time in where you may like to display a certain value inline, depending on the Eval() value that is data bound. At times like this, the <a href="http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx">ternary operator</a> helps a great deal. </p>
<p>For example, I have a datetime object (“DatePaid”) that is being retrieved from the database using Linq to Sql. And every time the value is null it display the date on the user interface as such “1/01/0001”. I would really like a blank column displayed instead. I achieved this by using the ternary operator inline as below:</p>
<div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;margin:20px 0 10px;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;margin:0;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> &lt;%# Eval(<span style="color:#006080;">"DatePaid"</span>) == <span style="color:#0000ff;">null</span> ? <span style="color:#006080;">"&amp;nbsp;"</span> : Eval(<span style="color:#006080;">"DatePaid"</span>)</pre>
<p><!--CRLF--></div>
</div>
<p>The above translates as such, if “DatePaid” is null then insert blank space “&amp;nbsp;”, else show “DatePaid”</p>
<p>What if you had multiple conditions? Such as If-Else If-Else If-Else. In circumstances like this, it’s probably best handle the values server side, using a data bound event for example.</p>
<p>Hope this helps.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/harishmathanan.wordpress.com/483/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/harishmathanan.wordpress.com/483/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=483&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingmode.com/2012/08/17/data-binding-inline-evaluation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dfc3cebd7faac25d5f7ca66dfc04ea9d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">harishmathanan</media:title>
		</media:content>
	</item>
		<item>
		<title>Error Entering break mode failed</title>
		<link>http://codingmode.com/2012/08/13/error-entering-break-mode-failed/</link>
		<comments>http://codingmode.com/2012/08/13/error-entering-break-mode-failed/#comments</comments>
		<pubDate>Mon, 13 Aug 2012 07:49:56 +0000</pubDate>
		<dc:creator>Harish Mathanan</dc:creator>
				<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Error]]></category>

		<guid isPermaLink="false">https://harishmathanan.wordpress.com/?p=481</guid>
		<description><![CDATA[I got the following error after copying over my solution from my development server into my laptop and tried debugging the solution. The full error is as below: Entering break mode failed for the following reason &#60;filename&#62; does not belong to the project being debugged. I tried re-building my solution and closing and opening up [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=481&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I got the following error after copying over my solution from my development server into my laptop and tried debugging the solution. The full error is as below:</p>
<p><em><font color="#ff0000">Entering break mode failed for the following reason &lt;filename&gt; does not belong to the project being debugged. </font></em></p>
<p>I tried re-building my solution and closing and opening up Visual Studio with no luck <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Eventually, I tried to clean my solution, but because I only had Visual Studio Express this option was not available to me. </p>
<p>The resolution? I manually deleted the <strong>bin and obj folders</strong> from my solution directory then opened the solution and rebuild it again. This worked like a charm, hope it helps you.</p>
<p><a href="http://harishmathanan.files.wordpress.com/2012/08/err.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="err" border="0" alt="err" src="http://harishmathanan.files.wordpress.com/2012/08/err_thumb.png?w=189&#038;h=182" width="189" height="182"></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/harishmathanan.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/harishmathanan.wordpress.com/481/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=481&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingmode.com/2012/08/13/error-entering-break-mode-failed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dfc3cebd7faac25d5f7ca66dfc04ea9d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">harishmathanan</media:title>
		</media:content>

		<media:content url="http://harishmathanan.files.wordpress.com/2012/08/err_thumb.png" medium="image">
			<media:title type="html">err</media:title>
		</media:content>
	</item>
		<item>
		<title>.NET Why Code to Interface?</title>
		<link>http://codingmode.com/2012/08/06/net-why-code-to-interface/</link>
		<comments>http://codingmode.com/2012/08/06/net-why-code-to-interface/#comments</comments>
		<pubDate>Mon, 06 Aug 2012 03:43:39 +0000</pubDate>
		<dc:creator>Harish Mathanan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">https://harishmathanan.wordpress.com/?p=474</guid>
		<description><![CDATA[You may have seen programming design patterns and practices encourage coding to interfaces. And you may have also seen examples as to how to achieve this. The question is, why? Why should a programmer make this his second nature. The following example would hopefully, shed some light into this, and hopefully help you get into [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=474&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>You may have seen programming design patterns and practices encourage coding to interfaces. And you may have also seen examples as to how to achieve this. The question is, why? Why should a programmer make this his second nature.</p>
<p>The following example would hopefully, shed some light into this, and hopefully help you get into the habit of good coding habits.</p>
<p>Let’s just say we take the “Game of Thrones”. In the book and TV series you would have noticed the numerous characters involved. I begin with creating two classes, representing two types of characters, one for a Knight and another for a Lord.</p>
<pre class="csharpcode">    <span class="kwrd">class</span> Knight
    {
        <span class="rem">// TODO </span>
    }

    <span class="kwrd">class</span> Lord
    {
        <span class="rem">// TODO</span>
    }</pre>
<p>As you can see, they do absolutely nothing at the moment. But one of the main functionality for my program is to retrieve character’s attributes. I have the method for this ready, the idea of this method is to retrieve the attributes from a particular character and return them as a string array.</p>
<pre class="csharpcode">        <span class="kwrd">public</span> <span class="kwrd">string</span>[] GetCharacterAttributes()
        {
            <span class="rem">// TODO</span>
        }</pre>
<p>I could instantiate my required object from the concrete classes before as below.</p>
<pre class="csharpcode">        <span class="kwrd">public</span> <span class="kwrd">string</span>[] GetCharacterAttributes()
        {
            var character = <span class="kwrd">new</span> Knight();
            <span class="rem">// Retrieve the attributes from Knight</span>
        }</pre>
<p>The above would be ok and have absolutely no problems. However, what if I needed to get attributes for a Lord character type as well? I could pass them in as parameters, as below.</p>
<pre class="csharpcode">        <span class="kwrd">public</span> <span class="kwrd">string</span>[] GetCharacterAttributes(Knight character)
        {
            <span class="rem">//  Retrieve Knight attributes.</span>
        }

        <span class="kwrd">public</span> <span class="kwrd">string</span>[] GetCharacterAttributes(Lord character)
        {
            <span class="rem">//  Retrieve Lord attributes.</span>
        }</pre>
<p>But this would make the code messy and complex. Alternatively, I could do something like the below instead.</p>
<pre class="csharpcode">        <span class="kwrd">public</span> <span class="kwrd">string</span>[] GetKnightAttributes()
        {
            var character = <span class="kwrd">new</span> Knight();
            <span class="rem">// Retrieve the attributes from Knight</span>
        }

        <span class="kwrd">public</span> <span class="kwrd">string</span>[] GetLordAttributes()
        {
            var character = <span class="kwrd">new</span> Lord();
            <span class="rem">// Retrieve the attributes from Lord            </span>
        }</pre>
<p>The above will work, the code will not be messy, and there would not be any complications. However, I am repeating the code, and I have to do it for each of my concrete class or character. And for those of you who have read the “A Song of Ice and Fire” books would know that this will get insanely hard as more and more characters are introduced.</p>
<p>This is where coding to an interface becomes valuable. I first create an interface called ICharacter, and then use that interface on my concrete character classes as below.</p>
<pre class="csharpcode">    <span class="rem">// Interface for Game of Thrones Characters</span>
    <span class="kwrd">public</span> <span class="kwrd">interface</span> ICharacter
    {
        <span class="kwrd">string</span> Name { get; set; }
    }

    <span class="rem">// Implementation of Interface on Knight concrete class.</span>
    <span class="kwrd">class</span> Knight : ICharacter
    {
        <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }
    }

    <span class="rem">// Implementation of Interface for Lord concrete class.</span>
    <span class="kwrd">class</span> Lord : ICharacter
    {
        <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }
    }</pre>
<p>Now let’s go back to our method of retrieving character attributes. I can now have just the one method, with no issues. All I’m doing now, is passing in the interface as the method parameter.</p>
<pre class="csharpcode">        <span class="kwrd">public</span> <span class="kwrd">string</span>[] GetCharacterAttributes(ICharacter character)
        {
            <span class="rem">// character passed in can be a Knight or a Lord</span>
            <span class="rem">// or it can be any other new characters that may get</span>
            <span class="rem">// created using the ICharacter interface.</span>
        }</pre>
<p>As you can see from the above, by coding to an interface, I do not have to deal with messy or complex code, and I do not need to repeat my code. Please note, there are ways in improving this through, Dependency Injection, but that will be a another post. I hope this helps, thank you.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/harishmathanan.wordpress.com/474/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/harishmathanan.wordpress.com/474/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=474&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingmode.com/2012/08/06/net-why-code-to-interface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dfc3cebd7faac25d5f7ca66dfc04ea9d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">harishmathanan</media:title>
		</media:content>
	</item>
		<item>
		<title>.getJSON Error</title>
		<link>http://codingmode.com/2012/06/08/getjson-error/</link>
		<comments>http://codingmode.com/2012/06/08/getjson-error/#comments</comments>
		<pubDate>Fri, 08 Jun 2012 01:47:57 +0000</pubDate>
		<dc:creator>Harish Mathanan</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Error]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">https://harishmathanan.wordpress.com/?p=471</guid>
		<description><![CDATA[If you get a &#8216;context&#8217; is null or not an object error, have a look at the JavaScript files that you are referencing on the page. It is known that the problem lies with the vsdoc file. Remove that reference from the page and only use your jQuery file and all will be fine again.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=471&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>If you get a <strong>&#8216;context&#8217; is null or not an object error</strong>, have a look at the JavaScript files that you are referencing on the page. It is known that the problem lies with the vsdoc file. Remove that reference from the page and only use your jQuery file and all will be fine again.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/harishmathanan.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/harishmathanan.wordpress.com/471/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=471&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingmode.com/2012/06/08/getjson-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dfc3cebd7faac25d5f7ca66dfc04ea9d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">harishmathanan</media:title>
		</media:content>
	</item>
		<item>
		<title>C# Simple Password Generator</title>
		<link>http://codingmode.com/2012/02/28/csharp-simple-password-generator/</link>
		<comments>http://codingmode.com/2012/02/28/csharp-simple-password-generator/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 04:17:47 +0000</pubDate>
		<dc:creator>Harish Mathanan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">https://harishmathanan.wordpress.com/?p=466</guid>
		<description><![CDATA[One of the simplest and easiest approach to generating random passwords, is to use the GetRandomFileName() static method from the Path class. Snippet below demonstrates this. 1: using System; 2: using System.IO; 3:&#160; 4: static void Main(string[] args) 5: { 6: // Calls the static method from the Path type 7: var randomFolderFile = Path.GetRandomFileName(); [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=466&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>One of the simplest and easiest approach to generating random passwords, is to use the <a href="http://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename.aspx">GetRandomFileName()</a> static method from the <a href="http://msdn.microsoft.com/en-us/library/system.io.path_methods.aspx">Path</a> class.</p>
<p>Snippet below demonstrates this.</p>
<div style="border-bottom:silver 1px solid;border-left:silver 1px solid;line-height:1pt!important;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">using</span> System;</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span> <span style="color:#0000ff;">using</span> System.IO;</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>&nbsp; </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span> {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>     <span style="color:#008000;">// Calls the static method from the Path type</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span>     var randomFolderFile = Path.GetRandomFileName();</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum8">   8:</span>&nbsp; </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum9">   9:</span>     <span style="color:#008000;">// Strips out dot characters</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum10">  10:</span>     randomFolderFile = randomFolderFile.Replace(<span style="color:#006080;">"."</span>, <span style="color:#006080;">""</span>); </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum11">  11:</span>     Console.WriteLine(randomFolderFile);</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum12">  12:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Happy Coding.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/harishmathanan.wordpress.com/466/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/harishmathanan.wordpress.com/466/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=466&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingmode.com/2012/02/28/csharp-simple-password-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dfc3cebd7faac25d5f7ca66dfc04ea9d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">harishmathanan</media:title>
		</media:content>
	</item>
		<item>
		<title>ASP.NET DataView Paging and Sorting.</title>
		<link>http://codingmode.com/2012/02/27/aspnet-dataview-paging-and-sorting/</link>
		<comments>http://codingmode.com/2012/02/27/aspnet-dataview-paging-and-sorting/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 04:26:36 +0000</pubDate>
		<dc:creator>Harish Mathanan</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[DataView]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">https://harishmathanan.wordpress.com/?p=459</guid>
		<description><![CDATA[This post demonstrates the use of the DataView class for simplifying Paging and Sorting operations on a GridView control. Prior to getting into the workings of it, do note, that I’m using data from Northwind Customers table. Essentially, all I am doing is retrieving the ContactName, CompanyName and Country from all Customers records and returning [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=459&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This post demonstrates the use of the <a href="http://msdn.microsoft.com/en-us/library/system.data.dataview(v=vs.90).aspx">DataView</a> class for simplifying Paging and Sorting operations on a <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview(v=vs.90).aspx">GridView</a> control.</p>
<p>Prior to getting into the workings of it, do note, that I’m using data from Northwind Customers table. Essentially, all I am doing is retrieving the ContactName, CompanyName and Country from all Customers records and returning them as an IEnumerable&lt;T&gt; object in my Data Access Layer.</p>
<p>To begin, I have a GridView control defined with 3 literal controls. Do note, the SortExpression defined on each TemplateField. Also note the OnSorting and OnPageIndexChanging events.</p>
<div style="border-bottom:silver 1px solid;border-left:silver 1px solid;line-height:1px!important;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:GridView</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span>               <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="customersGrid"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>               <span style="color:#ff0000;">CellPadding</span><span style="color:#0000ff;">="5"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>               <span style="color:#ff0000;">CellSpacing</span><span style="color:#0000ff;">="3"</span> <span style="color:#ff0000;">AutoGenerateColumns</span><span style="color:#0000ff;">="False"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>               <span style="color:#ff0000;">AllowSorting</span><span style="color:#0000ff;">="True"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>               <span style="color:#ff0000;">OnSorting</span><span style="color:#0000ff;">="CustomersSorting"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span>               <span style="color:#ff0000;">AllowPaging</span><span style="color:#0000ff;">="True"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum8">   8:</span>               <span style="color:#ff0000;">PageSize</span><span style="color:#0000ff;">="15"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum9">   9:</span>               <span style="color:#ff0000;">OnPageIndexChanging</span><span style="color:#0000ff;">="CustomersPageChaging"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum10">  10:</span>               <span style="color:#ff0000;">CssClass</span><span style="color:#0000ff;">="gridStyle"</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum11">  11:</span>     </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum12">  12:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Columns</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum13">  13:</span>         <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TemplateField</span> <span style="color:#ff0000;">HeaderText</span><span style="color:#0000ff;">="Name"</span> <span style="color:#ff0000;">SortExpression</span><span style="color:#0000ff;">="Name"</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum14">  14:</span>             <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum15">  15:</span>                 <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Literal</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="customerName"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum16">  16:</span>                              <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">='&lt;%# DataBinder.Eval(Container.DataItem, "Name") %&gt;'</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum17">  17:</span>                 <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:Literal</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum18">  18:</span>             <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum19">  19:</span>         <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:TemplateField</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum20">  20:</span>         </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum21">  21:</span>         <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TemplateField</span> <span style="color:#ff0000;">HeaderText</span><span style="color:#0000ff;">="Company"</span> <span style="color:#ff0000;">SortExpression</span><span style="color:#0000ff;">="Company"</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum22">  22:</span>             <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum23">  23:</span>                 <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Literal</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="customerCompany"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum24">  24:</span>                              <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">='&lt;%# DataBinder.Eval(Container.DataItem, "Company") %&gt;'</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum25">  25:</span>                 <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:Literal</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum26">  26:</span>             <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum27">  27:</span>         <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:TemplateField</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum28">  28:</span>         </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum29">  29:</span>         <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TemplateField</span> <span style="color:#ff0000;">HeaderText</span><span style="color:#0000ff;">="Country"</span> <span style="color:#ff0000;">SortExpression</span><span style="color:#0000ff;">="Country"</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum30">  30:</span>             <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum31">  31:</span>                 <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Literal</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="customerCountry"</span> </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum32">  32:</span>                             <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">='&lt;%# DataBinder.Eval(Container.DataItem, "Country") %&gt;'</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum33">  33:</span>                 <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:Literal</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum34">  34:</span>             <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum35">  35:</span>         <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:TemplateField</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum36">  36:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Columns</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum37">  37:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:GridView</span><span style="color:#0000ff;">&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>Then on the code-behind, I’ve set up a property called SortOrder. This property keeps track of the ViewState[“SortOrder”] value that is saved on page postbacks.</p>
<div style="border-bottom:silver 1px solid;border-left:silver 1px solid;line-height:1px!important;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> SortOrder</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span> {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>     get</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>         <span style="color:#0000ff;">if</span> (ViewState[<span style="color:#006080;">"SortOrder"</span>].ToString() == <span style="color:#006080;">"DESC"</span>)</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span>             ViewState[<span style="color:#006080;">"SortOrder"</span>] = <span style="color:#006080;">"ASC"</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum8">   8:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum9">   9:</span>         <span style="color:#0000ff;">else</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum10">  10:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum11">  11:</span>             ViewState[<span style="color:#006080;">"SortOrder"</span>] = <span style="color:#006080;">"DESC"</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum12">  12:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum13">  13:</span>&nbsp; </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum14">  14:</span>         <span style="color:#0000ff;">return</span> ViewState[<span style="color:#006080;">"SortOrder"</span>].ToString();</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum15">  15:</span>     }</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum16">  16:</span>     set</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum17">  17:</span>     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum18">  18:</span>         ViewState[<span style="color:#006080;">"SortOrder"</span>] = <span style="color:#0000ff;">value</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum19">  19:</span>     }</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum20">  20:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>The Page_Load method, sets the SortOrder property to an empty string, then calls a custom method to bind the data to the GridView control.</p>
<div style="border-bottom:silver 1px solid;border-left:silver 1px solid;line-height:1px!important;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Page_Load(<span style="color:#0000ff;">object</span> sender, EventArgs e)</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span> {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>     <span style="color:#0000ff;">if</span>(Page.IsPostBack) <span style="color:#0000ff;">return</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>&nbsp; </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>     ViewState[<span style="color:#006080;">"SortOrder"</span>] = <span style="color:#006080;">""</span>;</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>     BindCustomersGrid(<span style="color:#006080;">""</span>,<span style="color:#006080;">""</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>The custom bind method that is called from the Page_Load method takes in 2 string parameters. The parameters define the sort expression and the sort direction. </p>
<p>The method first checks if the sort expression is not an empty string. If it isn’t empty it then proceeds to use the <a href="http://msdn.microsoft.com/en-us/library/system.data.dataview.sort(v=vs.90).aspx">DataView Sort</a> property, prior to binding the data to the GridView control.</p>
<div style="border-bottom:silver 1px solid;border-left:silver 1px solid;line-height:1px!important;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> BindCustomersGrid(<span style="color:#0000ff;">string</span> sortExp, <span style="color:#0000ff;">string</span> sortDir)</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span> {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>     <span style="color:#008000;">// Custom method is called and DataView object is returned.</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>     var view = GetCustomerData();</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>&nbsp; </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>     <span style="color:#008000;">// Check if Sort Expression is available.</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span>     <span style="color:#0000ff;">if</span> (sortExp != <span style="color:#0000ff;">string</span>.Empty)</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum8">   8:</span>     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum9">   9:</span>         view.Sort = <span style="color:#0000ff;">string</span>.Format(<span style="color:#006080;">"{0} {1}"</span>, sortExp, sortDir);</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum10">  10:</span>     }</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum11">  11:</span>&nbsp; </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum12">  12:</span>     <span style="color:#008000;">// Bind data to GridView control.</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum13">  13:</span>     customersGrid.DataSource = view;</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum14">  14:</span>     customersGrid.DataBind();</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum15">  15:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>The custom method that returns the DataView object as shown in snippet above, is define in snippet below.</p>
<div style="border-bottom:silver 1px solid;border-left:silver 1px solid;line-height:1px!important;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> DataView GetCustomerData()</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span> {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>     <span style="color:#008000;">// Initialise and construct new DataTable</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>     <span style="color:#0000ff;">using</span> (var table = <span style="color:#0000ff;">new</span> DataTable())</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>     {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>         table.Columns.Add(<span style="color:#006080;">"Name"</span>, <span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">string</span>));</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span>         table.Columns.Add(<span style="color:#006080;">"Company"</span>, <span style="color:#0000ff;">typeof</span> (<span style="color:#0000ff;">string</span>));</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum8">   8:</span>         table.Columns.Add(<span style="color:#006080;">"Country"</span>, <span style="color:#0000ff;">typeof</span> (<span style="color:#0000ff;">string</span>));</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum9">   9:</span>&nbsp; </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum10">  10:</span>         <span style="color:#008000;">// Retrieve customer records from database layer</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum11">  11:</span>         <span style="color:#008000;">// and populate DateTable rows.</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum12">  12:</span>         <span style="color:#0000ff;">foreach</span> (var customer <span style="color:#0000ff;">in</span> CustomerData.GetAllCustomers())</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum13">  13:</span>         {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum14">  14:</span>             table.Rows.Add(customer.Name, customer.Company, customer.Country);</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum15">  15:</span>         }</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum16">  16:</span>&nbsp; </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum17">  17:</span>         <span style="color:#0000ff;">return</span> table.DefaultView;</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum18">  18:</span>     }</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum19">  19:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Now we come to the core part of this post. How do we perform sorting? Well, if you have followed the snippets along you would have noticed the custom bind method takes in 2 parameters. It then checks the sort expression parameter for a value, then uses the <a href="http://msdn.microsoft.com/en-us/library/system.data.dataview.sort(v=vs.90).aspx">DataView Sort</a> property to apply the sorting. Not too complex, but the easier bit is on the GridView OnSorting event as show below. Basically, calling the custom bind method by passing in both, the sort expression and the sort order.</p>
<div style="border-bottom:silver 1px solid;border-left:silver 1px solid;line-height:1px!important;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> CustomersSorting(<span style="color:#0000ff;">object</span> sender, GridViewSortEventArgs e)</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span> {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>     <span style="color:#008000;">// Sorting is performed by calling custom bind method and specifying</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>     <span style="color:#008000;">// sort expression as defined in GridView control and the SortOrder</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>     <span style="color:#008000;">// property value.</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span>     BindCustomersGrid(e.SortExpression, SortOrder);</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum7">   7:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Now to the next part of the post which is, How do we perform paging?. Well, on the GridView OnPageIndexChanging event, a new page index is set based on the GridViewPageEventArgs passed in, then the custom bind method is called again.</p>
<div style="border-bottom:silver 1px solid;border-left:silver 1px solid;line-height:1px!important;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> CustomersPageChaging(<span style="color:#0000ff;">object</span> sender, GridViewPageEventArgs e)</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span> {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>     customersGrid.PageIndex = e.NewPageIndex;</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>     BindCustomersGrid(<span style="color:#006080;">""</span>, <span style="color:#006080;">""</span>);</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Hope the post helps show how easy it is to get Paging and Sorting operations working, through the use of a DataView object.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/harishmathanan.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/harishmathanan.wordpress.com/459/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=459&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingmode.com/2012/02/27/aspnet-dataview-paging-and-sorting/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dfc3cebd7faac25d5f7ca66dfc04ea9d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">harishmathanan</media:title>
		</media:content>
	</item>
		<item>
		<title>Error Object cannot be cast from DBNull to other types.</title>
		<link>http://codingmode.com/2012/02/22/error-object-cannot-be-cast-from-dbnull-to-other-types/</link>
		<comments>http://codingmode.com/2012/02/22/error-object-cannot-be-cast-from-dbnull-to-other-types/#comments</comments>
		<pubDate>Wed, 22 Feb 2012 04:46:44 +0000</pubDate>
		<dc:creator>Harish Mathanan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Error]]></category>

		<guid isPermaLink="false">https://harishmathanan.wordpress.com/?p=455</guid>
		<description><![CDATA[This happens when the database is returning an object of type DBNull. Best approach towards handling this error would be to check if the object is of type DBNull first, before attempting to use it. Sample snippet below demonstrates this, using the Convert.IsDBNull method. 1: var reader = command.ExecuteReader() // assumes SqlCommand has been initialised [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=455&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This happens when the database is returning an object of type DBNull. Best approach towards handling this error would be to check if the object is of type DBNull first, before attempting to use it. Sample snippet below demonstrates this, using the <a href="http://msdn.microsoft.com/en-us/library/system.convert.isdbnull.aspx">Convert.IsDBNull</a> method.</p>
<div style="border-bottom:silver 1px solid;border-left:silver 1px solid;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;margin:20px 0 10px;padding:4px;" id="codeSnippetWrapper">
<div style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;line-height:1pt!important;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> var reader = command.ExecuteReader() <span style="color:#008000;">// assumes SqlCommand has been initialised</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span>&nbsp; </pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span> <span style="color:#0000ff;">if</span>(!Convert.IsDBNull(reader[<span style="color:#006080;">"DateInDatabase"</span>])) <span style="color:#008000;">// if it is not returning DBNull</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span> {</pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:white;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>     <span style="color:#008000;">// Do Something</span></pre>
<p><!--CRLF-->
<pre style="text-align:left;background-color:#f4f4f4;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum6">   6:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Hope this helps.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/harishmathanan.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/harishmathanan.wordpress.com/455/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingmode.com&#038;blog=10058827&#038;post=455&#038;subd=harishmathanan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingmode.com/2012/02/22/error-object-cannot-be-cast-from-dbnull-to-other-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dfc3cebd7faac25d5f7ca66dfc04ea9d?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">harishmathanan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
