<?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>Yappitty Yap</title>
	<atom:link href="http://kalpeshshirodker.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://kalpeshshirodker.wordpress.com</link>
	<description>"Lead, follow, or get out of the way."</description>
	<lastBuildDate>Mon, 05 Oct 2009 11:38:19 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='kalpeshshirodker.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/39b9f0f4a92a37a323fea6ad0aa6f632?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Yappitty Yap</title>
		<link>http://kalpeshshirodker.wordpress.com</link>
	</image>
			<item>
		<title>Getting the absolute path in ASP.NET which is application independent</title>
		<link>http://kalpeshshirodker.wordpress.com/2009/10/05/getting-the-absolute-path-in-asp-net-which-is-application-independent/</link>
		<comments>http://kalpeshshirodker.wordpress.com/2009/10/05/getting-the-absolute-path-in-asp-net-which-is-application-independent/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 11:37:49 +0000</pubDate>
		<dc:creator>kalpeshshirodker</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Dot Net]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://kalpeshshirodker.wordpress.com/2009/10/05/getting-the-absolute-path-in-asp-net-which-is-application-independent/</guid>
		<description><![CDATA[There comes a time when you need to get the absolute path to reference a file or some resource existing at some relative path of the website or web application. As things turned i was in need some of the similar functionality. The first thing that came to my mind was doing something similar :
&#160;

//get [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=95&subd=kalpeshshirodker&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>There comes a time when you need to get the absolute path to reference a file or some resource existing at some relative path of the website or web application. As things turned i was in need some of the similar functionality. The first thing that came to my mind was doing something similar :</p>
<p>&#160;</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0ef1f6db-8ebd-46ec-bced-7350501c3f9b" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#008000;">//</span><span style="color:#008000;">get the absolute path for the following relative path</span><span style="color:#008000;">
</span><span style="color:#000000;">String relPath </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">@"</span><span style="color:#800000;">~/ImageResource/ImageName.jpg</span><span style="color:#800000;">"</span><span style="color:#000000;">;
Control cntrl </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> Control();
String absPath </span><span style="color:#000000;">=</span><span style="color:#000000;"> cntrl.ResolveClientUrl(relPath);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>As it is this works great, but i wanted something more elegant, which doesnt involve the creation of the Control object. So i searched for sometime and stumbled upon an unknown utility function available, right over in .NET framework. Interesting to know that there so many useful functionality in-built in .NET framework which rarely are seen in practice. </p>
<p>Ok then about this utility API is called VirtualPathUtility and it is contained in the System.Web namespace. So using the VirtualUtilityPath you can rewrite the above piece of code as follows </p>
</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8d72dcf1-813c-4ddb-8d68-af20c55a3d5f" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;"> </span><span style="color:#008000;">//</span><span style="color:#008000;">get the absolute path for the following relative path</span><span style="color:#008000;">
</span><span style="color:#000000;">String relPath </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">@"</span><span style="color:#800000;">~/ImageResource/ImageName.jpg</span><span style="color:#800000;">"</span><span style="color:#000000;">;
String absPath </span><span style="color:#000000;">=</span><span style="color:#000000;">VirtualPathUtility.ToAbsolute(relPath);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Nice little function. But beware there are some pitfalls you need to avoid. This function is not one size fits all solution. You need to be aware of some of shortfalls described in some of the posts below. But for simple use, this works great.</p>
<p>&#160;</p>
<table cellspacing="0" cellpadding="2" width="459" border="0">
<tbody>
<tr>
<td valign="top" width="457"><strong>Related Posts</strong></td>
</tr>
<tr>
<td valign="top" width="457"><a title="MSDN: VirtualPathUtility Class" href="http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.aspx" target="_blank">MSDN: VirtualPathUtility Class</a></td>
</tr>
<tr>
<td valign="top" width="457"><a title="Weblogs.asp.net: VirtualPathUtility Class" href="http://weblogs.asp.net/dwahlin/archive/2006/03/30/441555.aspx" target="_blank">Weblogs.asp.net: VirtualPathUtility Class</a></td>
</tr>
<tr>
<td valign="top" width="457"><a title="How VirtualPathUtility combines paths in .Net 2.0 - level 100" href="http://codebetter.com/blogs/jeffrey.palermo/archive/2006/03/10/140621.aspx" target="_blank">How VirtualPathUtility combines paths in .Net 2.0 &#8211; level 100</a></td>
</tr>
</tbody>
</table>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kalpeshshirodker.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kalpeshshirodker.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kalpeshshirodker.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kalpeshshirodker.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kalpeshshirodker.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kalpeshshirodker.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kalpeshshirodker.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kalpeshshirodker.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kalpeshshirodker.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kalpeshshirodker.wordpress.com/95/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=95&subd=kalpeshshirodker&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://kalpeshshirodker.wordpress.com/2009/10/05/getting-the-absolute-path-in-asp-net-which-is-application-independent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bf0e9a8b13c5fa268cd3faa5d1d913ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kalpeshshirodker</media:title>
		</media:content>
	</item>
		<item>
		<title>How-To : Splitting delimited strings using XQuery in SQL</title>
		<link>http://kalpeshshirodker.wordpress.com/2009/09/25/splitting-delimited-strings-using-xquery/</link>
		<comments>http://kalpeshshirodker.wordpress.com/2009/09/25/splitting-delimited-strings-using-xquery/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 04:10:54 +0000</pubDate>
		<dc:creator>kalpeshshirodker</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[XQuery]]></category>

		<guid isPermaLink="false">http://kalpeshshirodker.wordpress.com/2009/09/25/splitting-delimited-strings-using-xquery/</guid>
		<description><![CDATA[Well, this was just kind of an experiment of sorts of using XQuery. The idea just occured some time back when i was trying some other similar experiment. 
Now as you all know there is no in-built function for Split in SQL. So lots of people have come out with different solution. This solution is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=91&subd=kalpeshshirodker&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Well, this was just kind of an experiment of sorts of using XQuery. The idea just occured some time back when i was trying some other similar experiment. </p>
<p>Now as you all know there is no in-built function for Split in SQL. So lots of people have come out with different solution. This solution is just another solution to the old problem of splitting delimited strings. </p>
<p>The solutions i found mostly revolved around string manipulation, and i have been using the same before my chance encounter with XML support in SQL. The solution i came out with was using XQuery support in SQL Server and it works pretty well too. My thoughts are, it might be just a little wee faster than the other implementation (no numbers from me on that front <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</p>
<h3>Implementation</h3>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3508b104-7dd5-4a92-ab44-2097df121f09" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">DECLARE</span><span style="color:#000000;"> </span><span style="color:#008000;">@DelimitedString</span><span style="color:#000000;"> </span><span style="color:#0000FF;">NVARCHAR</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">500</span><span style="color:#000000;">)
</span><span style="color:#0000FF;">DECLARE</span><span style="color:#000000;"> </span><span style="color:#008000;">@Delimiter</span><span style="color:#000000;"> </span><span style="color:#0000FF;">NVARCHAR</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">10</span><span style="color:#000000;">)
</span><span style="color:#0000FF;">DECLARE</span><span style="color:#000000;"> </span><span style="color:#008000;">@DelimitedStringXML</span><span style="color:#000000;"> XML

</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@DelimitedString</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">Apples,Bananas,Mangoes,Peaches,Watermelons</span><span style="color:#FF0000;">'</span><span style="color:#000000;">

</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@Delimiter</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">,</span><span style="color:#FF0000;">'</span><span style="color:#000000;">

</span><span style="color:#008080;">--</span><span style="color:#008080;">Create a document fragment for the delimited string</span><span style="color:#008080;">
</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@DelimitedString</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">REPLACE</span><span style="color:#000000;">(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;s&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;"> </span><span style="color:#808080;">+</span><span style="color:#000000;"> </span><span style="color:#008000;">@DelimitedString</span><span style="color:#000000;"> </span><span style="color:#808080;">+</span><span style="color:#000000;"> </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;/s&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;">,</span><span style="color:#008000;">@Delimiter</span><span style="color:#000000;">, </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;/s&gt;&lt;s&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;"> )
</span><span style="color:#0000FF;">PRINT</span><span style="color:#000000;"> </span><span style="color:#008000;">@DelimitedString</span><span style="color:#000000;"> </span><span style="color:#008080;">--</span><span style="color:#008080;">PRINTS &lt;s&gt;Apples&lt;/s&gt;&lt;s&gt;Bananas&lt;/s&gt;&lt;s&gt;Mangoes&lt;/s&gt;&lt;s&gt;Peaches&lt;/s&gt;&lt;s&gt;Watermelons&lt;/s&gt;</span><span style="color:#008080;">
</span><span style="color:#000000;">
</span><span style="color:#008080;">--</span><span style="color:#008080;">Cast the string to xml format, for use in the xquery</span><span style="color:#008080;">
</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@DelimitedStringXML</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">CAST</span><span style="color:#000000;">(</span><span style="color:#008000;">@DelimitedString</span><span style="color:#000000;"> </span><span style="color:#0000FF;">AS</span><span style="color:#000000;"> XML)

</span><span style="color:#0000FF;">SELECT</span><span style="color:#000000;"> t.r.query(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">./text()</span><span style="color:#FF0000;">'</span><span style="color:#000000;">)
</span><span style="color:#0000FF;">FROM</span><span style="color:#000000;"> </span><span style="color:#008000;">@DelimitedStringXML</span><span style="color:#000000;">.nodes(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">/s</span><span style="color:#FF0000;">'</span><span style="color:#000000;">) t(r)</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>For people who are unfamiliar with Xquery can check out the reference section.</p>
<p>Have fun and happy coding</p>
<h3>Reference</h3>
<p><a title="MSDN :: Introduction to XQuery in SQL Server 2005" href="http://msdn.microsoft.com/en-us/library/ms345122(SQL.90).aspx" target="_blank">MSDN :: Introduction to XQuery in SQL Server 2005</a></p>
<p><a title="MSDN Blogs :: XQuery Inside SQL Server 2005" href="http://blogs.msdn.com/mrorke/" target="_blank">MSDN Blogs :: XQuery Inside SQL Server 2005</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kalpeshshirodker.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kalpeshshirodker.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kalpeshshirodker.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kalpeshshirodker.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kalpeshshirodker.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kalpeshshirodker.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kalpeshshirodker.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kalpeshshirodker.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kalpeshshirodker.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kalpeshshirodker.wordpress.com/91/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=91&subd=kalpeshshirodker&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://kalpeshshirodker.wordpress.com/2009/09/25/splitting-delimited-strings-using-xquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bf0e9a8b13c5fa268cd3faa5d1d913ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kalpeshshirodker</media:title>
		</media:content>
	</item>
		<item>
		<title>Trimming extra space in SQL</title>
		<link>http://kalpeshshirodker.wordpress.com/2009/09/24/truncate-extra-space-in-sql/</link>
		<comments>http://kalpeshshirodker.wordpress.com/2009/09/24/truncate-extra-space-in-sql/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 05:40:23 +0000</pubDate>
		<dc:creator>kalpeshshirodker</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[XQuery]]></category>

		<guid isPermaLink="false">http://kalpeshshirodker.wordpress.com/?p=87</guid>
		<description><![CDATA[Sometime back i had come across this small problem. Mind you this is not a problem you come across many times, but it is interesting little problem nonetheless. The problem was removing extra spaces in a given string.
For e.g if you have some string like so “This is a&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; line which&#160;&#160;&#160; contains multiple&#160;&#160;&#160;&#160;&#160;&#160;&#160; spcaes.” and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=87&subd=kalpeshshirodker&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometime back i had come across this small problem. Mind you this is not a problem you come across many times, but it is interesting little problem nonetheless. The problem was removing extra spaces in a given string.</p>
<p>For e.g if you have some string like so “This is a&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; line which&#160;&#160;&#160; contains multiple&#160;&#160;&#160;&#160;&#160;&#160;&#160; spcaes.” and the disired result is with only one space seperating the words “This is a line which contains multiple spcaes.” </p>
<p>There are various ways you can do it; here i outline 2 methods</p>
<ul>
<li>Using SQL REPLACE function </li>
<li>Using SQL FOR XML XPATH </li>
</ul>
<h3>Using SQL REPLACE function</h3>
<p>First the solution :</p>
</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a1b4f1a7-579d-4b88-a83f-352912e30195" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#008080;">--</span><span style="color:#008080;">Using string replace</span><span style="color:#008080;">
</span><span style="color:#0000FF;">DECLARE</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#0000FF;">NVARCHAR</span><span style="color:#000000;">(</span><span style="color:#FF00FF;">MAX</span><span style="color:#000000;">)

</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">This is a                   line which    contains multiple        spaces.</span><span style="color:#FF0000;">'</span><span style="color:#000000;">
</span><span style="color:#0000FF;">PRINT</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;">

</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">REPLACE</span><span style="color:#000000;">(</span><span style="color:#FF00FF;">REPLACE</span><span style="color:#000000;">(</span><span style="color:#FF00FF;">REPLACE</span><span style="color:#000000;">(</span><span style="color:#008000;">@text</span><span style="color:#000000;">, </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">), </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;">), </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&gt;&lt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;">, </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">0</span><span style="color:#000000;">)), </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;">, </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">));
</span><span style="color:#0000FF;">PRINT</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Can you figure out what is happening here? Ok, lets split the REPLACE function for more clarity. I am not going to explain the code, you can see the comments <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>&#160;</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4fe08522-33b0-41d8-a63d-fb0654f22ba0" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#008080;">--</span><span style="color:#008080;">using replace : explanation</span><span style="color:#008080;">
</span><span style="color:#0000FF;">DECLARE</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#0000FF;">NVARCHAR</span><span style="color:#000000;">(</span><span style="color:#FF00FF;">MAX</span><span style="color:#000000;">)
</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">This is a                   line which    contains multiple        spaces.</span><span style="color:#FF0000;">'</span><span style="color:#000000;">
</span><span style="color:#0000FF;">PRINT</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#008080;">--</span><span style="color:#008080;">Print</span><span style="color:#008080;">
</span><span style="color:#000000;">
</span><span style="color:#008080;">--</span><span style="color:#008080;">Replace each space character with the following characters '&lt;&gt;'</span><span style="color:#008080;">
</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">REPLACE</span><span style="color:#000000;">(</span><span style="color:#008000;">@text</span><span style="color:#000000;">, </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">), </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;">);
</span><span style="color:#0000FF;">PRINT</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#008080;">--</span><span style="color:#008080;">PRINTS --This&lt;&gt;is&lt;&gt;a&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;line&lt;&gt;which&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;contains&lt;&gt;multiple&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;spaces.</span><span style="color:#008080;">
</span><span style="color:#000000;">
</span><span style="color:#008080;">--</span><span style="color:#008080;">Replace each '&lt;&gt;' character with the following characters with empty string</span><span style="color:#008080;">
</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">REPLACE</span><span style="color:#000000;">(</span><span style="color:#008000;">@text</span><span style="color:#000000;">, </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&gt;&lt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;">, </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">0</span><span style="color:#000000;">));
</span><span style="color:#0000FF;">PRINT</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#008080;">--</span><span style="color:#008080;">PRINTS This&lt;&gt;is&lt;&gt;a&lt;&gt;line&lt;&gt;which&lt;&gt;contains&lt;&gt;multiple&lt;&gt;spaces.</span><span style="color:#008080;">
</span><span style="color:#000000;">
</span><span style="color:#008080;">--</span><span style="color:#008080;">Now you have only single spaces which are denoted by '&lt;&gt;'</span><span style="color:#008080;">
--</span><span style="color:#008080;">Replace the '&lt;&gt;' with the space character </span><span style="color:#008080;">
</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">REPLACE</span><span style="color:#000000;">(</span><span style="color:#008000;">@text</span><span style="color:#000000;">, </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;">, </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">));
</span><span style="color:#0000FF;">PRINT</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#008080;">--</span><span style="color:#008080;">PRINTS This is a line which contains multiple spaces.</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<h3>Using SQL FOR XML XPATH</h3>
<p>Now this one is just for kicks <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I wanted to see if the same problem could be solved using the FOR XML PATH. And here is how it can be done.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ee1fba14-ff81-4397-8f39-9a02e962ba3f" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#008080;">--</span><span style="color:#008080;">using xml </span><span style="color:#008080;">
</span><span style="color:#0000FF;">DECLARE</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#0000FF;">NVARCHAR</span><span style="color:#000000;">(</span><span style="color:#FF00FF;">MAX</span><span style="color:#000000;">)
</span><span style="color:#0000FF;">DECLARE</span><span style="color:#000000;"> </span><span style="color:#008000;">@xmlText</span><span style="color:#000000;"> XML

</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">This is a                   line which    contains multiple        spaces.</span><span style="color:#FF0000;">'</span><span style="color:#000000;">

</span><span style="color:#008080;">--</span><span style="color:#008080;">Add the following string &lt;r&gt; at the beginning</span><span style="color:#008080;">
--</span><span style="color:#008080;">Add the following string &lt;/r&gt; at the end</span><span style="color:#008080;">
--</span><span style="color:#008080;">Replace each space with the following string '&lt;/r&gt;&lt;r&gt;'. </span><span style="color:#008080;">
--</span><span style="color:#008080;">This will give a document fragment in the following format</span><span style="color:#008080;">
--</span><span style="color:#008080;">&lt;r&gt;word&lt;/r&gt;&lt;r&gt;&lt;/r&gt;.....&lt;/r&gt;</span><span style="color:#008080;">
</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">REPLACE</span><span style="color:#000000;">(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;r&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;"> </span><span style="color:#808080;">+</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">+</span><span style="color:#000000;"> </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;/r&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;"> , </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">), </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;/r&gt;&lt;r&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;"> )
</span><span style="color:#0000FF;">PRINT</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#008080;">--</span><span style="color:#008080;">PRINTS &lt;r&gt;This&lt;/r&gt;&lt;r&gt;is&lt;/r&gt;&lt;r&gt;a&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;line&lt;/r&gt;&lt;r&gt;which&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;contains&lt;/r&gt;&lt;r&gt;multiple&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;&lt;/r&gt;&lt;r&gt;spaces.&lt;/r&gt;</span><span style="color:#008080;">
</span><span style="color:#000000;">
</span><span style="color:#008080;">--</span><span style="color:#008080;">Cast the string to XML datatype so that we can work with the xml data type function</span><span style="color:#008080;">
</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@xmlText</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">CAST</span><span style="color:#000000;">(</span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#0000FF;">AS</span><span style="color:#000000;"> xml)

</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;">
    (
    </span><span style="color:#008080;">--</span><span style="color:#008080;">Select all the nodes which are not empty</span><span style="color:#008080;">
--</span><span style="color:#008080;">Append a space at the end</span><span style="color:#008080;">
</span><span style="color:#000000;">        </span><span style="color:#0000FF;">SELECT</span><span style="color:#000000;"> t.r.value(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">.[1]</span><span style="color:#FF0000;">'</span><span style="color:#000000;">, </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">nvarchar(max)</span><span style="color:#FF0000;">'</span><span style="color:#000000;">) </span><span style="color:#808080;">+</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">)
        </span><span style="color:#0000FF;">FROM</span><span style="color:#000000;"> </span><span style="color:#008000;">@xmlText</span><span style="color:#000000;">.nodes(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">//r</span><span style="color:#FF0000;">'</span><span style="color:#000000;">) t(r)
        </span><span style="color:#0000FF;">WHERE</span><span style="color:#000000;"> t.r.value(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">.[1]</span><span style="color:#FF0000;">'</span><span style="color:#000000;">, </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">nvarchar(max)</span><span style="color:#FF0000;">'</span><span style="color:#000000;">) </span><span style="color:#808080;">&lt;&gt;</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">)
        </span><span style="color:#0000FF;">FOR</span><span style="color:#000000;"> XML PATH (</span><span style="color:#FF0000;">''</span><span style="color:#000000;">)
    )
</span><span style="color:#0000FF;">PRINT</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#008080;">--</span><span style="color:#008080;">PRINTS This is a line which contains multiple spaces.</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>It is normally advised to use Exist function instead of Value function as shown above for checking in the where clause. Here is how it can be done with Exist clause.</p>
</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a089da5f-f1d6-43ed-ac24-0b5353f274a6" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#008080;">--</span><span style="color:#008080;">using xml </span><span style="color:#008080;">
</span><span style="color:#0000FF;">DECLARE</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#0000FF;">NVARCHAR</span><span style="color:#000000;">(</span><span style="color:#FF00FF;">MAX</span><span style="color:#000000;">)
</span><span style="color:#0000FF;">DECLARE</span><span style="color:#000000;"> </span><span style="color:#008000;">@xmlText</span><span style="color:#000000;"> XML

</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">This is a                   line which    contains multiple        spaces.</span><span style="color:#FF0000;">'</span><span style="color:#000000;">
</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">REPLACE</span><span style="color:#000000;">(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;r&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;"> </span><span style="color:#808080;">+</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">+</span><span style="color:#000000;"> </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;/r&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;"> , </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">), </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">&lt;/r&gt;&lt;r&gt;</span><span style="color:#FF0000;">'</span><span style="color:#000000;"> )

</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;">
    (
        </span><span style="color:#0000FF;">SELECT</span><span style="color:#000000;"> t.r.value(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">.[1]</span><span style="color:#FF0000;">'</span><span style="color:#000000;">, </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">nvarchar(max)</span><span style="color:#FF0000;">'</span><span style="color:#000000;">) </span><span style="color:#808080;">+</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">SPACE</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">)
        </span><span style="color:#0000FF;">FROM</span><span style="color:#000000;"> </span><span style="color:#008000;">@xmlText</span><span style="color:#000000;">.nodes(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">//r</span><span style="color:#FF0000;">'</span><span style="color:#000000;">) t(r)
        </span><span style="color:#0000FF;">WHERE</span><span style="color:#000000;"> t.r.exist(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">./child::text()</span><span style="color:#FF0000;">'</span><span style="color:#000000;">) </span><span style="color:#808080;">&gt;</span><span style="color:#000000;"> </span><span style="color:#800000;font-weight:bold;">0</span><span style="color:#000000;">
        </span><span style="color:#0000FF;">FOR</span><span style="color:#000000;"> XML PATH (</span><span style="color:#FF0000;">''</span><span style="color:#000000;">)
    )
</span><span style="color:#0000FF;">PRINT</span><span style="color:#000000;"> </span><span style="color:#008000;">@text</span><span style="color:#000000;"> </span><span style="color:#008080;">--</span><span style="color:#008080;">PRINTS This is a line which contains multiple spaces. </span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Nice. Have fun and Happy coding <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kalpeshshirodker.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kalpeshshirodker.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kalpeshshirodker.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kalpeshshirodker.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kalpeshshirodker.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kalpeshshirodker.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kalpeshshirodker.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kalpeshshirodker.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kalpeshshirodker.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kalpeshshirodker.wordpress.com/87/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=87&subd=kalpeshshirodker&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://kalpeshshirodker.wordpress.com/2009/09/24/truncate-extra-space-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bf0e9a8b13c5fa268cd3faa5d1d913ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kalpeshshirodker</media:title>
		</media:content>
	</item>
		<item>
		<title>Dataset to XML and loss of information</title>
		<link>http://kalpeshshirodker.wordpress.com/2009/09/23/dataset-to-xml/</link>
		<comments>http://kalpeshshirodker.wordpress.com/2009/09/23/dataset-to-xml/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 10:16:07 +0000</pubDate>
		<dc:creator>kalpeshshirodker</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Dot Net]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://kalpeshshirodker.wordpress.com/2009/09/23/dataset-to-xml/</guid>
		<description><![CDATA[When weneed to persist a dataset object, usually what we do is use the WriteXml() method for the Dataset object. For most cases the default implementation works fine. 
So 

DataSet dsObj = GetDatasetObject();
dsObj.WriteXml(filename);

 If while saving the dataset, a particular contained NULL values, that column information will not be outputted. So when you try to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=85&subd=kalpeshshirodker&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>When weneed to persist a dataset object, usually what we do is use the WriteXml() method for the Dataset object. For most cases the default implementation works fine. </p>
<p>So </p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9b0a02f5-8be0-4fb3-bf67-614396b2b854" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">DataSet dsObj </span><span style="color:#808080;">=</span><span style="color:#000000;"> GetDatasetObject();
dsObj.WriteXml(filename);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>the above command works fine and saves the dataset xml into the specified file, provided that all the columns have data. </p>
<p>This is the default implementation and this is how it works; if the column contains valid value or some sort of value the column will be written to the output else if the column contains a NULL value, that particular column will not be written to the output. Now this is not so much of an issue, if there is atleast one row which contains a proper value while all the other rows contain NULL.</p>
<p>The fun starts when the value in a given column in all the rows contain NULL, what happens? What happens, is that since there is no value for the specified column, the column is not outputted to the file name. </p>
<p>Consider that you want to read the xml into dataset which had been previously written.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b463da46-c482-4949-a7d2-432e0228d9cf" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">DataSet dsObj </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">;
dsObj.ReadXml(filename);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p> If while saving the dataset, a particular contained NULL values, that column information will not be outputted. So when you try to load the xml again in the dataset, what occurs is loss of information, (even though NULL value is no information in particular). So the column will not be created in the dataset and any access to it will result in an exception.</p>
<p>To overcome this scenario, you need to use the overloaded version for the WriteXml and ReadXml function as below</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9ecacd21-b6e5-4af9-8766-13a13032274a" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;"> </span><span style="color:#808080;">//</span><span style="color:#000000;">Write dataset </span><span style="color:#0000FF;">schema</span><span style="color:#000000;">, along </span><span style="color:#0000FF;">with</span><span style="color:#000000;"> the data
DataSet dsObj </span><span style="color:#808080;">=</span><span style="color:#000000;"> GetDatasetObject();
dsObj.WriteXml(filename, XmlWriteMode.WriteSchema);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>What this statement does is it writes the schema associated with the dataset, so even if a column contains all NULL values, we shall not loose the column information. Now it is a simple matter of loading the dataset back.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8d5a54e2-8a77-43c1-a5ef-b5317a58cd3f" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#808080;">//</span><span style="color:#0000FF;">create</span><span style="color:#000000;"> the dataset based </span><span style="color:#0000FF;">on</span><span style="color:#000000;"> the specified </span><span style="color:#0000FF;">schema</span><span style="color:#000000;">
DataSet dsObj </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">;
dsObj.ReadXml(filename, XmlReadMode.ReadSchema);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Now we are good. Happy coding.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kalpeshshirodker.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kalpeshshirodker.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kalpeshshirodker.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kalpeshshirodker.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kalpeshshirodker.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kalpeshshirodker.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kalpeshshirodker.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kalpeshshirodker.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kalpeshshirodker.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kalpeshshirodker.wordpress.com/85/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=85&subd=kalpeshshirodker&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://kalpeshshirodker.wordpress.com/2009/09/23/dataset-to-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bf0e9a8b13c5fa268cd3faa5d1d913ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kalpeshshirodker</media:title>
		</media:content>
	</item>
		<item>
		<title>Online Conversion Websites</title>
		<link>http://kalpeshshirodker.wordpress.com/2009/09/21/online-conversion-websites/</link>
		<comments>http://kalpeshshirodker.wordpress.com/2009/09/21/online-conversion-websites/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 13:51:22 +0000</pubDate>
		<dc:creator>kalpeshshirodker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kalpeshshirodker.wordpress.com/2009/09/21/online-conversion-websites/</guid>
		<description><![CDATA[Sometime or the other you may want to convert your files from one file to other. There whole lot of sites which allows you to do it. Here are a couple of sites which are nice easy to use and no fuss.
youconverit
ZamZar
pdfonline
Comet Docs
&#160;
Enjoy!!!
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=82&subd=kalpeshshirodker&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometime or the other you may want to convert your files from one file to other. There whole lot of sites which allows you to do it. Here are a couple of sites which are nice easy to use and no fuss.</p>
<p><a href="http://www.youconvertit.com/" target="_blank">youconverit</a></p>
<p><a title="ZamZar" href="http://www.zamzar.com" target="_blank">ZamZar</a></p>
<p><a title="pdfonline" href="http://www.pdfonline.com/" target="_blank">pdfonline</a></p>
<p><a title="Comet Docs" href="http://www.cometdocs.com/" target="_blank">Comet Docs</a></p>
<p>&#160;</p>
<p>Enjoy!!!</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kalpeshshirodker.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kalpeshshirodker.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kalpeshshirodker.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kalpeshshirodker.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kalpeshshirodker.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kalpeshshirodker.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kalpeshshirodker.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kalpeshshirodker.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kalpeshshirodker.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kalpeshshirodker.wordpress.com/82/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=82&subd=kalpeshshirodker&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://kalpeshshirodker.wordpress.com/2009/09/21/online-conversion-websites/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bf0e9a8b13c5fa268cd3faa5d1d913ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kalpeshshirodker</media:title>
		</media:content>
	</item>
		<item>
		<title>Me and my camera : Adventures of an amateur photographer</title>
		<link>http://kalpeshshirodker.wordpress.com/2009/09/21/me-and-my-camera-adventures-of-an-amateur-photographer/</link>
		<comments>http://kalpeshshirodker.wordpress.com/2009/09/21/me-and-my-camera-adventures-of-an-amateur-photographer/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 12:15:24 +0000</pubDate>
		<dc:creator>kalpeshshirodker</dc:creator>
				<category><![CDATA[Chatter]]></category>
		<category><![CDATA[Extra-Curricular Activities]]></category>
		<category><![CDATA[Photography]]></category>

		<guid isPermaLink="false">http://kalpeshshirodker.wordpress.com/2009/09/21/me-and-my-camera-adventures-of-an-amateur-photographer/</guid>
		<description><![CDATA[Photography is one of my hobby. But film photography is very expensive and poor me cannot afford the luxury, so recently i went ahead and gifted myself a digital camera so that i could pursue my hobby. 
As digital camera market is ever increasing and there are good cameras with good function around i splurged [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=81&subd=kalpeshshirodker&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Photography is one of my hobby. But film photography is very expensive and poor me cannot afford the luxury, so recently i went ahead and gifted myself a digital camera so that i could pursue my hobby. </p>
<p>As digital camera market is ever increasing and there are good cameras with good function around i splurged a little and got a Canon S5is. It turned out to be a pretty good investment, now i can pursue my hobby without feeling a pinch (well i already felt the pinch when i bought it, so now i am ok <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> )</p>
<p>The Canon S5IS provides good control over the manual setting, so i could play around a little with ligthing, focus and the stuff. I was pretty surprised by some of the photos i got, quite pleasing and all the credit goes to my subjects (some of them didnt even know i was capturing them and some didnt care <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> )</p>
<p>For posterity i posted the photos online at <a title="Photoshop.com :: Kalpesh Shirodker&#39;s Album" href="https://www.photoshop.com/user/kalpeshshirodker" target="_blank">Photoshop.com</a>. By the some of the photos are pretty old, from the times when i didnt have a camera of my own and</p>
<p>had to borrow one from my friends <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>This is supposed to be frequently updated page, but the lazy person in me stops me every time, so i intend to keep it updated, dont count on it though.</p>
<p>Hope you like them <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here is a glimpse of some of the photos, for the complete list follow the link : <a title="Photoshop.com :: Kalpesh Shirodker&#39;s Album" href="https://www.photoshop.com/user/kalpeshshirodker">Photoshop.com</a>.</p>
<p>
<div class="wlWriterEditableSmartContent" id="scid:66721397-FF69-4ca6-AEC4-17E6B3208830:11b5e6d9-51ab-4674-bea1-3ffb2ffdaf7b" style="display:inline;float:none;margin:0;padding:0;"><a style="border:0;" href="http://cid-80ac0c7db15305cc.skydrive.live.com/redir.aspx?page=browse&amp;resid=80AC0C7DB15305CC!158&amp;ct=photos"><img style="border:0;" alt="View Me and my camera" src="http://kalpeshshirodker.files.wordpress.com/2009/09/inlinerepresentation186ff516cd2e4f5f8228ef241b4a3a76.jpg" /></a>
<div style="width:400px;text-align:right;"><a href="http://cid-80ac0c7db15305cc.skydrive.live.com/redir.aspx?page=browse&amp;resid=80AC0C7DB15305CC!158&amp;ct=photos">View Full Album</a></div>
</div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kalpeshshirodker.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kalpeshshirodker.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kalpeshshirodker.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kalpeshshirodker.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kalpeshshirodker.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kalpeshshirodker.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kalpeshshirodker.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kalpeshshirodker.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kalpeshshirodker.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kalpeshshirodker.wordpress.com/81/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=81&subd=kalpeshshirodker&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://kalpeshshirodker.wordpress.com/2009/09/21/me-and-my-camera-adventures-of-an-amateur-photographer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bf0e9a8b13c5fa268cd3faa5d1d913ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kalpeshshirodker</media:title>
		</media:content>

		<media:content url="http://kalpeshshirodker.files.wordpress.com/2009/09/inlinerepresentation186ff516cd2e4f5f8228ef241b4a3a76.jpg" medium="image">
			<media:title type="html">View Me and my camera</media:title>
		</media:content>
	</item>
		<item>
		<title>Retrieving comma seperated values from SQL Server</title>
		<link>http://kalpeshshirodker.wordpress.com/2009/09/19/retrieving-comma-seperated-values-from-sql-server/</link>
		<comments>http://kalpeshshirodker.wordpress.com/2009/09/19/retrieving-comma-seperated-values-from-sql-server/#comments</comments>
		<pubDate>Sat, 19 Sep 2009 13:55:05 +0000</pubDate>
		<dc:creator>kalpeshshirodker</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[XQuery]]></category>

		<guid isPermaLink="false">http://kalpeshshirodker.wordpress.com/2009/09/19/retrieving-comma-seperated-values-from-sql-server/</guid>
		<description><![CDATA[Many a times, we are pressed with a problem of retrieving comma seperated values from SQL server. There are quite a few ways to do it, for instance using cursors, using user defined functions or using COALESCE (the most preferred). Come SQL Server 2005 and there is one more technique which can be employed (his [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=74&subd=kalpeshshirodker&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Many a times, we are pressed with a problem of retrieving comma seperated values from SQL server. There are quite a few ways to do it, for instance using cursors, using user defined functions or using COALESCE (the most preferred). Come SQL Server 2005 and there is one more technique which can be employed (his is the one i prefer the most, simple, elegant and no fuss)</p>
<p>In SQL Server 2005 and above you can use the FOR XML Path mode syntax to achieve the result set. We wont go in details of how FOR XML Path works, please check the reference link for more information</p>
<p>Lets take a small example; suppose we wanted to return a list of all the tables contained in the database in a comma seperated list. Using the XML Path Mode we can do it in the following manner:</p>
<p>&#160;</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:16933cf4-9ea6-4bee-b9cb-a39e49984d99" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">DECLARE</span><span style="color:#000000;"> </span><span style="color:#008000;">@commaSeperatedTableName</span><span style="color:#000000;"> </span><span style="color:#0000FF;">NVARCHAR</span><span style="color:#000000;">(</span><span style="color:#FF00FF;">MAX</span><span style="color:#000000;">)

</span><span style="color:#0000FF;">SET</span><span style="color:#000000;"> </span><span style="color:#008000;">@commaSeperatedTableName</span><span style="color:#000000;"> </span><span style="color:#808080;">=</span><span style="color:#000000;"> (
</span><span style="color:#0000FF;">select</span><span style="color:#000000;"> </span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">,</span><span style="color:#FF0000;">'</span><span style="color:#000000;"> </span><span style="color:#808080;">+</span><span style="color:#000000;"> </span><span style="color:#FF0000;">[</span><span style="color:#FF0000;">name</span><span style="color:#FF0000;">]</span><span style="color:#000000;">
</span><span style="color:#0000FF;">from</span><span style="color:#000000;"> sys.tables
</span><span style="color:#0000FF;">for</span><span style="color:#000000;"> xml path (</span><span style="color:#FF0000;">''</span><span style="color:#000000;">))</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The @commaSeperatedTableName variable will contain the list of all table contained in the database in the following format : “,tbl1,tb2,tb3,tb4”</p>
<p>Note that there is comma prepended at the beginning of the string, if we want to remove the comma so that the returned result in the following format “,tbl1,tb2,tb3,tb4”, all we need to do is use the STUFF function available in the SQL Server</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:dc8d3464-9f6c-42ff-92a4-1aae9b91be89" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">SELECT</span><span style="color:#000000;"> </span><span style="color:#FF00FF;">STUFF</span><span style="color:#000000;">(</span><span style="color:#008000;">@commaSeperatedTableName</span><span style="color:#000000;">, </span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">,</span><span style="color:#800000;font-weight:bold;">1</span><span style="color:#000000;">,</span><span style="color:#FF0000;">''</span><span style="color:#000000;">)
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Sweet, this is as easy as it can get. Happy querying.</p>
<p>Reference</p>
<p><a title="MSDN: Using XML PATH Mode" href="http://msdn.microsoft.com/en-us/library/ms189885.aspx" target="_blank">MSDN: Using XML PATH Mode</a></p>
<p><a title="GeeksWithBlogs: FOR XML PATH - A New Mode in FOR XML with SQL Server 2005." href="http://geekswithblogs.net/chavansachinr/archive/2007/04/30/112132.aspx" target="_blank">GeeksWithBlogs: FOR XML PATH &#8211; A New Mode in FOR XML with SQL Server 2005.</a></p>
<p><a title="MSDN: STUFF (Transact-SQL)" href="http://technet.microsoft.com/en-us/library/ms188043.aspx" target="_blank">MSDN: STUFF (Transact-SQL)</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kalpeshshirodker.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kalpeshshirodker.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kalpeshshirodker.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kalpeshshirodker.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kalpeshshirodker.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kalpeshshirodker.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kalpeshshirodker.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kalpeshshirodker.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kalpeshshirodker.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kalpeshshirodker.wordpress.com/74/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=74&subd=kalpeshshirodker&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://kalpeshshirodker.wordpress.com/2009/09/19/retrieving-comma-seperated-values-from-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bf0e9a8b13c5fa268cd3faa5d1d913ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kalpeshshirodker</media:title>
		</media:content>
	</item>
		<item>
		<title>Using custom serialization to store object information in ViewState</title>
		<link>http://kalpeshshirodker.wordpress.com/2009/09/19/using-custom-serialization-to-store-object-information-in-viewstate/</link>
		<comments>http://kalpeshshirodker.wordpress.com/2009/09/19/using-custom-serialization-to-store-object-information-in-viewstate/#comments</comments>
		<pubDate>Sat, 19 Sep 2009 12:45:43 +0000</pubDate>
		<dc:creator>kalpeshshirodker</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Dot Net]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kalpeshshirodker.wordpress.com/?p=67</guid>
		<description><![CDATA[Recently i came across a wierd problem while developing a web user control. I needed to persist some data across page postback’s. I took the ViewState approach, which had been used previously to do the same task. 
I had thought that it would be easy as it had been done a multiple times earlier, but [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=67&subd=kalpeshshirodker&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Recently i came across a wierd problem while developing a web user control. I needed to persist some data across page postback’s. I took the ViewState approach, which had been used previously to do the same task. </p>
<p>I had thought that it would be easy as it had been done a multiple times earlier, but i was in for a surprise. The object i was trying to persist could not be serialized, moreover it was a object i could not change to make it serializable, now i was in dilemma. After trying various methods and loosing my hair over it, i finally stumbled upon an interesting solution (not after i a lot of poking and prodding).</p>
<p>The solution i found was how ASP.Net does serialization itself, after a bit of nosing around using .NET reflector in the System.Web namespace. </p>
<p>After persisted searching, I realized that .Net framework used IStateFormatter object to serialize objects. IStateFormatter is contained is part of the PageStatePersister class (Please check the reference section below for more information)</p>
<p>We wont go into much detail about what is PageStatePersister object, we shall see an example of how the problem was resolved. The StateFormatter class provides to methods to serialized and deserialize an object, quite naturally called Serialize and Deserialize namely. </p>
<p>The Serialize method takes a parameter of type Object and returns an serialized string, which we can easily save to ViewState. Conversely the Deserialize method takes this serialized string and converts it back into the Object. We shall need some handling to convert the our object type to and fro during the serialization process</p>
<p>Lets say i needed to persist a collection of objects (BookDetail) to the View State object. </p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:93a35d83-f156-4ca0-999b-cc99454c5bcd" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:White;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">List</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">BookDetail</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> bkDetailCollection </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> List</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">BookDetail</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> (); </span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>We need to conver the bkDetailCollection collection to Object class inorder to use StateFormatter class. Your can use any way you want to convert it into an object </p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2db802fb-0505-4818-891e-082255e49cbf" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">Object objBookDetails </span><span style="color:#000000;">=</span><span style="color:#000000;"> GetBookDetailCollectionObject(); </span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Once we get the above object it is a simple method of just doing the following for serializing the object </p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1dd62339-162e-4ec5-94bb-76b88a39e7e2" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:hidden;"><span style="color:#000000;">IStateFormatter stateFormatter </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> ObjectStateFormatter();
String strSerializedObject </span><span style="color:#000000;">=</span><span style="color:#000000;"> stateFormatter.Serialize(objBookDetails ); 

</span><span style="color:#008000;">//</span><span style="color:#008000;">Persist to view state</span><span style="color:#008000;">
</span><span style="color:#000000;">ViewState[</span><span style="color:#800000;">"</span><span style="color:#800000;">persistedBookDetails</span><span style="color:#800000;">"</span><span style="color:#000000;">] </span><span style="color:#000000;">=</span><span style="color:#000000;"> str;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>and the reverse code is just as simple </p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a231ac71-861c-46f3-9bf7-85868db10e2c" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">String persistedobjBookDetailsString </span><span style="color:#000000;">=</span><span style="color:#000000;"> ViewState[</span><span style="color:#800000;">"</span><span style="color:#800000;">persistedBookDetails</span><span style="color:#800000;">"</span><span style="color:#000000;">]; 

                </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (</span><span style="color:#000000;">!</span><span style="color:#000000;">String.IsNullOrEmpty(persistedobjBookDetailsString ))
                {
                    IStateFormatter stateFormatter </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> ObjectStateFormatter();
                    objBookDetails </span><span style="color:#000000;">=</span><span style="color:#000000;"> stateFormatter.Deserialize(persistedobjBookDetailsString ); 

</span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (objBookDetails </span><span style="color:#000000;">!=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">)
                    {
                        List</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">BookDetail</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> bkDetailCollection </span><span style="color:#000000;">=</span><span style="color:#000000;"> GetBookDetailCollectionCollection(objBookDetails );
                    } 

                }
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Now there it is, using this technique you can serialize and deserialize any type of custom method. Mind you this is a bit of an extreme solution, when nothing else works <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>Happy coding </p>
<p>Reference</p>
<p><a title="MSDN: PageStatePersister Class" href="http://msdn.microsoft.com/en-us/library/system.web.ui.pagestatepersister(VS.80).aspx" target="_blank">MSDN: PageStatePersister Class</a></p>
<p><a title="4GuysFromRolla.com: Persisting Page State in ASP.NET 2.0" href="http://www.4guysfromrolla.com/demos/printPage.aspx?path=/articles/011707-1.aspx" target="_blank">4GuysFromRolla.com: Persisting Page State in ASP.NET 2.0</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kalpeshshirodker.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kalpeshshirodker.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kalpeshshirodker.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kalpeshshirodker.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kalpeshshirodker.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kalpeshshirodker.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kalpeshshirodker.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kalpeshshirodker.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kalpeshshirodker.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kalpeshshirodker.wordpress.com/67/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=67&subd=kalpeshshirodker&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://kalpeshshirodker.wordpress.com/2009/09/19/using-custom-serialization-to-store-object-information-in-viewstate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bf0e9a8b13c5fa268cd3faa5d1d913ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kalpeshshirodker</media:title>
		</media:content>
	</item>
		<item>
		<title>HOW-TO: Search string value contained in comma seperated string</title>
		<link>http://kalpeshshirodker.wordpress.com/2009/09/14/how-to-search-string-value-contained-in-comma-seperated-string/</link>
		<comments>http://kalpeshshirodker.wordpress.com/2009/09/14/how-to-search-string-value-contained-in-comma-seperated-string/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 10:29:45 +0000</pubDate>
		<dc:creator>kalpeshshirodker</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://kalpeshshirodker.wordpress.com/2009/09/14/how-to-search-string-value-contained-in-comma-seperated-string/</guid>
		<description><![CDATA[Quite often when we need to pass multiple values to the stored procedure, we pass the value as comma seperated string. The problem is how do we search for a column having a value contained in the comma seperated string.
For e.g. suppose you have a Books table which has &#34;Type&#34; property. The &#34;Type&#34; property can [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=65&subd=kalpeshshirodker&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Quite often when we need to pass multiple values to the stored procedure, we pass the value as comma seperated string. The problem is how do we search for a column having a value contained in the comma seperated string.</p>
<p>For e.g. suppose you have a Books table which has &quot;Type&quot; property. The &quot;Type&quot; property can hold one of the following values &quot;Novel&quot;, &quot;Magazine&quot;, &quot;Journal&quot; etc&#8230; </p>
<p>Now we want to search for books based on certain type of books, take for instance Novel and Magazine. For this instance what we would do is pass the values as comma seperated, so our sp will get the values as &quot;Novel,Magazine&quot;. </p>
<p>There are various ways by which this can be accomplished. One of these method which is widely used is the split operation. i.e. we split the values in the corresponding values and then we make a search in the table</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f0fc611e-11be-4907-8c8a-e5a46ca80eb5" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">SELECT</span><span style="color:#000000;"> </span><span style="color:#808080;">*</span><span style="color:#000000;">
</span><span style="color:#0000FF;">FROM</span><span style="color:#000000;"> Books bks
</span><span style="color:#0000FF;">WHERE</span><span style="color:#000000;"> bks.Type </span><span style="color:#808080;">IN</span><span style="color:#000000;">
(</span><span style="color:#0000FF;">SELECT</span><span style="color:#000000;"> </span><span style="color:#808080;">*</span><span style="color:#000000;">
</span><span style="color:#0000FF;">FROM</span><span style="color:#000000;"> dbo.Split(</span><span style="color:#FF0000;">'</span><span style="color:#FF0000;">Novel,Magazine</span><span style="color:#FF0000;">'</span><span style="color:#000000;">) </span><span style="color:#008080;">--</span><span style="color:#008080;">dbo.split is user defined funcation which returns table containing one row per each value </span><span style="color:#008080;">
</span><span style="color:#000000;">) 

</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This method works fine, well and good. </p>
<p>There is another method and the whole credit for this method goes to my collegues Hrishikesh, who showed this method to me. The method is very simple and it makes the use of like operator (which many would shun)</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4430d9c0-93b6-4f80-ade6-20950ecdad7e" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">declare</span><span style="color:#000000;"> </span><span style="color:#008000;">@types</span><span style="color:#000000;"> </span><span style="color:#0000FF;">as</span><span style="color:#000000;"> </span><span style="color:#0000FF;">nvarchar</span><span style="color:#000000;">(</span><span style="color:#800000;font-weight:bold;">250</span><span style="color:#000000;">)
</span><span style="color:#0000FF;">set</span><span style="color:#000000;"> </span><span style="color:#008000;">@types</span><span style="color:#808080;">=</span><span style="color:#000000;"> ‘Novel,Magazine’

–</span><span style="color:#0000FF;">add</span><span style="color:#000000;"> a comma at the start </span><span style="color:#808080;">and</span><span style="color:#000000;"> </span><span style="color:#0000FF;">end</span><span style="color:#000000;"> </span><span style="color:#0000FF;">of</span><span style="color:#000000;"> the string
</span><span style="color:#0000FF;">set</span><span style="color:#000000;"> </span><span style="color:#008000;">@types</span><span style="color:#808080;">=</span><span style="color:#000000;"> ‘,’</span><span style="color:#808080;">+</span><span style="color:#008000;">@types</span><span style="color:#808080;">+</span><span style="color:#000000;">’,’
</span><span style="color:#0000FF;">select</span><span style="color:#000000;"> </span><span style="color:#808080;">*</span><span style="color:#000000;">
</span><span style="color:#0000FF;">from</span><span style="color:#000000;"> Books
</span><span style="color:#0000FF;">where</span><span style="color:#000000;"> </span><span style="color:#008000;">@types</span><span style="color:#000000;"> </span><span style="color:#808080;">LIKE</span><span style="color:#000000;"> ‘</span><span style="color:#808080;">%</span><span style="color:#000000;">,’ </span><span style="color:#808080;">+</span><span style="color:#000000;">type</span><span style="color:#808080;">+</span><span style="color:#000000;"> ‘,</span><span style="color:#808080;">%</span><span style="color:#000000;">’
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The idea is very simple : </p>
<p>We get the values contained in the column Type and add the comma string at either end so that we get a value in the following format &quot;,Novel,&quot;, &quot;,Magazine,&quot; etc. Next we check if the value is contained in the comma seperated string, if the value is found we get the result as expected.</p>
<p>This technique uses very less code and works like a charm. Happy coding.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kalpeshshirodker.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kalpeshshirodker.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kalpeshshirodker.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kalpeshshirodker.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kalpeshshirodker.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kalpeshshirodker.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kalpeshshirodker.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kalpeshshirodker.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kalpeshshirodker.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kalpeshshirodker.wordpress.com/65/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=65&subd=kalpeshshirodker&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://kalpeshshirodker.wordpress.com/2009/09/14/how-to-search-string-value-contained-in-comma-seperated-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bf0e9a8b13c5fa268cd3faa5d1d913ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kalpeshshirodker</media:title>
		</media:content>
	</item>
		<item>
		<title>Enum with Flags Attribute and Extension method</title>
		<link>http://kalpeshshirodker.wordpress.com/2009/09/12/enum-with-flags-attribute-and-extension-method/</link>
		<comments>http://kalpeshshirodker.wordpress.com/2009/09/12/enum-with-flags-attribute-and-extension-method/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 07:37:47 +0000</pubDate>
		<dc:creator>kalpeshshirodker</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Dot Net]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://kalpeshshirodker.wordpress.com/?p=63</guid>
		<description><![CDATA[For quite some time now i have been intending to write about Enum with Flags attribute set. Enum with Flags attribute set allows the enum to be used as Bit flags where each bit signifies whether a particular option is set or not. When using this very powerful feature there is always a need to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=63&subd=kalpeshshirodker&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>For quite some time now i have been intending to write about Enum with Flags attribute set. Enum with Flags attribute set allows the enum to be used as Bit flags where each bit signifies whether a particular option is set or not. When using this very powerful feature there is always a need to check whether a particular bit is set or not and most of them time we end up duplicating the following piece of code. </p>
<p><strong><em>(enumvariable &amp; EnumType.enumproperty) == EnumType.enumproperty </em></strong></p>
<p>Needless to say it is a waste of time and clutters the code unnecessarily. So I had been trying to use Extension method, but stumbled upon some problem regarding casting of values. Then i stumbled upon this interesting article which does exactly what i want <a href="http://www.codeproject.com/Articles/37921/Enums-Flags-and-Csharp-Oh-my-bad-pun.aspx" target="_blank">Enums, Flags, and C# — Oh my! (bad pun…)</a>. </p>
<p>Neat really neat. Enjoy coding</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kalpeshshirodker.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kalpeshshirodker.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kalpeshshirodker.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kalpeshshirodker.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kalpeshshirodker.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kalpeshshirodker.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kalpeshshirodker.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kalpeshshirodker.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kalpeshshirodker.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kalpeshshirodker.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kalpeshshirodker.wordpress.com&blog=675108&post=63&subd=kalpeshshirodker&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://kalpeshshirodker.wordpress.com/2009/09/12/enum-with-flags-attribute-and-extension-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bf0e9a8b13c5fa268cd3faa5d1d913ec?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kalpeshshirodker</media:title>
		</media:content>
	</item>
	</channel>
</rss>