<?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/"
	>

<channel>
	<title>richard-slater.co.uk &#187; C#</title>
	<atom:link href="http://www.richard-slater.co.uk/archives/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richard-slater.co.uk</link>
	<description>Jesus, Life, Programming and Systems Administration</description>
	<lastBuildDate>Fri, 16 Jul 2010 21:04:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>ASP.net 3.5 GridView RowCommand event fired twice</title>
		<link>http://www.richard-slater.co.uk/archives/2010/04/01/asp-net-3-5-gridview-rowcommand-event-fired-twice/</link>
		<comments>http://www.richard-slater.co.uk/archives/2010/04/01/asp-net-3-5-gridview-rowcommand-event-fired-twice/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 15:25:27 +0000</pubDate>
		<dc:creator>Richard Slater</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sys. Admin.]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.richard-slater.co.uk/?p=763</guid>
		<description><![CDATA[I am writing this up to hopefully save someone else time in the future, this particular problem took up six hours of my day yesterday causing quite a bit of frustration for me, as the developer, and the users of the application. If you are searching for the solution scroll down to the bottom of [...]]]></description>
			<content:encoded><![CDATA[<p>I am writing this up to hopefully save someone else time in the future, this particular problem took up six hours of my day yesterday causing quite a bit of frustration for me, as the developer, and the users of the application.</p>
<p>If you are searching for the solution scroll down to the bottom of the page where I will outline the solution I used to resolve the problem. It is also worth pointing out that this does appear to be fixed in .NET 4. Certainly I was able to consistently reproduce the problem with VS2008/.NET 3.5 on multiple different computers. However after converting the project to VS2010/.NET 5 I haven&#8217;t seen the issue.</p>
<h1>Explanation of the problem</h1>
<p>I wrote and maintain an application that publishes a list of courses and allows users to book onto these courses, what I have listed below is a simplified version of this application.</p>
<p>The administration console contains two lists:</p>
<ul>
<li><strong>Published Courses</strong> &#8211; courses visible to all employees.</li>
<li><strong>Unpublished Courses </strong>- courses waiting to be published, only visible from the administration console.</li>
</ul>
<p>Courses can be freely published (i.e. moved from Unpublished to Published) by clicking green tick. Courses that have not had any bookings made can be unpublished by clicking the red cross.</p>
<p>The cross and the tick are implemented as <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx">GridView</a> <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.aspx">ButtonFields</a>:</p>
<pre>
</pre>
<pre><code>&lt;asp:ButtonField ButtonType="Image" CommandName="UnpublishCourse"
</code><code>    ImageUrl="~/images/unpublish.png" InsertVisible="False" Text="Unpublish" /&gt;
</code></pre>
<p><a href="http://www.richard-slater.co.uk/wp-content/uploads/2010/04/CourseBookings1.png"><img class="aligncenter size-medium wp-image-765" title="CourseBookings" src="http://www.richard-slater.co.uk/wp-content/uploads/2010/04/CourseBookings1-300x114.png" alt="" width="300" height="114" /></a></p>
<p>This application has been running for six months, the issue had not been observed up until yesterday. The user explained to me that when they were publishing courses they were always published in pairs, equally when unpublishing courses it was being done in pairs, concealingly unpublishing a course with bookings.</p>
<h1>Investigating the problem</h1>
<p>Initially I tried to reproduce this on my local machine, backed up and subsequently restored the database locally made sure I was running the same revision as the server and fired it up. Couldn&#8217;t reproduce the problem, no matter how fast I clicked it wouldn&#8217;t happen. Tried various permutations of code and database but could only reproduce on the server.</p>
<p>Refreshed the binaries on the server with the HEAD from subversion, problem was still happening most of the time. I confirmed that it wasn&#8217;t an issue with the stored procedures by running them manually through LinqPad.</p>
<p>I started putting debug statements at the entry points to the critical parts of the code, this yielded an interesting output on my development machine, each time the cross or the tick was clicked UnpublishedGridView_RowCommand was fired twice. This gave me something to search for, seems I am not the only one to have this problem, <a href="https://connect.microsoft.com/VisualStudio/feedback/details/102115/gridview-rowcommand-event-firing-twice">Microsoft tried to reproduce it in 2006</a> but couldn&#8217;t.</p>
<h1>Solving the problem</h1>
<p>As it turns out there are several ways of fixing the problem, several people have used timers to <a href="http://www.labbookpages.co.uk/electronics/debounce.html">&#8220;debounce&#8221;</a> the RowCommand event, assuming that the event is always going to be fired twice a session variable can be used to filter out the second event.</p>
<p>Because the event is only fired twice when ButtonType=&#8221;Image&#8221; not when ButtonType=&#8221;Link&#8221; you can set the text property to the HTML to render your image. This resulted in the code above becomming:</p>
<pre>
</pre>
<pre><code>&lt;asp:ButtonField ButtonType="Link" CommandName="UnpublishCourse"
</code><code>    InsertVisible="False" Text="&lt;img src=images/unpublish.png /&gt;" /&gt;
</code></pre>
<p>This proved to be the simplest possible solution, Visual Studio 2008 throws a warning about ASP.net validation, but I can live with that as long as the application works. In addition to the simplicity of the solution it also continues to work in ASP.net 4 (which doesn&#8217;t exhibit the double event behaviour).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richard-slater.co.uk/archives/2010/04/01/asp-net-3-5-gridview-rowcommand-event-fired-twice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lack of AutoEllipsis support in ToolStripSystemRenderer</title>
		<link>http://www.richard-slater.co.uk/archives/2010/03/07/lack-of-autoellipsis-support-in-toolstripsystemrenderer/</link>
		<comments>http://www.richard-slater.co.uk/archives/2010/03/07/lack-of-autoellipsis-support-in-toolstripsystemrenderer/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 21:03:04 +0000</pubDate>
		<dc:creator>Richard Slater</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[EVEMon]]></category>
		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://www.richard-slater.co.uk/?p=753</guid>
		<description><![CDATA[AutoEllipsis is a property introduced to System.Windows.Forms.Label with .NET 3.0, which in the event of the text overflowing the rendering rectangle of the Label will trim the end and add a Ellipsis (&#8220;…&#8221;), if this does occur the ToolTip for the label will also be set to the full (untrimmed text). Unfortunately this functionality is [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.label.autoellipsis%28VS.85%29.aspx">AutoEllipsis</a> is a property introduced to System.Windows.Forms.Label with .NET 3.0, which in the event of the text overflowing the rendering rectangle of the Label will trim the end and add a Ellipsis (&#8220;…&#8221;), if this does occur the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx">ToolTip</a> for the label will also be set to the full (untrimmed text).</p>
<p>Unfortunately this functionality is not available for <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripstatuslabel.aspx">ToolStripStatusLabel</a>. To make things worse in the event the text overflows it disappears completely. This bug, oversight, feature or whatever you want to call it cause some confusion after the release of EVEMon 1.3.0.1912. Several people assumed the new more verbose status bar was broken, being empty and all.</p>
<p>We put together a kludge fix, which would set the text and if it overflowed try to guess the length with <a href="http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx">Graphics.MeasureString</a>. This worked fairly well, it cause some flickering when resizing the window and would leave a small gap on the right hand side of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.statusstrip.aspx">StatusStrip</a>.</p>
<p>I knew there must be a better way, and seeing an article about the <a href="http://msdn.microsoft.com/en-us/library/system.drawing.stringformat.aspx">StringFormat</a> class reminded me of the need to find it. Searching about a bit found me a post on <a href="http://discuss.joelonsoftware.com/default.asp?dotnet.12.597246.5">Joel on Software</a>, I refined the code a little and came up with this (which is basically identical to Tom&#8217;s solution):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> AutoEllipsisToolStripRenderer <span style="color: #008000;">:</span> ToolStripSystemRenderer
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnRenderItemText<span style="color: #000000;">&#40;</span>ToolStripItemTextRenderEventArgs e<span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    ToolStripStatusLabel label <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">Item</span> <span style="color: #0600FF;">as</span> ToolStripStatusLabel<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>label <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnRenderItemText</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      return<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    TextRenderer.<span style="color: #0000FF;">DrawText</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Graphics</span>,
      label.<span style="color: #0000FF;">Text</span>,
      label.<span style="color: #0000FF;">Font</span>,
      e.<span style="color: #0000FF;">TextRectangle</span>,
      label.<span style="color: #0000FF;">ForeColor</span>,
      TextFormatFlags.<span style="color: #0000FF;">EndEllipsis</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You need to wire this code into your StatusStrip:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">MainStatusStrip</span>.<span style="color: #0000FF;">Renderer</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> AutoEllipsisToolStripRenderer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>To the ToolStripStatusLabel will also need it&#8217;s Spring property set to true, and if you want the text left aligned the TextAlign Property will need to be set to MiddleLeft.</p>
<p>If you want the ToolTip to work correctly the StatusStrip will need to have ShowItemToolTips set to work, and the ToolStripStatusLabel AutoToolTip set to true. It isn&#8217;t perfect as the ToolTip is displayed when the text is not truncated, but it is close enough for my purposes.</p>
<p>I am exploring WPF at the moment, I was glad to see the default behaviour of a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.statusbar.aspx">StatusBar</a> was to just stop rendering the text at the bounds of control, an ellipsis could be added with the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.texttrimming.aspx">TextTrimming</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.textwrapping.aspx">TextWraping</a> properties:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;statusbar</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0,102,0,0&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;MainStatusBar&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;statusbaritem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textblock</span> <span style="color: #000066;">TextWrapping</span>=<span style="color: #ff0000;">&quot;NoWrap&quot;</span> <span style="color: #000066;">TextTrimming</span>=<span style="color: #ff0000;">&quot;CharacterEllipsis&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            Some Text Goes Here, this text may be very long as demonstrated here. In the event we run out of space an ellipsis is used.
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/textblock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/statusbaritem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/statusbar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.richard-slater.co.uk/archives/2010/03/07/lack-of-autoellipsis-support-in-toolstripsystemrenderer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>LINQPad Crash</title>
		<link>http://www.richard-slater.co.uk/archives/2010/03/02/linqpad-crash/</link>
		<comments>http://www.richard-slater.co.uk/archives/2010/03/02/linqpad-crash/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 21:48:18 +0000</pubDate>
		<dc:creator>Richard Slater</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Crash]]></category>
		<category><![CDATA[Exception]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQPad]]></category>

		<guid isPermaLink="false">http://www.richard-slater.co.uk/?p=746</guid>
		<description><![CDATA[Update: I never did &#8220;fix&#8221; this problem, installing .NET 4 then using LINQPad 4 seems to work well. I found myself using LINQPad more often than creating console applications, so much so I dicided to make the small but worth while investment in the optional &#8220;Autocompletion&#8221; (Intelisense-like) component. The licence is great because I can have [...]]]></description>
			<content:encoded><![CDATA[<p><strong><span style="color: #ff0000;">Update:</span> I never did &#8220;fix&#8221; this problem, installing .NET 4 then using LINQPad 4 seems to work well.</strong></p>
<p>I found myself using <a href="http://www.linqpad.net/">LINQPad</a> more often than creating console applications, so much so I dicided to make the small but worth while investment in the optional &#8220;Autocompletion&#8221; (Intelisense-like) component. The licence is great because I can have it installed on all three of my PCs without having to buy extra licences.</p>
<p>I was figuring out the limits of the Math.Pow function a few days ago on the laptop when the LINQPad upgrade message appeared, not sure what happened next because LINQPad crashed with the following exception.</p>
<p><strong>System Specification:</strong></p>
<ul>
<li>Windows 7 Home Premium x64</li>
<li>.NET v2.0.50727 (+3.0 &amp; 3.5)</li>
<li>.NET v4.0.20506</li>
<li>VisualStudio 2010 Beta1</li>
</ul>
<pre>System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at LINQPad.Program.ProcessException(Exception ex)
   at LINQPad.Program.Start(String[] args)
   at LINQPad.ProgramStarter.Run(String[] args)
   at LINQPad.Loader.Main(String[] args)</pre>
<p>If anyone has any theories as to how this can be fixed I would be very apprecitive if you could post in the comments.</p>
<p>So far I have tried:</p>
<ul>
<li>Reinstalling from the latest (2.10.1)<strong> </strong>from the LINQPad website.</li>
<li>Restarted the computer.</li>
<li>Removing LINQPad through Add/Remove Programs.</li>
<li>Remove LINQPAD manually.</li>
<li>Rename %APPDATA%\LINQPad.</li>
<li>Looked for Native Images in C:\Windows\assembly &#8211; None there</li>
</ul>
<p>It seems to me that LINQPad throws some exception, which it&#8217;s built in exception handler tries to handle then fails, this probably means that the above stack trace is probably not indicative of what is causing the problem. Not that I think it will make a difference but I am going to try upgrading to Visual Studio 2010 RC tomorrow then at least I wll be able to use LINQPad for .NET 4.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richard-slater.co.uk/archives/2010/03/02/linqpad-crash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preventing the PictureBox control from locking files</title>
		<link>http://www.richard-slater.co.uk/archives/2010/02/28/preventing-the-picturebox-control-from-locking-files/</link>
		<comments>http://www.richard-slater.co.uk/archives/2010/02/28/preventing-the-picturebox-control-from-locking-files/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 20:18:58 +0000</pubDate>
		<dc:creator>Richard Slater</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[EVEMon]]></category>

		<guid isPermaLink="false">http://www.richard-slater.co.uk/?p=739</guid>
		<description><![CDATA[One of our more regular contributors to EVEMon posted on our forums showing that the application was incapable of updating cached files (specifically images), after a bit testing I discovered the following Exception was being thrown when trying to overwrite the file in question: System.IO.IOException: The process cannot access the file 'path\filename' because it is [...]]]></description>
			<content:encoded><![CDATA[<p>One of our more regular contributors to <a href="http://evemon.battleclinic.com/">EVEMon</a> posted on our forums showing that the application was incapable of updating cached files (specifically images), after a bit testing I discovered the following Exception was being thrown when trying to overwrite the file in question:</p>
<pre>System.IO.IOException: The process cannot access the file 'path\filename' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at EVEMon.Common.FileHelper.OverwriteOrWarnTheUser(String srcFileName, String destFileName) in EVEMon.Common\FileHelper.cs:line 108
   at EVEMon.Common.FileHelper.OverwriteOrWarnTheUser(String destFileName, Func`2 writeContentFunc) in EVEMon.Common\FileHelper.cs:line 82
   at EVEMon.Common.Controls.CharacterPortrait.SavePortraitToCache(Image newImage) in EVEMon.Common\Controls\CharacterPortrait.cs:line 248
</pre>
<p>After a bit of searching around I discovered a <a href="http://stackoverflow.com/questions/2188464/net-app-locks-file">post on StackOverflow</a> identifying that System.Drawing.Bitmap(string filename) would lock the filename until the Bitmap was disposed of. The post presented a solution but no code, A bit of further searching confirmed my expectation that <a href="http://msdn.microsoft.com/en-us/library/4sahykhd.aspx">Image.FromFile(string filename)</a> was subject to the same locking behaviour:</p>
<blockquote><p>The file remains locked until the <a id="ctl00_MTCS_main_ctl51_ctl00_ctl00" onclick="javascript:Track('ctl00_MTCS_main_ctl51_ctl00_contenthere|ctl00_MTCS_main_ctl51_ctl00_ctl00',this);" href="http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx">Image</a> is disposed.</p></blockquote>
<p>A bit more searching identified another <a href="http://stackoverflow.com/questions/542217/load-a-bitmapsource-and-save-using-the-same-name-in-wpf-ioexception">post on StackOverflow</a> which gave me the basic syntax and structure for the code I was going to need to implement this in EVEMon. The final code looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>189
190
191
192
193
194
195
196
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">MemoryStream stream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> imageBytes <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">ReadAllBytes</span><span style="color: #000000;">&#40;</span>cacheFileName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
stream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>imageBytes, <span style="color: #FF0000;">0</span>, imageBytes.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
stream.<span style="color: #0000FF;">Position</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
var image <span style="color: #008000;">=</span> Image.<span style="color: #0000FF;">FromStream</span><span style="color: #000000;">&#40;</span>stream<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">return</span> image<span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>It appears that GDI+ will lock any image that is loaded into a control in WinForms and WPF, several comments on StackOverflow and byte.com suggested that even disposing of the control and the FileStream was not a reliable way of being able to write to the file so the above method is seems to be be the best solution all round.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richard-slater.co.uk/archives/2010/02/28/preventing-the-picturebox-control-from-locking-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Controling Code Outlining with the Keyboard</title>
		<link>http://www.richard-slater.co.uk/archives/2009/11/03/controling-code-outlining-with-the-keyboard/</link>
		<comments>http://www.richard-slater.co.uk/archives/2009/11/03/controling-code-outlining-with-the-keyboard/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 19:13:35 +0000</pubDate>
		<dc:creator>Richard Slater</dc:creator>
				<category><![CDATA[CodeRush]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.richard-slater.co.uk/?p=702</guid>
		<description><![CDATA[Code outlining is a feature of supported by Visual Studio and many other editors, MSDN has some good documentation for VS2005, VS2008 and VS2010. If I were asked to explain this as briefly as possible, I would probably say: Code Outlining is the logical partitioning of code in such a way that the user interface, [...]]]></description>
			<content:encoded><![CDATA[<p>Code outlining is a feature of supported by Visual Studio and many other editors, MSDN has some good documentation for <a href="http://msdn.microsoft.com/en-us/library/td6a5x4s(VS.80).aspx">VS2005</a>, <a href="http://msdn.microsoft.com/en-us/library/td6a5x4s.aspx">VS2008 </a>and <a href="http://msdn.microsoft.com/en-us/library/td6a5x4s(VS.100).aspx">VS2010</a>. If I were asked to explain this as briefly as possible, I would probably say:</p>
<blockquote><p>Code Outlining is the logical partitioning of code in such a way that the user interface, or editor, is able to selectively hide the body of the content (such as a class, struct, enum or method) whilst leaving the signature or some identifying comment visible.</p></blockquote>
<p>You can see this in action in Visual Studio 2008 with the following Screenshot:</p>
<p><img class="alignnone size-full wp-image-705" title="CodeOutliningVS2008" src="http://www.richard-slater.co.uk/wp-content/uploads/2009/11/CodeOutliningVS2008.png" alt="CodeOutliningVS2008" width="500" height="295" /></p>
<p>I accidentally turned off Code Outlining today by hitting some keyboard shortcut that I didn&#8217;t know how to reverse, this lead me to discover several useful keyboard shortcuts for managing the display of your code from the keyboard.</p>
<p>As it turns out I managed to hit Ctrl-M followed by Ctrl-P (or just P in fact) which maps to Edit.StopOutlining, by default it seems that the Visual C# 2005 mapping scheme doesn&#8217;t provide a shortcut to enable Automatic Outlining so instead you can access the command through Edit Menu -&gt; Outlining -&gt; Start Automatic Outlining.</p>
<p>Enabled again, I get to play with code outlining from the keyboard:</p>
<ul>
<li>To toggle (collapse an expanded block or expand a collapsed block) the closest outlined element use Ctrl-M followed by Ctrl-M.</li>
<li>To toggle everything use Ctrl-M followed by Ctrl-L (I find little use for this)</li>
<li>To collapse to definitions use Ctrl-M followed by Ctrl-O</li>
</ul>
<p>The last one is the most useful when used in conjunction with <a href="http://msdn.microsoft.com/en-us/library/9a1ybwek.aspx">Regions</a> as after colapsing to definitions you will get something similar to this:</p>
<p><img class="alignnone size-full wp-image-706" title="ColapseToDefinitionsVS2008" src="http://www.richard-slater.co.uk/wp-content/uploads/2009/11/ColapseToDefinitionsVS2008.png" alt="ColapseToDefinitionsVS2008" width="555" height="389" /></p>
<p>You might have noticed in the first screenshot that CodeRush Xpress adds a coloured line between the beginning and end of blocks of code, this is a nice feature if you have long blocks of code, which of course you shouldn&#8217;t have.</p>
<p><img class="alignnone size-full wp-image-707" title="CodeRushXpressBlockLines" src="http://www.richard-slater.co.uk/wp-content/uploads/2009/11/CodeRushXpressBlockLines.png" alt="CodeRushXpressBlockLines" width="70" height="107" /></p>
<p>There we go, an errant key stroke can lead to learning and blogging, who would have thought it?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richard-slater.co.uk/archives/2009/11/03/controling-code-outlining-with-the-keyboard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adjusting Selections in CodeRush Xpress</title>
		<link>http://www.richard-slater.co.uk/archives/2009/10/16/adjusting-selections-in-coderush-xpress/</link>
		<comments>http://www.richard-slater.co.uk/archives/2009/10/16/adjusting-selections-in-coderush-xpress/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 19:30:24 +0000</pubDate>
		<dc:creator>Richard Slater</dc:creator>
				<category><![CDATA[CodeRush]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DevExpress]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Resharper]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.richard-slater.co.uk/?p=663</guid>
		<description><![CDATA[I found this function totally accidentally when I knocked my mouse into a key when something interesting popped up on Twitter. I happened to have a variable selected like so: The key that I knocked was the Number Pad &#8220;+&#8221; key, and it expanded the selection like this: As any self respecting Systems Administrator would [...]]]></description>
			<content:encoded><![CDATA[<p>I found this function totally accidentally when I knocked my mouse into a key when something interesting popped up on Twitter. I happened to have a variable selected like so:</p>
<p><img class="size-full wp-image-664 alignnone" title="Variable Selected" src="http://www.richard-slater.co.uk/wp-content/uploads/2009/10/VariableSelected.JPG" alt="Variable Selected in Visual Studio" width="198" height="69" /></p>
<p>The key that I knocked was the Number Pad &#8220;+&#8221; key, and it expanded the selection like this:</p>
<p><img class="alignnone size-full wp-image-665" title="VariableSelectedPlus1" src="http://www.richard-slater.co.uk/wp-content/uploads/2009/10/VariableSelectedPlus1.JPG" alt="VariableSelectedPlus1" width="184" height="70" /></p>
<p>As any self respecting Systems Administrator would do I wondered if doing exactly the same thing repeatedly would have equal or compound effects.</p>
<p><img class="alignnone size-full wp-image-666" title="VariableSelectedAgainAgain" src="http://www.richard-slater.co.uk/wp-content/uploads/2009/10/VariableSelectedAgainAgain.JPG" alt="VariableSelectedAgainAgain" width="535" height="158" /></p>
<p>Strangely enough it worked, the selection will continue to expand selecting increasingly larger sections of code. The reverse works as well if you press the Number Pad &#8220;-&#8221; key the selection will be reduced.</p>
<dl id="attachment_667" class="wp-caption alignright" style="width: 298px;">
<dt class="wp-caption-dt"><img class="size-full wp-image-667  " title="WhatHappened" src="http://www.richard-slater.co.uk/wp-content/uploads/2009/10/WhatHappened.png" alt="WhatHappened" width="288" height="178" /></dt>
</dl>
<p>As I was looking for <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/selection.xml">documentation</a> on this function; I found that you could use the CamelCase select function to start selections off using Ctrl+Shift+Left (or Ctrl+Shift+Right) this will take your selection from the current cursor position to the next upper case character to the left (or right). This can be coupled with the Number Pad &#8220;+&#8221; to expand your selection from a variable to an expression, to a line then a block. Neat!</p>
<p>Each of these discoveries was announced with a handy little popup in the bottom right hand corner of the viewport telling me what happened, and if relevant what function was suppressed.</p>
<p>I am going to make a concerted effort to spend my lunch break watching some more videos on DevExpress&#8217; training site. I got 10 minutes today and watched what appeared to be footage from a launch event or a conference which was interesting but didn&#8217;t really teach me anything specifically.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richard-slater.co.uk/archives/2009/10/16/adjusting-selections-in-coderush-xpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Find Files in CodeRush Xpress</title>
		<link>http://www.richard-slater.co.uk/archives/2009/10/12/find-files-in-coderush-xpress/</link>
		<comments>http://www.richard-slater.co.uk/archives/2009/10/12/find-files-in-coderush-xpress/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 12:27:41 +0000</pubDate>
		<dc:creator>Richard Slater</dc:creator>
				<category><![CDATA[CodeRush]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DevExpress]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Resharper]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.richard-slater.co.uk/?p=657</guid>
		<description><![CDATA[As I mentioned in my previous entry I have started using DevExpress&#8217;s CodeRush Xpress. It is a free cut down version of CodeRush that I heard about in an episode of .NET Rocks. I have wanted add something to my Visual Studio development experience and I can&#8217;t justify the cost of either Resharper or CodeRush [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in my previous entry I have started using <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/Index.xml">DevExpress&#8217;s CodeRush Xpress</a>. It is a free cut down version of <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a> that I heard about in an episode of <a href="http://www.dotnetrocks.com/">.NET Rocks</a>. I have wanted add something to my Visual Studio development experience and I can&#8217;t justify the cost of either Resharper or CodeRush at the moment. There has been quite a bit of discussion about <a href="http://www.hanselman.com/blog/ReSharperVsCodeRush.aspx">Resharper vs. CodeRush</a> and in my experience most people I have spoken to love one and hate the other.</p>
<p>I hope to be able to write a series of posts about some of the features found in CodeRush Xpress which I hope will clarify their purpose and use in my mind and maybe help someone find the function they are looking for.</p>
<p><img class="alignleft size-full wp-image-658" title="QuickFileNavigationAbou" src="http://www.richard-slater.co.uk/wp-content/uploads/2009/10/QuickFileNavigationAbou.png" alt="QuickFileNavigationAbou" width="323" height="154" />First off &#8220;Quick File Navigation&#8221;, this is a search function for locating a file by file name, I am finding it increasingly useful when looking through patches as it allows me to very quickly jump to a file name. Particularly as I am trying to move my projects to a one class per file so if I can remember the class name I can find the code file very quickly.</p>
<p>The &#8220;Quick File Navigation&#8221; dialog is accessible through the keyboard short cut Ctrl+Alt+F. Typing will filter the list box by the name of the file matching anywhere in the filename including the extension.</p>
<p>An additional feature for those who like me use CamelCase in their file names; if you enter your filter terms in capital letters it will search for capitalized words, in order within file names. Thus entering &#8220;AW&#8221; into the search box will also bring up the AboutWindow.cs in the above solution.</p>
<p>Combining the above with the Ctrl-G keyboard shortcut in Visual Studio 2008 to go to a specific line we can do the following to go to line 162 in ShipLoadoutSelectWindow.cs:</p>
<p><em>Ctrl+Alt+F  →  S,L,S,W  →  Ctrl+G →  1,6,2 </em></p>
<p>DevExpress have a great <a href="http://tv.devexpress.com/CodeRushXpress06Navigation.movie">training video</a> on their tv.devexpress.com sub site.</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 225px; width: 1px; height: 1px;"><!-- 		@page { margin: 2cm } 		P { margin-bottom: 0.21cm } --></p>
<p style="border: 1px solid #000000; padding: 0.05cm; margin-bottom: 0cm;"><span style="font-family: Calibri,sans-serif;">Ctrl+Alt+F  →  S,L,S,W  →  Ctrl+G →  1,6,2 </span></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.richard-slater.co.uk/archives/2009/10/12/find-files-in-coderush-xpress/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fixing EVEMon&#8217;s Crashy C++ DLLs</title>
		<link>http://www.richard-slater.co.uk/archives/2009/04/18/evemons-crashy-dlls/</link>
		<comments>http://www.richard-slater.co.uk/archives/2009/04/18/evemons-crashy-dlls/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 19:22:06 +0000</pubDate>
		<dc:creator>Richard Slater</dc:creator>
				<category><![CDATA[EVE Online]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[EVEMon]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.richard-slater.co.uk/?p=628</guid>
		<description><![CDATA[I have been working on EVEMon for about two months now, taking on the responsibility of committing changes to the trunk, fixing bugs and adding new features. As a project I have been involved for several years submitting bug fixes and little features, it was down to my experiences with EVEMon that I decided to [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working on <a href="http://evemon.battleclinic.com/">EVEMon</a> for about two months now, taking on the responsibility of committing changes to the trunk, fixing bugs and adding new features. As a project I have been involved for several years submitting bug fixes and little features, it was down to my experiences with EVEMon that I decided to implement <a class='wikipedia' href='http://en.wikipedia.org/wiki/Subversion_%28software%29' title='Wikipedia article on Subversion (software)'>Subversion</a> and <a href="http://trac.edgewall.org/">Trac</a> at work.</p>
<p>Unfortunately the first time it came to me to be responsible for a release, it seemed to go terribly wrong. The updated installer worked fine, and it seemed initially there were no problems with the updated code base. However BattleClinic shortly went a little mad with bug reports similar to this one:</p>
<blockquote><p>EVEMon Version: 1.2.7.1283<br />
.NET Runtime Version: 2.0.50727.1434<br />
Operating System: Microsoft Windows NT 6.0.6001 Service Pack 1<br />
Executable Path: &#8220;C:\Program Files\EVEMon\EVEMon.exe&#8221;</p>
<p>System.IO.FileLoadException: Could not load file or assembly &#8216;lgLCDNETWrapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null&#8217; or one of its dependencies. The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log for more detail. (Exception from HRESULT: 0x800736B1)<br />
File name: &#8216;lgLCDNETWrapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null&#8217; &#8212;&gt; System.Runtime.InteropServices.COMException (0x800736B1): The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log for more detail. (Exception from HRESULT: 0x800736B1)<br />
at EVEMon.LogitechG15.Lcdisplay.Dispose(Boolean bDisposing)<br />
at EVEMon.LogitechG15.Lcdisplay.Finalize() in D:\EVEMon\EveMon.LogitechG15\Lcdisplay.cs:line 70</p></blockquote>
<p>For those who are not familiar the above is the crash report generated by EVEMon when a .NET exception is unhandled. lgLCDNETWrapper is the C++ library EVEMon uses to communicate with the <a href="http://www.logitech.com/index.cfm/keyboards/keyboard/devices/3498&amp;cl=US,EN">Logitech G15</a>. A similar error was generated for EVEMons other C++ component the window relocator.</p>
<p><a href="http://www.battleclinic.com/forum/index.php?action=profile;u=23955">Tonto Auri</a> quickly spotted that the problem was something to do with the re-compiled C++ DLLs and switching the new DLLs with those from an older package resolved the problems. Whilst this was a fix, I wasn&#8217;t willing to just revert the changes and give up on the changes we had made to the G15 library, additionally I was concerned that we had not made changes to the window relocation code and that was causing at least one person a problem.</p>
<p>Quite a bit of searching about with Google and <a href="http://stackoverflow.com/">Stack Overflow</a> resulting in finding these two little gems of information:</p>
<ul>
<li><a href="http://stackoverflow.com/questions/230715/mixed-mode-library-and-crt-dependencies-help">Mixed Mode Library and CRT Dependencies</a> (Stack Overflow)</li>
<li><a href="http://www.nuonsoft.com/blog/2008/10/29/binding-to-the-most-recent-visual-studio-libraries/">Binding to the most recent Visual Studio Libraries</a> (Nuonsoft)</li>
</ul>
<p>These two posts basically gave me the tools and the knowledge to fix EVEMon&#8217;s problems.</p>
<p>First I made the sudden (and &#8220;facepalm&#8221;) realization that EVEMon deployed the Visual Studio 2005 Redistributable; we had moved to Visual Studio 2008 SP1 about a month before the release of 1.2.7. I confirmed this with the <a href="http://www.dependencywalker.com/">Dependency Walker</a> utility, by walking lgLCDNETWrapper.dll I was able to confirm that version 9.0.21022.8 of the Microsoft CRT Library (Microsoft.VC90.CRT) would be required.</p>
<p>The next trick was figuring out which re-distributables to deploy with EVEMon to ensure the end user would have all the dependencies required to use all of the features; frustratingly it seems that Microsoft only bundles the latest version of the re-distributables with Visual Studio 2008 and that was version 9.0.30729.1 which was not going to cut it.</p>
<p>Enter Nuonsofts excellent article providing a step by step guide on adding the required compiler flags to ensure the latest version of the CRT was bound. I did find a nice GUI resource editor that was capable of getting the manifests out in <a href="http://www.wilsonc.demon.co.uk/d10resourceeditor.htm">XN Resource Editor</a> which made the process a little faster.</p>
<p>A little bit of hacking later the updated Visual Studio 2008 SP1 CRT DLLs and manifest file were deployed to the Microsoft.VC90.CRT folder within the EVEMon program files directory as the VC80 CRT libraries were in the past and the two C++ projects were setup to bind to the latest version with the _BIND_TO_CURRENT_CRT_VERSION=1 preprocessor definition.</p>
<p>A bit more poking arround with XN Resource Editor and Dependency Walker showed we were now in a far better position to have a working copy of EVEMon in the hands of our end users.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richard-slater.co.uk/archives/2009/04/18/evemons-crashy-dlls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Strange SPQuery Behaviour</title>
		<link>http://www.richard-slater.co.uk/archives/2009/02/13/strange-spquery-behaviour/</link>
		<comments>http://www.richard-slater.co.uk/archives/2009/02/13/strange-spquery-behaviour/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 12:31:43 +0000</pubDate>
		<dc:creator>Richard Slater</dc:creator>
				<category><![CDATA[Diary]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CAML]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[WebParts]]></category>

		<guid isPermaLink="false">http://www.richard-slater.co.uk/?p=571</guid>
		<description><![CDATA[I have been working on a very simple web part for a long time, the actual code for the web part takes less than an hour to write and merely displays a colour coded letter based on a query from a SharePoint list taking the date as a parameter. It has taken a long time [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working on a very simple web part for a long time, the actual code for the web part takes less than an hour to write and merely displays a colour coded letter based on a query from a SharePoint list taking the date as a parameter.</p>
<p>It has taken a long time because of some strange behaviour with the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.aspx">SPQuery</a> object, initially there was a problem with every row in the table being returned irrespective of the query, the odd thing was that if you ran the query in <a href="http://www.u2u.info/Blogs/Patrick/Lists/Posts/Post.aspx?ID=1315">U2U CAML Query Builder</a> it worked and the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitemcollection.count.aspx">SPListItemCollection.count</a> method would return 1 suggesting the query was working.</p>
<p>After adding a copious number of debugging command, I built up a pattern of what was happening and why. However no matter what I changed it either threw an exception as expected or returned all of the rows. I started commenting out lines of code one at a time trying to find the culprit, eventually moving on to commenting out the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.writeline.aspx">Debug.WriteLine</a> statements, until I hit this line:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>86
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Debug.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>fetchCurrentWeek.<span style="color: #0000FF;">ViewXml</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>After commenting out this line the whole thing worked perfectly, I can put the date forward on the server and it will update I can change the source table and it reflects the changes after the cache has been cleared. I have looked over the Microsoft Documentation on <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.viewxml.aspx">SPQuery.ViewXml</a> and can&#8217;t find any reference to it actually affecting the functionality of the object.</p>
<p>I have included my code at the end of this post for anyone to see, feel free to experiment with it. If you figure it out please do let me know.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>SPWeb site <span style="color: #008000;">=</span> SPControl.<span style="color: #0000FF;">GetContextSite</span><span style="color: #000000;">&#40;</span>Context<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">OpenWeb</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
 SPList weekList <span style="color: #008000;">=</span> site.<span style="color: #0000FF;">GetList</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SourceList</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008080; font-style: italic;">// sharepoint requires that we use a ISO8601 DateTime string,</span>
 <span style="color: #008080; font-style: italic;">// generate it and insert it into the query</span>
 <span style="color: #FF0000;">string</span> mondayOfCurrentWeek <span style="color: #008000;">=</span> SPUtility.<span style="color: #0000FF;">CreateISO8601DateTimeFromSystemDateTime</span><span style="color: #000000;">&#40;</span>GetMondayOfCurrentWeek<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Date</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 StringBuilder camlBuilder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 camlBuilder.<span style="color: #0000FF;">AppendLine</span>  <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;Where&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 camlBuilder.<span style="color: #0000FF;">AppendLine</span>  <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;  &lt;Eq&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 camlBuilder.<span style="color: #0000FF;">AppendLine</span>  <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;    &lt;FieldRef Name='WeekCommencing'/&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 camlBuilder.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;    &lt;Value Type='DateTime'&gt;{0}&lt;/Value&gt;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>, mondayOfCurrentWeek<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 camlBuilder.<span style="color: #0000FF;">AppendLine</span>  <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;  &lt;/Eq&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 camlBuilder.<span style="color: #0000FF;">AppendLine</span>  <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;/Where&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
 <span style="color: #FF0000;">string</span> getWeekCAML <span style="color: #008000;">=</span> camlBuilder.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 SPQuery fetchCurrentWeek <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPQuery<span style="color: #000000;">&#40;</span>weekList.<span style="color: #0000FF;">DefaultView</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008080; font-style: italic;">// fetch all of the matching weeks from the table</span>
 fetchCurrentWeek.<span style="color: #0000FF;">Query</span> <span style="color: #008000;">=</span> getWeekCAML<span style="color: #008000;">;</span>
 SPListItemCollection matchingWeeks <span style="color: #008000;">=</span> weekList.<span style="color: #0000FF;">GetItems</span><span style="color: #000000;">&#40;</span>fetchCurrentWeek<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// the results of the query should only ever equal 1 if it dosn't something is wrong</span>
 <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>matchingWeeks.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
  SPListItem week <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>SPListItem<span style="color: #000000;">&#41;</span>matchingWeeks<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
  WeekType <span style="color: #008000;">=</span> week<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Week Type&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  WeekType <span style="color: #008000;">=</span> SPEncode.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>WeekType<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// HtmlEncode the result to make sure it dosn't contain any nastys</span>
  <span style="color: #FF0000;">String</span> WeekNumber <span style="color: #008000;">=</span> week<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Week Number&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  WeekNumber <span style="color: #008000;">=</span> SPEncode.<span style="color: #0000FF;">HtmlEncode</span><span style="color: #000000;">&#40;</span>WeekNumber<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  Debug.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span>CultureInfo.<span style="color: #0000FF;">InvariantCulture</span>, <span style="color: #666666;">&quot;[WeekWidget] Found record for {0} as Week {1} - Number {2}&quot;</span>, mondayOfCurrentWeek, WeekType, WeekNumber<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008080; font-style: italic;">// cache the WeekType for 7 days from midnight on Monday as this is when the changeover between weeks happenswou</span>
  Context.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span>CACHE_WEEKTYPE, WeekType, <span style="color: #0600FF;">null</span>, GetMondayOfCurrentWeek<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddDays</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">7</span><span style="color: #000000;">&#41;</span>, TimeSpan.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #000000;">&#125;</span> <span style="color: #008080; font-style: italic;">// end if matchingWeeks count equal to 1</span>
 <span style="color: #0600FF;">else</span>
 <span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// more than one week matched, we can not determine the output correctly</span>
  ErrorText.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;More than one week matched in the table, please check the table.&quot;</span><span style="color: #008000;">;</span>
  LogEvent<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;More than one week (&quot;</span> <span style="color: #008000;">+</span> matchingWeeks.<span style="color: #0000FF;">Count</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;) matched in the table, please check the table.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #000000;">&#125;</span> <span style="color: #008080; font-style: italic;">// end else</span></pre></div></div>

<p>Happy Hunting</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richard-slater.co.uk/archives/2009/02/13/strange-spquery-behaviour/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
