<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Ben Dill's Blog</title><link>http://weblogs.asp.net/bdill/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>IE Image Bug = Website Performance Hit</title><link>http://weblogs.asp.net/bdill/archive/2009/01/25/ie-image-bug-website-performance-hit.aspx</link><pubDate>Mon, 26 Jan 2009 02:10:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6860412</guid><dc:creator>MuteThis</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bdill/rsscomments.aspx?PostID=6860412</wfw:commentRss><comments>http://weblogs.asp.net/bdill/archive/2009/01/25/ie-image-bug-website-performance-hit.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;While troubleshooting a site I'm developing, I noticed that a page was double posting.&amp;nbsp; After a little brute force diagnostics, (comment everything out, bring things in one-at-a-time), I was able to isolate the issue.&lt;/P&gt;
&lt;P mce_keep="true"&gt;My company has a couple of ImageButtons that are wired up as hover buttons.&amp;nbsp; We have a control that you set the on &amp;amp; off images on and it does the rest.&amp;nbsp; For some reason (Murphy's Law I suppose), we weren't sending out the ImageButton's ImageUrl as the off image.&amp;nbsp; We just let the startup javascript assign that client side.&amp;nbsp; This resulted in an input type=image form field with an empty src attribute being sent to the client.&amp;nbsp; That seems benign right?&amp;nbsp; Well it isn't...&lt;/P&gt;
&lt;P mce_keep="true"&gt;If you have on your page one image field with a valid&amp;nbsp;src attribute and another with no&amp;nbsp;src attribute, IE interepts the src of the missing attribute to be the url of the current page and then IE will proceed to request the current page again in hopes of displaying an image for the missing scr attribute.&amp;nbsp; Ouch right?&lt;/P&gt;
&lt;P mce_keep="true"&gt;This means that if you have any control, image button, asp:image, etc on you site that is sending out src="", which you might not, IE will perform a double post to your page.&amp;nbsp; Yes a double post and all form fields are sent, it is a full blown duplicate request.&amp;nbsp; This means performance * .5...&lt;/P&gt;
&lt;P mce_keep="true"&gt;This is easily tested in your ASP.NET application.&amp;nbsp; All you need is a page with the following markup and a wired up Page_Load with a breakpoint on string Foo = "Bar";.&amp;nbsp; Run the debugger an enjoy...&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;lt;img src="" /&amp;gt;&amp;lt;img src="valid.jpg" /&amp;gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;Disclamier:&amp;nbsp; I haven't tested to see if the non-blank src must be valid...&amp;nbsp; When I say IE I have only tested IE7 and it fails.&amp;nbsp; I did test Google Chrome and it did not create a duplicate request.&lt;/P&gt;
&lt;P mce_keep="true"&gt;I realize that this might not be a big deal because I'm the only person out there sending out empty src attributes, but I thought I'd share the find.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6860412" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bdill/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item><item><title>Who Owns Your Databases?</title><link>http://weblogs.asp.net/bdill/archive/2009/01/21/who-owns-your-databases.aspx</link><pubDate>Wed, 21 Jan 2009 19:36:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6851978</guid><dc:creator>MuteThis</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bdill/rsscomments.aspx?PostID=6851978</wfw:commentRss><comments>http://weblogs.asp.net/bdill/archive/2009/01/21/who-owns-your-databases.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;Right now we have owners set to whoever creates the databases for most databases, but&amp;nbsp;there are a few owned by sa.&amp;nbsp; I'm thinking how great it would be to consolidate this.&amp;nbsp; To this end, I've created a database owner domain account and added it as a SQL server login.&amp;nbsp; The login only has the public server role.&amp;nbsp; In a move common with things I typically do, going overboard, I have denied this accounts ability to connect to the server and disabled it.&lt;/P&gt;
&lt;P mce_keep="true"&gt;So far things seem to be going smoothly.&amp;nbsp; One thing to note is that if you are planning on using unsafe clr assemblies the owner will need to have unsafe permissions....&lt;/P&gt;
&lt;P mce_keep="true"&gt;This seems like a fairly secure database owner setup to me.&amp;nbsp; I mentioned it to someone and he asked if we just had a security problem...&amp;nbsp; No, that's the point, we don't want one.&amp;nbsp;&amp;nbsp;What do you think?&lt;/P&gt;
&lt;P mce_keep="true"&gt;If things work out, we'll be looking for a way to automatically set the owner.&amp;nbsp; I'm guessing it'll need to be a server level trigger, because I don't see anything in model that let's you assign a default owner...&lt;/P&gt;
&lt;P mce_keep="true"&gt;Feedback greatly appreciated.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6851978" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bdill/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://weblogs.asp.net/bdill/archive/tags/Security/default.aspx">Security</category></item><item><title>Windows Server 2008 Typo???</title><link>http://weblogs.asp.net/bdill/archive/2009/01/06/windows-server-2008-typo.aspx</link><pubDate>Wed, 07 Jan 2009 03:19:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6823183</guid><dc:creator>MuteThis</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bdill/rsscomments.aspx?PostID=6823183</wfw:commentRss><comments>http://weblogs.asp.net/bdill/archive/2009/01/06/windows-server-2008-typo.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;I haven't posted in a long time and this isn't exaclty ground breaking content, but I saw this today and I can't help myself.&amp;nbsp; To me this looks like a typo in the help file name.&amp;nbsp; Perhaps I'm seeing something where there is nothing.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;IMG title="IE ESC Typo?" style="WIDTH: 514px; HEIGHT: 500px" height=500 alt="IE ESC Typo?" src="http://weblogs.asp.net/blogs/bdill/IEESCTypo.png" width=514 mce_src="http://weblogs.asp.net/blogs/bdill/IEESCTypo.png"&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6823183" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bdill/archive/tags/Community+News/default.aspx">Community News</category></item><item><title>Integer Based Bit Manipulation - C#</title><link>http://weblogs.asp.net/bdill/archive/2008/01/06/integer-based-bit-manipulation-c.aspx</link><pubDate>Sun, 06 Jan 2008 06:33:02 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5556891</guid><dc:creator>MuteThis</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bdill/rsscomments.aspx?PostID=5556891</wfw:commentRss><comments>http://weblogs.asp.net/bdill/archive/2008/01/06/integer-based-bit-manipulation-c.aspx#comments</comments><description>&lt;p&gt;As a follow up to my post from &lt;a href="http://weblogs.asp.net/bdill/archive/2008/01/05/integer-based-bit-manipulation-sql.aspx"&gt;yesterday&lt;/a&gt;, here is the C# extension version of the integer bit manipulation.&amp;#160; In order to facilitate the code I post, I have created a &lt;a href="http://www.codeplex.com"&gt;Codeplex&lt;/a&gt; project.&amp;#160; The project name is &lt;a href="http://www.codeplex.com/Pickle"&gt;Pickle&lt;/a&gt;, from my last name.&amp;#160; It's going to be a kitchen sink project with all sorts of utilities in it.&lt;/p&gt;  &lt;p&gt;As it stands now, I have one solution with five projects in it.&amp;#160; I have a Core project where I'm keeping my extension methods and various utilities, a database project where I'm keeping things like &lt;a href="http://weblogs.asp.net/bdill/archive/2007/09/29/sql-server-print-max.aspx"&gt;PrintMax&lt;/a&gt; and the &lt;a href="http://weblogs.asp.net/bdill/archive/2008/01/05/integer-based-bit-manipulation-sql.aspx"&gt;SQL bit manipulation&lt;/a&gt;, a SQLCLR project where I've got my &lt;a href="http://weblogs.asp.net/bdill/archive/2007/10/25/sql-xml-unicode-issue.aspx"&gt;double byte encoding function&lt;/a&gt;, since I'm trying to get into TDD I also have a Unit Test project and finally I have a web project where I'm splashing around with various things.&amp;#160; That was a run-on sentence to end all run-on sentences.&amp;#160; Perhaps codeplex is overkill, but it just seemed to make sense.&amp;#160; Instead of posting code, from now on I'll post screenshots and links to the source code on codeplex.&lt;/p&gt;  &lt;h4&gt;Extension Methods&lt;/h4&gt;  &lt;p&gt;As promised, I have created C# extension methods for the bit manipulation.&amp;#160; The can be used to test integer values throughout your project.&amp;#160; I have created the same set of functions (IsBit, ReadBit, SetBit, IsBitUnion).&lt;/p&gt;  &lt;p&gt;Here is a look at the SetBit extension method:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bdill/WindowsLiveWriter/IntegerBasedBitManipulationC_B8F/CSharpBitExtensionCode.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="453" alt="CSharpBitExtensionCode" src="http://weblogs.asp.net/blogs/bdill/WindowsLiveWriter/IntegerBasedBitManipulationC_B8F/CSharpBitExtensionCode_thumb.png" width="749" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And here is a look at the IsBitUnion usage:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bdill/WindowsLiveWriter/IntegerBasedBitManipulationC_B8F/CSharpBitExtensionUsage.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="228" alt="CSharpBitExtensionUsage" src="http://weblogs.asp.net/blogs/bdill/WindowsLiveWriter/IntegerBasedBitManipulationC_B8F/CSharpBitExtensionUsage_thumb.png" width="539" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;For these two calls IsBitUnion returned true then false as expected.&lt;/p&gt;  &lt;p&gt;Here is a link to the &lt;a href="http://www.codeplex.com/Pickle/SourceControl/FileView.aspx?itemId=21161&amp;amp;changeSetId=1127"&gt;source code file&lt;/a&gt; in my &lt;a href="http://www.codeplex.com/Pickle"&gt;codeplex&lt;/a&gt; project that contains the bit and other extension methods.&lt;/p&gt;  &lt;p&gt;By the way.&amp;#160; I'm not sure what version of TFS Codeplex is running, but I thought I saw that they were using 2005.&amp;#160; I'm running the VS.Net 2008 Team Suite with the TFS 2008 plug-in and connecting with no issues.&amp;#160; This is exciting for me, I wasn't sure if the VS.Net 2k8 worked with TFS 2k5.&amp;#160; Really, I'm still not sure, but I'm feeling better :D&lt;/p&gt;  &lt;p&gt;All feedback is encouraged.&lt;/p&gt;  &lt;p&gt;Thanks,    &lt;br /&gt;Ben&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5556891" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bdill/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bdill/archive/tags/Extension+Method/default.aspx">Extension Method</category></item><item><title>Integer Based Bit Manipulation - SQL</title><link>http://weblogs.asp.net/bdill/archive/2008/01/05/integer-based-bit-manipulation-sql.aspx</link><pubDate>Sat, 05 Jan 2008 10:28:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5551376</guid><dc:creator>MuteThis</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bdill/rsscomments.aspx?PostID=5551376</wfw:commentRss><comments>http://weblogs.asp.net/bdill/archive/2008/01/05/integer-based-bit-manipulation-sql.aspx#comments</comments><description>&lt;h4&gt;Introduction&lt;/h4&gt;  &lt;p&gt;Hopefully my topic isn't too far off.&amp;#160; In this post I'm going to cover what I like to think of as easy bit manipulation.&amp;#160; Well, it's easier anyway.&amp;#160; The basic concept is that an integer is comprised of bits.&amp;#160; Integers are easier to look at and play with than bits, especially if we're storing bit values in SQL and we want to update them...&amp;#160; Using logical bitwise operators, we can query one integer with another to see which bits they have in common.&amp;#160; When performing the query, if one value is strictly a bit, we can see if that bit is 'on' in the second value, which is a sum of one or more bits.&amp;#160; Alternatively, we can test two summed bits to see if one is a union of the other.&amp;#160; To me this means all of one exists completely in the other.&lt;/p&gt;  &lt;p&gt;I plan to post a C# extension method version of the SQL functions below in a day or two.&amp;#160; Since I'm not using VS.Net 2008 full time yet, I need to convert what I have to extension methods.&amp;#160; Well, I guess I don't &lt;u&gt;need&lt;/u&gt; to, but since I think extension methods are cool, I'm going to:)&lt;/p&gt;  &lt;h4&gt;Example&lt;/h4&gt;  &lt;p&gt;I'll start with a security example, but the context can be anything at all.&amp;#160; We need two values, one is the test bit and one is a sum of bits.&lt;/p&gt;  &lt;p&gt;Since we're doing permissions, our test bits might look like the following.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;CanView = 1 &lt;/li&gt;    &lt;li&gt;CanEdit = 2 &lt;/li&gt;    &lt;li&gt;CanSubmit = 4 &lt;/li&gt;    &lt;li&gt;CanApprove = 8 &lt;/li&gt;    &lt;li&gt;CanReject = 16 &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;What about that sum of bits?&amp;#160; Sure, lets say we have an administrator with all bits.&amp;#160; Their sum of bits would be 1 + 2 + 4 + 8 + 16 = 31.&amp;#160; Perhaps we have a contributor, they can only view edit and submit, they would have 1 + 2 + 4 = 7.&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2" width="269" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="91"&gt;&amp;#160;&lt;/td&gt;        &lt;td valign="top" width="76"&gt;Admin&lt;/td&gt;        &lt;td valign="top" width="100"&gt;Contributor&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="99"&gt;Integer&lt;/td&gt;        &lt;td valign="top" width="81"&gt;31&lt;/td&gt;        &lt;td valign="top" width="104"&gt;7&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;Bits&lt;/td&gt;        &lt;td valign="top" width="83"&gt;0001 1111&lt;/td&gt;        &lt;td valign="top" width="106"&gt;0000 0111&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Ok, that was easy, let's say that users with Admin permissions have a 31 and contributors have a 7.&amp;#160; Suddenly we have the need to perform a check, can the user Approve? Can the user Edit?&lt;/p&gt;  &lt;p&gt;Let's see, we need another table to consider this, let's examine the bits of CanApprove and CanEdit.&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2" width="269" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="91"&gt;&amp;#160;&lt;/td&gt;        &lt;td valign="top" width="76"&gt;CanApprove&lt;/td&gt;        &lt;td valign="top" width="100"&gt;CanEdit&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="99"&gt;Integer&lt;/td&gt;        &lt;td valign="top" width="81"&gt;8&lt;/td&gt;        &lt;td valign="top" width="104"&gt;2&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="100"&gt;Bits&lt;/td&gt;        &lt;td valign="top" width="83"&gt;0000 1000&lt;/td&gt;        &lt;td valign="top" width="106"&gt;0000 0010&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;So, can the Admin approve or edit?&amp;#160; How about the Contributor?&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2" width="442" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="91"&gt;&amp;#160;&lt;/td&gt;        &lt;td valign="top" colspan="8"&gt;         &lt;p align="center"&gt;Admin&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="13"&gt;&amp;#160;&lt;/td&gt;        &lt;td valign="top" colspan="8"&gt;         &lt;p align="center"&gt;Contributor&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="91"&gt;Summed Bits&lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" bgcolor="#008000"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" bgcolor="#008000"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" bgcolor="#ff0000"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" bgcolor="#008000"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="91"&gt;CanApprove&lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" bgcolor="#008000"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" bgcolor="#ff0000"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="91"&gt;CanEdit&lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" bgcolor="#008000"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" bgcolor="#008000"&gt;         &lt;p align="center"&gt;1&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;0&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;I hope this is legible and understandable so far.&amp;#160; This last table is trying to line up the summed bits of the admin and contributors juxtaposed with the bits of CanApprove and CanEdit to simplify comparison.&amp;#160; The green shows a match and the red shows a miss.&amp;#160; Above we can see that the integer value for admin has both the CanApprove and CanEdit bits.&amp;#160; At the same time, we see that the Contributor bits do not contain the CanApprove bit, but they do contain the CanEdit bits.&lt;/p&gt;  &lt;p&gt;Pretty exciting stuff isn't it :)&lt;/p&gt;  &lt;h4&gt;Uses&lt;/h4&gt;  &lt;p&gt;Hopefully at this point, I have successfully demonstrated how integers can be used to define flags and then store them in aggregate.&amp;#160; Where I work we primarily use them in two instances.&lt;/p&gt;  &lt;p&gt;First in granular page permissions such as the example above, allowing functionality based on permissions.&amp;#160; After authorizing the user on a page, we then enable UI components based on the bits of the component compared to the bits of the user.&lt;/p&gt;  &lt;p&gt;Next, we use this as a way to store multiple bit fields.&amp;#160; Some might warn about performance, no we haven't tested performance here, because we haven't seen an issue.&amp;#160; For example, let's say we send out a form with multiple checkboxes on it.&amp;#160; If we have eight bit fields on a form, instead of persisting to eight bit columns we persist to one integer called FormBits.&amp;#160; When we need to know if something was checked or not, we simply examine the integer for the bits we need.&lt;/p&gt;  &lt;h4&gt;Code&lt;/h4&gt;  &lt;p&gt;Enough talk, what about this SQL stuff?&amp;#160; Below are methods to test and read bit values SQL.&amp;#160; There are some comments in the code, but I'm not going to elaborate on the bitwise operators.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;SQL&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;CREATE FUNCTION [Utilities].[SetBit] &lt;/li&gt;    &lt;li&gt;( &lt;/li&gt;    &lt;li&gt;&amp;#160; -- the bits to check as an integer &lt;/li&gt;    &lt;li&gt;&amp;#160; @iBits INT &lt;/li&gt;    &lt;li&gt;&amp;#160; -- this bit to test &lt;/li&gt;    &lt;li&gt;&amp;#160; -- this must be a bit value &lt;/li&gt;    &lt;li&gt;&amp;#160; -- e.g. 1, 2, 4, 8, 16, 32, 64, etc &lt;/li&gt;    &lt;li&gt;&amp;#160; -- non bit values will produce unknown results &lt;/li&gt;    &lt;li&gt;&amp;#160; -- e.g. 3, 5, 6, 7, 9, 10, 11, etc &lt;/li&gt;    &lt;li&gt;&amp;#160; ,@iTestBit INT &lt;/li&gt;    &lt;li&gt;&amp;#160; -- should the bit be on or off &lt;/li&gt;    &lt;li&gt;&amp;#160; ,@iBitState BIT &lt;/li&gt;    &lt;li&gt;) &lt;/li&gt;    &lt;li&gt;RETURNS INT &lt;/li&gt;    &lt;li&gt;AS &lt;/li&gt;    &lt;li&gt;BEGIN &lt;/li&gt;    &lt;li&gt;&amp;#160; DECLARE @BitState BIT; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; -- we are validating that the testbit paramater is a bit&lt;/li&gt;    &lt;li&gt;&amp;#160; -- if it isn't, we return null&lt;/li&gt;    &lt;li&gt;&amp;#160; IF (Utilities.IsBit(@iTestBit) = 0)&lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; RETURN NULL; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; -- determine the current state of the test bit&lt;/li&gt;    &lt;li&gt;&amp;#160; SET @BitState = Utilities.ReadBit(@iBits, @iTestBit); &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; -- leave if the value is correct&lt;/li&gt;    &lt;li&gt;&amp;#160; IF (@BitState = @iBitState)&lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; RETURN @iBits; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; IF (@BitState = 0 AND @iBitState = 1)&lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; RETURN @iBits + @iTestBit; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; RETURN @iBits - @iTestBit;&lt;/li&gt;    &lt;li&gt;END; &lt;/li&gt;    &lt;li&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;CREATE FUNCTION [Utilities].[ReadBit] &lt;/li&gt;    &lt;li&gt;( &lt;/li&gt;    &lt;li&gt;&amp;#160; -- the bits to check as an integer &lt;/li&gt;    &lt;li&gt;&amp;#160; @iBits INT &lt;/li&gt;    &lt;li&gt;&amp;#160; -- this bit to test &lt;/li&gt;    &lt;li&gt;&amp;#160; -- this must be a bit value &lt;/li&gt;    &lt;li&gt;&amp;#160; -- e.g. 1, 2, 4, 8, 16, 32, 64, etc &lt;/li&gt;    &lt;li&gt;&amp;#160; -- non bit values will produce unknown results &lt;/li&gt;    &lt;li&gt;&amp;#160; -- e.g. 3, 5, 6, 7, 9, 10, 11, etc &lt;/li&gt;    &lt;li&gt;&amp;#160; ,@iTestBit INT &lt;/li&gt;    &lt;li&gt;) &lt;/li&gt;    &lt;li&gt;RETURNS BIT &lt;/li&gt;    &lt;li&gt;AS &lt;/li&gt;    &lt;li&gt;BEGIN &lt;/li&gt;    &lt;li&gt;&amp;#160; DECLARE @ComparisonResults INT; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; -- we are validating that the testbit paramater is a bit &lt;/li&gt;    &lt;li&gt;&amp;#160; -- if it isn't, we return null &lt;/li&gt;    &lt;li&gt;&amp;#160; IF (Utilities.IsBit(@iTestBit) = 0) BEGIN &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; RETURN NULL; &lt;/li&gt;    &lt;li&gt;&amp;#160; END; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; -- bitwise and the bits and the test bit &lt;/li&gt;    &lt;li&gt;&amp;#160; SET @ComparisonResults = @iBits &amp;amp; @iTestBit; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; -- if the result equals the test bit, the bit is present &lt;/li&gt;    &lt;li&gt;&amp;#160; IF (@ComparisonResults = @iTestBit) BEGIN &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; RETURN 1; &lt;/li&gt;    &lt;li&gt;&amp;#160; END; &lt;/li&gt;    &lt;li&gt;&amp;#160; ELSE BEGIN &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; RETURN 0; &lt;/li&gt;    &lt;li&gt;&amp;#160; END; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; -- this return is required by the function &lt;/li&gt;    &lt;li&gt;&amp;#160; -- it should never be hit &lt;/li&gt;    &lt;li&gt;&amp;#160; RETURN 0; &lt;/li&gt;    &lt;li&gt;END; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;CREATE FUNCTION [Utilities].[IsBit] &lt;/li&gt;    &lt;li&gt;( &lt;/li&gt;    &lt;li&gt;&amp;#160; @iTestBit INT &lt;/li&gt;    &lt;li&gt;) &lt;/li&gt;    &lt;li&gt;RETURNS BIT &lt;/li&gt;    &lt;li&gt;AS &lt;/li&gt;    &lt;li&gt;BEGIN &lt;/li&gt;    &lt;li&gt;&amp;#160; -- This function will take an integer and return true if it is a bit value, i.e. 1, 2, 4, 8 &lt;/li&gt;    &lt;li&gt;&amp;#160; DECLARE @Result BIT; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; IF @iTestBit = 1 &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; -- Special case of TestBit = 1 &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; SET @Result = 1; &lt;/li&gt;    &lt;li&gt;&amp;#160; ELSE IF ROUND(LOG(@iTestBit)/LOG(2),1) = LOG(@iTestBit)/LOG(2) &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; -- if the LOG Base 2 of the TestBit value is an INTEGER, you have found 2, 4, 8, 16 &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; SET @Result = 1; &lt;/li&gt;    &lt;li&gt;&amp;#160; ELSE &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; SET @Result = 0; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;&amp;#160; RETURN @Result; &lt;/li&gt;    &lt;li&gt;END; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;CREATE FUNCTION Utilities.IsBitUnion &lt;/li&gt;    &lt;li&gt;( &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; -- the bits to check as an integer &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; @iBits INT &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; -- this bits to test &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; ,@iTestBits INT &lt;/li&gt;    &lt;li&gt;) &lt;/li&gt;    &lt;li&gt;RETURNS BIT &lt;/li&gt;    &lt;li&gt;AS &lt;/li&gt;    &lt;li&gt;BEGIN &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; DECLARE @ComparisonResults INT; &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; -- bitwise and the bits and the test bits &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; SET @ComparisonResults = @iBits &amp;amp; @iTestBits; &lt;/li&gt;    &lt;li&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; -- if the input bits equals the test bits, we have a union &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; IF (@ComparisonResults = @iTestBits) BEGIN &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; RETURN 1; &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; END; &lt;/li&gt;    &lt;li&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;&amp;#160;&amp;#160;&amp;#160; RETURN 0; &lt;/li&gt;    &lt;li&gt;END; &lt;/li&gt;    &lt;li&gt;&lt;/li&gt;    &lt;li&gt;-- CanView = 1 &lt;/li&gt;    &lt;li&gt;-- CanEdit = 2 &lt;/li&gt;    &lt;li&gt;-- CanSubmit = 4 &lt;/li&gt;    &lt;li&gt;-- CanApprove = 8 &lt;/li&gt;    &lt;li&gt;-- CanReject = 16 &lt;/li&gt;    &lt;li&gt;-- Admin = 31 &lt;/li&gt;    &lt;li&gt;-- Contributor = 7 &lt;/li&gt;    &lt;li&gt;SELECT Utilities.ReadBit(31, 8) -- 1 : Admin has CanApprove &lt;/li&gt;    &lt;li&gt;SELECT Utilities.ReadBit(31, 2) -- 1 : Admin has CanEdit &lt;/li&gt;    &lt;li&gt;SELECT Utilities.IsBitUnion(31, 10) -- 1 : Admin has CanApprove &amp;amp; CanEdit &lt;/li&gt;    &lt;li&gt;SELECT Utilities.ReadBit(7, 8) -- 0 : Contributor does not have CanApprove &lt;/li&gt;    &lt;li&gt;SELECT Utilities.ReadBit(7, 2) -- 1 : Contributor has CanEdit &lt;/li&gt;    &lt;li&gt;SELECT Utilities.IsBitUnion(7, 10) -- 0 : Contributor does not have CanApprove &amp;amp; CanEdit &lt;/li&gt;    &lt;li&gt;SELECT Utilities.SetBit(7, 8, 1) -- 15 : Add CanApprove to Contributor &lt;/li&gt;    &lt;li&gt;SELECT Utilities.ReadBit(15, 8) -- 1 : Contributor has CanApprove &lt;/li&gt;    &lt;li&gt;SELECT Utilities.IsBitUnion(15, 10) -- 1 : Contributor has CanApprove &amp;amp; CanEdit &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The IsBit function can be used as a constraint on columns that hold the individual bits that make up permissions and to validate that the test bit input to the ReadBit function is a bit.&amp;#160; If a non-bit were passed to readbit, bad things happen.&lt;/p&gt;  &lt;p&gt;Thank you for reading my post, any and all feedback is encouraged!    &lt;br /&gt;Ben&lt;/p&gt;  &lt;p&gt;P.S. As a quick SQL rant/question, what is up with synonyms not being allowed for schema bound objects?&amp;#160; Where I work, we have a shared database that multiple project databases reference in one way or another.&amp;#160; The Utilities.IsBit function above is employed in databases to ensure that any configured bit values are bits.&amp;#160; Makes sense right?&amp;#160; Well, since we cannot use a schema bound object in a check constraint, we have to have multiple copies of the function.&amp;#160; This is also the case with other entities that we would otherwise use a synonym to reference.&amp;#160; Is it me, or is this an unnecessary inconvenience?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5551376" width="1" height="1"&gt;</description></item><item><title>C# String Replication Extension Method</title><link>http://weblogs.asp.net/bdill/archive/2007/12/06/c-string-replication-extension-method.aspx</link><pubDate>Fri, 07 Dec 2007 04:45:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5415743</guid><dc:creator>MuteThis</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bdill/rsscomments.aspx?PostID=5415743</wfw:commentRss><comments>http://weblogs.asp.net/bdill/archive/2007/12/06/c-string-replication-extension-method.aspx#comments</comments><description>&lt;DIV class=csharpcode&gt;I finally got around to installing Visual Studio 2008 tonight and wanted to write a quick piece of code using a new feature.&amp;nbsp; The first thing that came to mind was a string replication extension method.&amp;nbsp; This was inspired by something I just wrote in 2.0 that at the time made me think "this would be a good extension method."&amp;nbsp; I realize the code I have isn't breaking any ground, but I thought I'd share it anyway.&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;Right now, it seems like the usefulness of extension methods and their pitfalls is being hashed out.&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;A couple examples:&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;&lt;A title=http://codebetter.com/blogs/gregyoung/archive/2007/12/05/a-use-for-extension-methods.aspx href="http://codebetter.com/blogs/gregyoung/archive/2007/12/05/a-use-for-extension-methods.aspx" mce_href="http://codebetter.com/blogs/gregyoung/archive/2007/12/05/a-use-for-extension-methods.aspx"&gt;http://codebetter.com/blogs/gregyoung/archive/2007/12/05/a-use-for-extension-methods.aspx&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;&lt;A title=http://www.interact-sw.co.uk/iangblog/2005/09/26/extensionmethods href="http://www.interact-sw.co.uk/iangblog/2005/09/26/extensionmethods" mce_href="http://www.interact-sw.co.uk/iangblog/2005/09/26/extensionmethods"&gt;http://www.interact-sw.co.uk/iangblog/2005/09/26/extensionmethods&lt;/A&gt;&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;Perhaps I'm caught up in the shiny new language feature and I'm not seeing the downside.&amp;nbsp; I do know this, having to remember things like "where is that SQL string cleaning method?", "don't we have something that strips HTML out of a string?", "where is that email address validator?", etc can be a pain and cause unnecessary duplication of effort.&amp;nbsp; I love the thought of being to bolt my string methods to the string object.&amp;nbsp; In doing so, I'm centralizing my string manipulation methods.&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;Something to note here is the notion of extension methods being implemented in their own namespace.&amp;nbsp; It could start getting ugly if extension methods showed up without invitation.&amp;nbsp; For example, if someone references your assembly and then adds a using statement, they might not be expecting to pick up all of your extension methods from that assembly.&amp;nbsp; Placing extension methods into their own namespace would help prevent this from happening.&lt;/DIV&gt;
&lt;H3 class=csharpcode&gt;The Task &amp;amp; Code&lt;/H3&gt;
&lt;DIV class=csharpcode&gt;Create a method that will take a source string and an input string, and append/prepend the input string a specified number of times to the source string.&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;&amp;nbsp;&lt;/DIV&gt;The logic of the replication method is straight forward, no need to dig into that.&amp;nbsp; The reason of the post is the extension method, so a quick review of what it takes to make one. 
&lt;DIV class=csharpcode&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;DIV class=csharpcode&gt;A Static Class&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class=csharpcode&gt;A Static Method&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class=csharpcode&gt;The first parameter of the method having the this keyword, indicating which type the method will be bound to.&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;/DIV&gt;
&lt;P class=csharpcode&gt;From here, the rest is academic...&lt;/P&gt;
&lt;P class=csharpcode&gt;Usage of the code below might look like this:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; Foo = &lt;SPAN class=str&gt;"Foo"&lt;/SPAN&gt;.Replicate(&lt;SPAN class=str&gt;"Bar"&lt;/SPAN&gt;, 5, ReplicationMode.Append);&lt;/PRE&gt;
&lt;DIV class=csharpcode&gt;Not exactly the most exciting example. At some point I would like to modify this to act like String.PadLeft or String.PadRight, but since it is getting late, I'll go with what I have for now...&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;Enjoy :)&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=csharpcode&gt;
&lt;OL&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #0600ff"&gt;using&lt;/SPAN&gt; &lt;SPAN style="COLOR: #000000"&gt;System&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #0600ff"&gt;using&lt;/SPAN&gt; &lt;SPAN style="COLOR: #000000"&gt;System&lt;/SPAN&gt;.&lt;SPAN style="COLOR: #0000ff"&gt;Text&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #0600ff"&gt;namespace&lt;/SPAN&gt; Core&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #000000"&gt;{&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// Where should replicated input&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;enum&lt;/SPAN&gt; ReplicationMode&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #000000"&gt;{&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// Replicated input is added behind the source&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; Append,&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// Replicated input is added in front of the source&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; Prepend&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #000000"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// Extension methods&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0600ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;class&lt;/SPAN&gt; Extensions&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #000000"&gt;{&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// Replicates the input string the specified number of times in front of or behind the source string&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;param name="source"&amp;gt;The string that the replicated input is being added to.&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;param name="input"&amp;gt;The string to replicate.&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;param name="count"&amp;gt;The number of times to replicate the input.&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;param name="replicationMode"&amp;gt;The positioning of the replicated data.&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;/// &amp;lt;returns&amp;gt;The source string if the input is null, empty or the count is less than one.&amp;nbsp; Otherwise the processed result of replication.&amp;lt;/returns&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0600ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;string&lt;/SPAN&gt; Replicate&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0600ff"&gt;this&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;string&lt;/SPAN&gt; source, &lt;SPAN style="COLOR: #ff0000"&gt;string&lt;/SPAN&gt; input, &lt;SPAN style="COLOR: #ff0000"&gt;int&lt;/SPAN&gt; count, ReplicationMode replicationMode&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #000000"&gt;{&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;// validate input&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;if&lt;/SPAN&gt; &lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;string&lt;/SPAN&gt;.&lt;SPAN style="COLOR: #0000ff"&gt;IsNullOrEmpty&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;input&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt; == &lt;SPAN style="COLOR: #0600ff"&gt;true&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0600ff"&gt;return&lt;/SPAN&gt; source;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;if&lt;/SPAN&gt; &lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;count &amp;lt; &lt;SPAN style="COLOR: #ff0000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0600ff"&gt;return&lt;/SPAN&gt; source;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;// Prime the result&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; StringBuilder Result = &lt;A href="http://www.google.com/search?q=new+msdn.microsoft.com"&gt;&lt;SPAN style="COLOR: #008000"&gt;new&lt;/SPAN&gt;&lt;/A&gt; StringBuilder&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;source.&lt;SPAN style="COLOR: #0000ff"&gt;Length&lt;/SPAN&gt; + &lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;input.&lt;SPAN style="COLOR: #0000ff"&gt;Length&lt;/SPAN&gt; * count&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Result.&lt;SPAN style="COLOR: #0000ff"&gt;Append&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;source&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;for&lt;/SPAN&gt; &lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;int&lt;/SPAN&gt; i = &lt;SPAN style="COLOR: #ff0000"&gt;0&lt;/SPAN&gt;; i &amp;lt; count; i++&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #000000"&gt;{&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;if&lt;/SPAN&gt; &lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;replicationMode == ReplicationMode.&lt;SPAN style="COLOR: #0000ff"&gt;Append&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Result.&lt;SPAN style="COLOR: #0000ff"&gt;Append&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;input&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;else&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Result.&lt;SPAN style="COLOR: #0000ff"&gt;Insert&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;0&lt;/SPAN&gt;, input&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #000000"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;return&lt;/SPAN&gt; Result.&lt;SPAN style="COLOR: #0000ff"&gt;ToString&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: #000000"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #000000"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #000000"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;/DIV&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5415743" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bdill/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bdill/archive/tags/Extension+Method/default.aspx">Extension Method</category></item><item><title>SQL + XML(?) + Unicode = Issue</title><link>http://weblogs.asp.net/bdill/archive/2007/10/25/sql-xml-unicode-issue.aspx</link><pubDate>Fri, 26 Oct 2007 02:09:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4755622</guid><dc:creator>MuteThis</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bdill/rsscomments.aspx?PostID=4755622</wfw:commentRss><comments>http://weblogs.asp.net/bdill/archive/2007/10/25/sql-xml-unicode-issue.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;The title of this post may not be entirely accurate, but it is pretty close.&amp;nbsp; I recently encountered an issue with a 3rd party component that was reading unicode information from a MSSQL 2005 database and then sending an email using the value.&amp;nbsp; For some reason, what appeared to be random unicode characters from SQL were displayed as the flattened '?'.&amp;nbsp; The odd thing was the they were right next to other unicode values that came through just fine.&amp;nbsp; Very odd...&lt;/P&gt;
&lt;P mce_keep="true"&gt;After profiling SQL, the request to, and the response from SQL looked like everything was fine, but this blackbox process was garbling data nonetheless.&amp;nbsp; Since the final destination for the information was an HTML formatted message, I took a shot in the dark, and tried encoding the unicode value.&amp;nbsp; This worked like a champ.&amp;nbsp; I could put &amp;amp;#35336; into the column and it would read out and display without a hitch.&lt;/P&gt;
&lt;P mce_keep="true"&gt;The solution is now clear, I need the ability to return a unicode field encoded for HTML.&amp;nbsp; Hello SQL CLR :-)&amp;nbsp; Wait, a moment, what about this whole XML thing?&amp;nbsp; Well, after I found the solution, I tried to find the problem.&amp;nbsp; The thrid party control is totally blackbox, so I have no way of knowing what they're doing, but I did stumble into this little gem in &lt;A class="" title=http://msdn2.microsoft.com/en-us/library/ms173781.aspx href="http://weblogs.asp.net/controlpanel/blogs/BOL" target=_blank mce_href="http://weblogs.asp.net/controlpanel/blogs/BOL"&gt;BOL&lt;/A&gt;:&lt;/P&gt;
&lt;P mce_keep="true"&gt;Caution:&lt;BR&gt;EVENTDATA returns XML data. This data is sent to the client as Unicode that uses 2 bytes for each character. The following Unicode code points can be represented in the XML that is returned by EVENTDATA:&lt;/P&gt;
&lt;P mce_keep="true"&gt;0x0009, 0x000A, 0x000D, &amp;gt;= 0x0020 &amp;amp;&amp;amp; &amp;lt;= 0xD7FF, &amp;gt;= 0xE000 &amp;amp;&amp;amp; &amp;lt;= 0xFFFD &lt;/P&gt;
&lt;P mce_keep="true"&gt;Some characters that can appear in Transact-SQL identifiers and data are not expressible or permissible in XML. Characters or data that have code points not shown in the previous list are mapped to a question mark (?).&lt;/P&gt;
&lt;P mce_keep="true"&gt;This somehow makes me think that the 3rd party component is serializing the data from SQL and the "random" unicode characters that are being flattened are actually outside XML's rendering capabilities...&amp;nbsp; This probably isn't the case, but it is the most plausable thing I found and with our current production schedule, it was a fix and move on.&lt;/P&gt;
&lt;P mce_keep="true"&gt;On to the code.&amp;nbsp; The following is a SQL CLR procedure that I created using an encoding function I found on the net and tweaked a little.&amp;nbsp; My apologies to the author for not being able to reference you at posting time.&lt;/P&gt;&lt;SPAN style="COLOR: #000000"&gt;&lt;SPAN style="COLOR: #000000"&gt;
&lt;OL&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #000000"&gt;[&lt;/SPAN&gt;Microsoft.&lt;SPAN style="COLOR: #0000ff"&gt;SqlServer&lt;/SPAN&gt;.&lt;SPAN style="COLOR: #0000ff"&gt;Server&lt;/SPAN&gt;.&lt;SPAN style="COLOR: #0000ff"&gt;SqlFunction&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;]&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #0600ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0600ff"&gt;static&lt;/SPAN&gt; SqlString DoubleByteToHtmlEncoding&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;SqlString input&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #000000"&gt;{&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;// check for null&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;if&lt;/SPAN&gt; &lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;input.&lt;SPAN style="COLOR: #0000ff"&gt;IsNull&lt;/SPAN&gt; == &lt;SPAN style="COLOR: #0600ff"&gt;true&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0600ff"&gt;return&lt;/SPAN&gt; input;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;// convert the sql string to a .net string&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #ff0000"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;[&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;]&lt;/SPAN&gt; chars = input.&lt;SPAN style="COLOR: #0000ff"&gt;ToString&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;.&lt;SPAN style="COLOR: #0000ff"&gt;ToCharArray&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;// create a string builder to hold the converted output, slightly larger than&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;// the source string&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; StringBuilder Result = &lt;SPAN style="COLOR: #008000"&gt;new&lt;/SPAN&gt; StringBuilder&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;chars.&lt;SPAN style="COLOR: #0000ff"&gt;Length&lt;/SPAN&gt; + &lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;chars.&lt;SPAN style="COLOR: #0000ff"&gt;Length&lt;/SPAN&gt; * &lt;SPAN style="COLOR: #ff0000"&gt;0.1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;foreach&lt;/SPAN&gt; &lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;char&lt;/SPAN&gt; c &lt;SPAN style="COLOR: #0600ff"&gt;in&lt;/SPAN&gt; chars&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #000000"&gt;{&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #ff0000"&gt;int&lt;/SPAN&gt; Value = Convert.&lt;SPAN style="COLOR: #0000ff"&gt;ToInt32&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;c&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;// if the value is larger than an ascii value, escape it&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;if&lt;/SPAN&gt; &lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;Value &amp;gt; &lt;SPAN style="COLOR: #ff0000"&gt;127&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Result.&lt;SPAN style="COLOR: #0000ff"&gt;AppendFormat&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;"&amp;amp;#{0};"&lt;/SPAN&gt;, Value&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;// otherwise add it as is&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;else&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Result.&lt;SPAN style="COLOR: #0000ff"&gt;Append&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;c&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #000000"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #008080; FONT-STYLE: italic"&gt;// return the processed result to sql&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&amp;nbsp; &lt;SPAN style="COLOR: #0600ff"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #008000"&gt;new&lt;/SPAN&gt; SqlString&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;Result.&lt;SPAN style="COLOR: #0000ff"&gt;ToString&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;)&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;LI style="FONT-WEIGHT: normal; COLOR: black; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;
&lt;DIV style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #000000"&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #000000"&gt;I know everyone loves that name, I have one small concern and it is simply that someone might think it will html encode all data, where it only encodes double byte values.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #000000"&gt;Now that I have this function, I just tell the 3rd party control that instead of reading from a table, read from a view, where the view implements the function on the necessary columns, leaving the source data untouched!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"&gt;&lt;SPAN style="COLOR: #000000"&gt;Consuming the function can be as easy as:&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN style="COLOR: #000000"&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;SELECT&lt;/FONT&gt;&lt;FONT size=2&gt; dbo&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;DoubleByteToHtmlEncoding&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;N&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'計'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;);&lt;BR&gt;or&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;&lt;FONT color=#0000ff size=2&gt;SELECT&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; dbo&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt;DoubleByteToHtmlEncoding&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt;t&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt;Col&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;FROM&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; TableName &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;AS&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT color=#000000&gt; t;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#808080 size=2&gt;&lt;FONT size=2&gt;&lt;FONT color=#000000&gt;Implementation of the clr function is outside the scope of this article, but here are a couple links that might get you started if you're interested:&lt;BR&gt;&lt;A class="" title="Using CLR Integration in SQL Server 2005" href="http://msdn2.microsoft.com/en-us/library/ms345136.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms345136.aspx"&gt;&lt;FONT color=#000000&gt;Using CLR Integration in SQL Server 2005&lt;/FONT&gt;&lt;/A&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;&lt;FONT size=2&gt;&lt;FONT color=#000000&gt;&lt;SPAN class=linkTerms&gt;&lt;BR&gt;&lt;A onclick="javascript:Track('ctl00_LibFrame_ctl06|ctl00_LibFrame_ctl21',this);" href="http://msdn2.microsoft.com/en-us/library/w2kae45k(VS.80).aspx"&gt;How to: Create and Run a CLR SQL Server User-Defined Function&lt;/A&gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4755622" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bdill/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://weblogs.asp.net/bdill/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bdill/archive/tags/SQL+CLR/default.aspx">SQL CLR</category></item><item><title>SQL Server - Print MAX</title><link>http://weblogs.asp.net/bdill/archive/2007/09/29/sql-server-print-max.aspx</link><pubDate>Sun, 30 Sep 2007 01:38:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4252014</guid><dc:creator>MuteThis</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bdill/rsscomments.aspx?PostID=4252014</wfw:commentRss><comments>http://weblogs.asp.net/bdill/archive/2007/09/29/sql-server-print-max.aspx#comments</comments><description>&lt;P&gt;Welcome to my blog, this is post numero uno.&amp;nbsp; I would like to thank &lt;A class="" title="Joe Stagner" href="http://joeon.net/" mce_href="http://joeon.net/"&gt;Joe Stagner&lt;/A&gt; for hooking me up with a blog on weblogs.asp.net.&amp;nbsp; My name is Bennett Dill and I am an Application Architect at a market research firm in Cincinnati Ohio.&amp;nbsp; I have been programming for about ten years starting with ASP Classic and VB 6 right on up to C# and ASP.NET.&amp;nbsp; I don't know if I'm going to be a good blogger, but I'll give it a whirl.&lt;/P&gt;
&lt;P&gt;Ok, so what about SQL Server &amp;amp; Print MAX?&amp;nbsp; From time-to-time, I deal with dynamic SQL.&amp;nbsp;&amp;nbsp;&amp;nbsp; Some of the dynamic queries get large, so &lt;A class="" title=NVARCHAR(MAX) href="http://msdn2.microsoft.com/en-us/library/ms186939.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms186939.aspx"&gt;NVARCHAR(MAX)&lt;/A&gt; was a welcome addition in MS SQL 2005.&amp;nbsp; Recently I encountered an error with a dynamic query, normally this isn’t a problem, print the SQL displaying the interpolated values and fix the typo.&amp;nbsp; Herein lies the problem, this SQL actually surpassed the NVARCHAR(4000), cool, the first SQL exceeding the old limit.&amp;nbsp; Why is that a problem?&amp;nbsp; Well, it seems as though the &lt;A class="" title=PRINT href="http://msdn2.microsoft.com/en-us/library/ms176047.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms176047.aspx"&gt;PRINT&lt;/A&gt; statement is still limited to NVARCHAR(4000)&amp;nbsp; or VARCHAR(8000).&lt;/P&gt;
&lt;P&gt;I needed a fix for this and the fix needed to act just like the built-in PRINT function.&amp;nbsp; Breaking every 4000 characters and printing wasn’t an option because it would insert line breaks at unknown locations because each PRINT statement adds a line break.&amp;nbsp; While there may be an option to PRINT without a line break, I don’t know it, and will need to quickly update this post when someone points it out ;-).&lt;/P&gt;
&lt;P&gt;Until then the fix is as follows, for a given string:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Walk it in 4000 character chunks&lt;/LI&gt;
&lt;LI&gt;Find the last line break&lt;/LI&gt;
&lt;LI&gt;Note it’s position&lt;/LI&gt;
&lt;LI&gt;Print up to it&lt;/LI&gt;
&lt;LI&gt;Skip it&lt;/LI&gt;
&lt;LI&gt;Go back to step 1&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Here is the SQL used to accomplish this (I apologize for not having a [code] style yet):&lt;/P&gt;
&lt;P&gt;CREATE PROCEDURE [dbo].[PrintMax]&lt;BR&gt;(&lt;BR&gt;&amp;nbsp;@iInput NVARCHAR(MAX)&lt;BR&gt;)&lt;BR&gt;AS&lt;BR&gt;BEGIN&lt;BR&gt;&amp;nbsp;-- nothing we can do with null data&lt;BR&gt;&amp;nbsp;IF (@iInput IS NULL) BEGIN&lt;BR&gt;&amp;nbsp;&amp;nbsp;RETURN;&lt;BR&gt;&amp;nbsp;END;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;-- This procedure was created to properly print&lt;BR&gt;&amp;nbsp;-- nvarchar(max) since the print statement can&lt;BR&gt;&amp;nbsp;-- only handle NVARCHAR(4000), we break the&lt;BR&gt;&amp;nbsp;-- input down into 4000 byte blocks and print&lt;BR&gt;&amp;nbsp;-- upto the last linebreak before the 4000 byte cutoff&lt;BR&gt;&amp;nbsp;DECLARE @ReversedData NVARCHAR(MAX)&lt;BR&gt;&amp;nbsp;&amp;nbsp;,@LineBreakIndex INT&lt;BR&gt;&amp;nbsp;&amp;nbsp;,@SearchLength INT;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;-- if the search length is less than the first occurance&lt;BR&gt;&amp;nbsp;-- of a line break, the data will be printed with a line break&lt;BR&gt;&amp;nbsp;-- at the SearchLength position even though there should not be&lt;BR&gt;&amp;nbsp;-- a break in the data there.&lt;BR&gt;&amp;nbsp;SET @SearchLength = 4000;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;-- only loop while the input is greater than the search length&lt;BR&gt;&amp;nbsp;WHILE (LEN(@iInput) &amp;gt; @SearchLength) BEGIN&lt;BR&gt;&amp;nbsp;&amp;nbsp;-- obtain and reverse the input upto the search length&lt;BR&gt;&amp;nbsp;&amp;nbsp;SET @ReversedData = LEFT(@iInput, @SearchLength);&lt;BR&gt;&amp;nbsp;&amp;nbsp;SET @ReversedData = REVERSE(@ReversedData);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;-- determine the position of the first line break for this piece of data&lt;BR&gt;&amp;nbsp;&amp;nbsp;SET @LineBreakIndex = CHARINDEX(CHAR(10) + CHAR(13), @ReversedData);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;-- print the input only showing data upto the line break&lt;BR&gt;&amp;nbsp;&amp;nbsp;-- the original linebreak will not be displayed&lt;BR&gt;&amp;nbsp;&amp;nbsp;PRINT LEFT(@iInput, @SearchLength - @LineBreakIndex + 1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;-- resize the input removing the data that was displayed and the line break.&lt;BR&gt;&amp;nbsp;&amp;nbsp;SET @iInput = RIGHT(@iInput, LEN(@iInput) - @SearchLength + @LineBreakIndex - 1);&lt;BR&gt;&amp;nbsp;END;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;-- if there is any data remaining, print it&lt;BR&gt;&amp;nbsp;IF (LEN(@iInput) &amp;gt; 0) BEGIN&lt;BR&gt;&amp;nbsp;&amp;nbsp;PRINT @iInput;&lt;BR&gt;&amp;nbsp;END;&lt;BR&gt;END;&lt;/P&gt;
&lt;P&gt;All feedback is welcome, thanks!&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4252014" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bdill/archive/tags/SQL+Server/default.aspx">SQL Server</category></item></channel></rss>