<?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>.Net Smoothie &#187; Visual Studio</title>
	<atom:link href="http://www.richardbushnell.net/category/visual-studio/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardbushnell.net</link>
	<description></description>
	<lastBuildDate>Wed, 30 Dec 2009 11:42:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The Value of Being Free to See the Source</title>
		<link>http://www.richardbushnell.net/2008/04/18/the-value-of-being-free-to-see-the-source/</link>
		<comments>http://www.richardbushnell.net/2008/04/18/the-value-of-being-free-to-see-the-source/#comments</comments>
		<pubDate>Fri, 18 Apr 2008 14:09:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[exceptions]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/04/18/the-value-of-being-free-to-see-the-source/</guid>
		<description><![CDATA[Since the source code to ASP.Net was made available, I&#8217;ve been using it extensively. Here&#8217;s a great example of why it&#8217;s so valuable. I&#8217;ve been trying to integrate the Enterprise Library 3.1 Exception Handling Block into my application. My application is split into a core and web UI specific components, so I&#8217;ve defined errors in [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F04%2F18%2Fthe-value-of-being-free-to-see-the-source%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Since the <a href="http://richardbushnell.net/index.php/2008/01/17/net-source-code-now-available/">source code to ASP.Net was made available</a>, I&#8217;ve been using it extensively. Here&#8217;s a great example of why it&#8217;s so valuable.</p>
<p>I&#8217;ve been trying to integrate the Enterprise Library 3.1 Exception Handling Block into my application. My application is split into a core and web UI specific components, so I&#8217;ve defined errors in my code to be thrown when a resource is not available. The web application configuration file specifies that if a specific exception, e.g. a ResourceNotFoundException, is thrown, the Exception Handling Block should replace that exception with a 404 Resource Not Found error using Http. That should in turn use the CustomErrors feature to redirect to a 404 not found page.</p>
<p>Makes sense, and sounds simple, don&#8217;t you think? </p>
<p>Nothing in the docs says that it shouldn&#8217;t work.</p>
<p><em>But it doesn&#8217;t</em>. It simply <em>won&#8217;t work</em>. Why? Well, there&#8217;s nothing on the web. But after spending some serious hours digging through the source code, I can finally see why.</p>
<p>Here&#8217;s a lovely little hidden-to-the-world snippet of the code I got inside of Visual Studio:</p>
<pre class="code">code = HttpException.GetHttpCodeForException(e);

<span style="color: green">// Don't raise event for 404.  See VSWhidbey 124147.
</span><span style="color: blue">if </span>(code != 404) {
  WebBaseEvent.RaiseRuntimeError(e, <span style="color: blue">this</span>);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>So it would never work! </p>
<p>Nice of them to let me know.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F04%2F18%2Fthe-value-of-being-free-to-see-the-source%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/04/18/the-value-of-being-free-to-see-the-source/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Easy Data-loading with LINQ-to-SQL and LINQ-to-XML</title>
		<link>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/</link>
		<comments>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 07:53:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LINQ-to-XML]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[VB.Net]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[VB]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/</guid>
		<description><![CDATA[.Net 3.5 had some nice tricks in it. LINQ-to-XML was one of them. With the new &#34;X&#34;-types, you can make working with XML really easy. VB.Net 9 takes it one step further, and lets you write XML in your code without strings. &#34;Hey Rich, that&#8217;s old news,&#34; I hear you say. &#34;And who&#8217;s interested in [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F20%2Feasy-data-loading-with-linq-to-sql-and-linq-to-xml-4%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>.Net 3.5 had some nice tricks in it. LINQ-to-XML was one of them. With the new &quot;X&quot;-types, you can make working with XML really easy. </p>
<p>VB.Net 9 takes it one step further, and lets you write XML in your code without strings.</p>
<p>&quot;Hey Rich, that&#8217;s old news,&quot; I hear you say. &quot;And who&#8217;s interested in VB today anyway?&quot;</p>
<p>Well, apparently there are a lot of VB-er&#8217;s still out there. I am mainly a C# developer myself, but I found that VB was perfect for a problem I had recently -&#160; loading of XML data into a SQL Server table.</p>
<p><span id="more-98"></span></p>
<h2>SQL Server 2005 and XML Fields</h2>
<p>I recently came across the need to load a database with XML data fields in SQL Server 2005. I had a series of XML documents which all needed to be pumped into a table containing a field of type XML. I could have opened up SQL Server Management Studio and fired in a load of strings, but I was a bit concerned that the XML might not be formatted quite correctly, which would have caused problems in the loading process.</p>
<p>Another problem was that I wanted to be able to add dynamic XML. I wanted to generate some XML data on the fly. That&#8217;s not so easy with insert statements in SQL.</p>
<h2>LINQ-to-SQL and XML Fields</h2>
<p>Using my recently-acquired LINQ-to-SQL knowledge, I knew that the Entities generated for an XML field are translated to <strong>XElements</strong> &#8211; the new XML type in .Net 3.5. I also knew that VB 9 lets you create an <strong>XElement</strong> by using XML in code itself: </p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image15.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="200" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb11.png" width="445" border="0" /></a>&#160;</p>
<p>So I came up with a plan&#8230;</p>
<h2>Inserts with LINQ-to-SQL</h2>
<p>For data-loading, LINQ-to-SQL is magic. A simple Insert is a piece of cake.</p>
<p>First, create your <strong>DataContext</strong> using the Visual Studio template, then drag the table you are inserting to onto the <strong>DataContext</strong> canvas. That gives you an Entity class. You then can create an Entity in your code, call <strong>InsertOnSubmit</strong> on the <strong>DataContext</strong> table, then call <strong>SubmitChanges</strong> on the <strong>DataContext</strong> itself.</p>
<p>Using Object Property Initializers, you can make this really easy.</p>
<p>Here&#8217;s what it looks like with VB.Net using an XML datatype:</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image13.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="631" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb9.png" width="475" border="0" /></a> </p>
<p>(Sorry to have torn the edge off the code there, but it wouldn&#8217;t fit nicely in my post at full width. XML can be a <em>little </em>long sometimes.)</p>
<p>The great thing here is that any malformed XML is highlighted (or even automatically corrected) for me by the VB compiler. I can also add parameters and generate lots of different XML using the familiar ASP syntax:</p>
<div style="width:480px;margin-right:100px">
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image14.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="112" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb10.png" width="374" border="0" /></a>&#160;</p>
</div>
<div style="clear:left">
<p>And as <strong>SubmitChanges</strong> uses a single transaction, the data-loading can easily be an all-or-nothing affair. In addition, any foreign key relationships can easily be taken care of by assigning objects to each other using standard object notation. The <strong>DataContext</strong> takes care of doing things in the correct order for you.</p>
</div>
<p>It&#8217;s all super-easy, and takes about 1 minute to get started.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F20%2Feasy-data-loading-with-linq-to-sql-and-linq-to-xml-4%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Remove and Sort Those Ugly &#8220;using-Statements&#8221;</title>
		<link>http://www.richardbushnell.net/2008/03/03/remove-and-sort-those-ugly-using-statements/</link>
		<comments>http://www.richardbushnell.net/2008/03/03/remove-and-sort-those-ugly-using-statements/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 08:10:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Commands]]></category>
		<category><![CDATA[PowerCommands]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/03/remove-and-sort-those-ugly-using-statements/</guid>
		<description><![CDATA[Visual Studio 2008 has lots of goodies in it, like LINQ syntax, CSS editing, and testing tools. There&#8217;s a lesser-known feature which I really appreciate though &#8211; the &#8220;Remove and Sort Usings&#8221; command in the C# editor. You activate the command by placing your cursor over the using statements and clicking on the right mouse-button. [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Fremove-and-sort-those-ugly-using-statements%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Visual Studio 2008 has lots of goodies in it, like LINQ syntax, CSS editing, and testing tools. There&#8217;s a lesser-known feature which I really appreciate though &#8211; the &#8220;Remove and Sort Usings&#8221; command in the C# editor.</p>
<p>You activate the command by placing your cursor over the using statements and clicking on the right mouse-button.</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image2.png"><img src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb.png" style="border: 0px none " alt="image" border="0" height="411" width="488" /></a></p>
<p><span id="more-65"></span></p>
<p>Using this groovy tool, I&#8217;ve been able to go through my old ugly C# files and get rid of all the superfluous using statements, then tidy them up, all at the click of a button. It&#8217;s something I always wanted to do, but had no time for, and didn&#8217;t want to pay for an extra tool to do it for me.</p>
<p>The <em>only </em>problem with the feature is that you have to go through every file and right-click on the &#8220;using&#8221; statements to tidy them up. That can get a bit tedious, but it&#8217;s worth it.</p>
<p>But now it&#8217;s the Visual Studio Extensibility Team to the rescue!</p>
<p>The VSX team at Microsoft have just released <a href="http://www.visualstudiogallery.com/ExtensionDetails.aspx?ExtensionID=df3f0c30-3d37-4e06-9ef8-3bff3508be31">PowerCommands for Visual Studio 2008</a>. It&#8217;s a free tool for Visual Studio with loads of new tools for making a VS-developer&#8217;s life easier. And one of those new tools is a Remove and Sort Usings command at the project level.</p>
<p>You activate it with a right-click on the project itself.</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image3.png"><img src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb1.png" style="border: 0px none ; margin: 0pt 140px 0pt 0pt" alt="image" border="0" height="288" width="357" /></a></p>
<p>The tool then does the same Remove and Sort command for every file in the project.</p>
<p><strong>Tip: </strong>Make sure your project is buildable before using the tool. If there is an error stopping the build from running, you will get dialogs asking you to confirm every change in every file.</p>
<p>PowerCommands for Visual Studio 2008 also contains commands for:</p>
<ul>
<li>Collapse Projects</li>
<li>Copy Class</li>
<li>Paste Class</li>
<li>Copy References</li>
<li>Paste References</li>
<li>Copy As Project Reference</li>
<li>Edit Project File</li>
<li>Open Containing Folder</li>
<li>Open Command Prompt</li>
<li>Unload Projects</li>
<li>Reload Projects</li>
<li>Remove and Sort Usings</li>
<li>Extract Constant</li>
<li>Clear Recent File List</li>
<li>Clear Recent Project List</li>
<li>Transform Templates</li>
<li>Close All</li>
</ul>
<p>So, where can you get the tool?</p>
<p>The official site for PowerCommands is at the <a href="http://www.visualstudiogallery.com/ExtensionDetails.aspx?ExtensionID=df3f0c30-3d37-4e06-9ef8-3bff3508be31">Visual Studio Gallery</a>, but when you get there, you won&#8217;t see a download button. You actually have to click the &#8220;Learn More&#8221; button to go to the <a href="http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=PowerCommands&amp;ReleaseId=559.">download page at the MSDN code gallery</a>.</p>
<p>You can read more about PowerCommands on the <a href="http://blogs.msdn.com/vsxteam/archive/2008/02/29/PowerCommands-for-Visual-Studio-2008-released.aspx">Visual Studio Team Extensibility blog</a>.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Fremove-and-sort-those-ugly-using-statements%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/03/remove-and-sort-those-ugly-using-statements/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The LinqDataSource and the Hidden Viewstate</title>
		<link>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/</link>
		<comments>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 08:00:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quaility]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LinqDataSource]]></category>
		<category><![CDATA[ViewState]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/</guid>
		<description><![CDATA[Yesterday I thought I&#8217;d learn about the LinqDataSource in ASP.Net 3.5, and got an interesting surprise. The new LinqDataSource can also be used with a LINQ-to-SQL model to perform updates. You simply add the DataSource to your page, set the table name, and set EnableUpdate to true. Then, using a standard DataControl, you can make [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F21%2Fthe-linqdatasource-and-the-hidden-viewstate%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Yesterday I thought I&#8217;d learn about the <strong>LinqDataSource</strong> in ASP.Net 3.5, and got an interesting surprise.</p>
<p>The new <strong>LinqDataSource</strong> can also be used with a LINQ-to-SQL model to perform updates. You simply add the <strong>DataSource</strong> to your page, set the table name, and set <strong>EnableUpdate</strong> to true. Then, using a standard <strong>DataControl</strong>, you can make updates to your data entities.</p>
<p>The question is, how does this work? It appears to be a bit magical. <span id="more-55"></span></p>
<h2>The Magic of LINQ</h2>
<p>In a <a href="http://richardbushnell.net/index.php/2008/02/18/how-to-update-data-with-linq-to-sql/">previous post</a>, I mentioned that LINQ-to-SQL updates can be done in two ways: either you make a call to retrieve a row, then update it, or you provide known values for all fields and try to update using optimistic concurrency.</p>
<p>If the <strong>LinqDataSource</strong> used the first technique, the performance might be bad. Plus, it would have to only update those fields that had been updated by the <strong>DataControl</strong>, and ignore the rest. That wouldn&#8217;t make sense, so I correctly supposed that was not what was happening.</p>
<p>It makes more sense that the <strong>LinqDataSource</strong> would use the values input by the user to make the changes. It could use optimistic concurrency to compare all the entered values.</p>
<p>The problem is, I don&#8217;t always display all fields from a particular row on the form, so how would the <strong>LinqDataSource</strong> know what the missing fields were. Remember, all the fields involved in the update are required for the optimistic concurrency to work. That is usually all the fields in a row, and they aren&#8217;t usually all displayed.</p>
<p>So, have a guess. Where does the <strong>LinqDataSource</strong> store it&#8217;s values?</p>
<p>Let&#8217;s think now. Where would be the <em>easiest </em>place to keep it? To keep it really simple, I&#8217;ll let you ignore <a href="http://richardbushnell.net/index.php/2007/12/23/qualities-of-a-dot-net-application-design/">all other aspects of building an application</a>, like security or performance.</p>
<p>Yep, you guessed it &#8211; <strong>ViewState</strong>.</p>
<p>The ViewState actually contains all values for all the fields in a LINQ-to-SQL entity by default. Those values are sent to the client, even if the user isn&#8217;t supposed to be able to see them.</p>
<p>Aaaarrgghhhh!</p>
<p>Now, let&#8217;s not get overexcited. The ViewState is encoded, so it&#8217;s not easy to just go and change it. But it&#8217;s not impossible. In general, I don&#8217;t think it&#8217;s such a great idea to have all data sent to a client by default. Someone is going to overlook something one day, and there&#8217;ll be a costly mistake.</p>
<p>And what if you display a large grid using the LinqDataSource. Well, you aren&#8217;t just getting the ViewState from the grid sent back and forth with your page, but the LinqDataSource is going to store <em>all your data</em> retrieved in the ViewState too.</p>
<p>Bummer!</p>
<p>Great control. Large overhead.</p>
<h2>Kicking out the ViewState</h2>
<p>The good news is that the ASP.Net team realized that this was a problem and did something about it.</p>
<p>If you look carefully, you will notice that the <strong>LinqDataSource</strong> has a property <strong>StoreOriginalValuesInViewState</strong>. By default (sigh) that property is set to true. If you set it to false, the control won&#8217;t work any more. Simple as that.</p>
<p>Here&#8217;s what the official documentation has to say about that:</p>
<blockquote><p>If you set the <strong>StoreOriginalValuesInViewState</strong> property to false, the original values are not persisted in view state for the data-bound control. In that case, LINQ to SQL cannot verify the integrity of the data. LINQ to SQL will throw an exception that indicates a data conflict even if the data in the data source has not actually changed.</p></blockquote>
<p>That&#8217;s in the remarks section of the <strong>LinqDataSource</strong>.<strong>StoreOriginalValuesInViewState</strong> property documentation.</p>
<p>Funnily enough, the same section states the problem with stuffing data into the ViewState:</p>
<blockquote><p>Storing the original values in view state can cause the page size to become unnecessarily large and can expose sensitive data to a malicious user.</p></blockquote>
<p>Now, I don&#8217;t know about you, but that looks like a pretty serious warning to me. Why isn&#8217;t it in a more obvious place, like as a comment that pops in up Intellisense whenever you use a LinqDataSource?</p>
<h2>Check the UpdateCheck</h2>
<p>So how do you handle it? How can you use the <strong>LinqDataSource</strong>?</p>
<p>The answer lies in the generation of the LINQ-to-SQL model which you generate. Each entity field has a <strong>Column </strong>attribute which allows you to specify if it is used for checking during an optimistic concurrency field comparison.</p>
<p>For example, you could create a Model with the following Property:</p>
<pre class="code">[<span style="color: #2b91af">Column</span>(Storage=<span style="color: #a31515">"_AddressID"</span>, UpdateCheck=<span style="color: #2b91af">UpdateCheck</span>.Never, ...
<span style="color: blue">public int </span>AddressID
...</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Notice the <strong>UpdateCheck.Never </strong>value. That means that the field will not be used in the optimistic concurrency check.</p>
<p>The documentation helps a little again:</p>
<blockquote><p>By default, when update and delete operations have been enabled, the <a href="ms-help://ms.vscc.v90/9b2f476d-7f28-2aa8-8143-3082edcf11d5.htm">LinqDataSource</a> control stores the original values for all the records in view state. The <a href="ms-help://ms.vscc.v90/9b2f476d-7f28-2aa8-8143-3082edcf11d5.htm">LinqDataSource</a> control stores values for all primary keys and all properties not marked with UpdateCheck.Never in the Column attribute. You set the UpdateCheck property of the Column attribute in the O/R Designer.</p></blockquote>
<p>Admittedly, I haven&#8217;t yet tried that, so I don&#8217;t really know how if you can change that setting for a field without changing generated code, which would be a big no-no. I&#8217;ll just have to get back to you on that.</p>
<h2>Stamp In, Please</h2>
<p>However, there is a shortcut trick.</p>
<p>Basically, if you add a field of type <strong>timestamp</strong> to your record, only that field will be used for the concurrency check, and only that field&#8217;s data will be stored in the ViewState.</p>
<blockquote><p>If the underlying data source contains a timestamp field that is automatically updated during an update, you can store only that value in view state. In that case, the timestamp property in the entity class is set to IsVersion=true and all the properties are set to UpdateCheck.Never. Because a timestamp field is automatically updated by the database every time that data in that record changes, LINQ to SQL determines from that value if data has changed. This helps reduce the size of view state, and no sensitive data is exposed. LINQ to SQL will check for data consistency by comparing the timestamp value in view state with the timestamp value in the database.</p></blockquote>
<p>The documentation is great, if you know where to find it. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>So, the answer is, if you use the <strong>LinqDataSource</strong> to throw a quick application together, make sure you use a <strong>timestamp</strong> field in your table. Call it <strong>UpdatedOn</strong>, or something. It will make the use of the <strong>LinqDataSource</strong> a lot easier, if only because you&#8217;ll sleep better not worrying about it.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F21%2Fthe-linqdatasource-and-the-hidden-viewstate%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Writing Custom Exception Classes the Quick Way</title>
		<link>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/</link>
		<comments>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 16:00:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/15/writing-custom-exception-classes-the-quick-way/</guid>
		<description><![CDATA[Until recently I thought this was a well-known feature. After demonstrating it a few times, I found out it wasn&#8217;t. A long time ago, in an cubicle far, far away, someone created the .Net Framework. To cut a long story short, they simultaneously produced guidelines for creating Exception classes, which you should always use or [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F15%2Fwriting-custom-exception-classes-the-quick-way%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Until recently I thought this was a well-known feature. After demonstrating it a few times, I found out it wasn&#8217;t.</p>
<p>A long time ago, in an cubicle far, far away, someone created the .Net Framework. To cut a long story short, they simultaneously produced guidelines for creating Exception classes, which you should always use or face having your fingernails pulled out with a staple-gun.</p>
<p>The guidelines state:</p>
<blockquote>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Arial','sans-serif'">&#8220;Use the common constructors shown in the following code example when creating exception classes. &#8220;<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">[C#]<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'">public</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">class</span> XxxException : ApplicationException<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'">{<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException() {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(<span style="color: blue">string</span> message) {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(<span style="color: blue">string</span> message, Exception inner) {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(SerializationInfo info, StreamingContext context) {&#8230;}<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'">}<o:p></o:p></span></p></blockquote>
<p><span id="more-53"></span><br />
If you don&#8217;t believe me, you can look here:</p>
<p><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp" title="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp</a></p>
<p>Now, I don&#8217;t know about you, but I can&#8217;t remember all that every time I create a custom exception class. Maybe that&#8217;s why the guidelines also state:</p>
<blockquote><p>In most cases, use the predefined exception types. Only define new exception types for programmatic scenarios, where you expect users of your class library to catch exceptions of this new type and perform a programmatic action based on the exception type itself.</p></blockquote>
<p>But let&#8217;s ignore that. It&#8217;s not relevant.</p>
<p>So, <em>did you know</em> that there is a Visual Studio code snippet that does the work for you? Here&#8217;s how you use it.</p>
<ol>
<li>Create a new class.</li>
<li>inside the namespace definition of the new class, type
<pre class="code">Exception</pre>
<p><a href="http://11011.net/software/vspaste"></a></li>
<li>Press the tab key twice.</li>
</ol>
<p>You then get the following code, free of charge:</p>
<pre class="code">[<span style="color: blue">global</span>::System.<span style="color: #2b91af">Serializable</span>]
<span style="color: blue">public class </span><span style="color: #2b91af">MyException </span>: <span style="color: #2b91af">Exception </span>{
<span style="color: green">
  </span><span style="color: blue">public </span>MyException() { }
  <span style="color: blue">public </span>MyException(<span style="color: blue">string </span>message) : <span style="color: blue">base</span>(message) { }
  <span style="color: blue">public </span>MyException(<span style="color: blue">string </span>message, <span style="color: #2b91af">Exception </span>inner) : <span style="color: blue">base</span>(message, inner) { }
  <span style="color: blue">protected </span>MyException(
  System.Runtime.Serialization.<span style="color: #2b91af">SerializationInfo </span>info,
  System.Runtime.Serialization.<span style="color: #2b91af">StreamingContext </span>context)
    : <span style="color: blue">base</span>(info, context) { }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>What&#8217;s more, you get code snippet functionality which automatically changes the names of all the constructors when you change the name of the class.</p>
<p>I wonder what other code snippets are available. Do you know of any cool ones? What are your favorites?</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F15%2Fwriting-custom-exception-classes-the-quick-way%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>.Net Source Code Now Available</title>
		<link>http://www.richardbushnell.net/2008/01/17/net-source-code-now-available/</link>
		<comments>http://www.richardbushnell.net/2008/01/17/net-source-code-now-available/#comments</comments>
		<pubDate>Thu, 17 Jan 2008 07:54:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[scott guthrie]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/17/net-source-code-now-available/</guid>
		<description><![CDATA[Scott Guthrie has just announced that the source code for the .Net framework has just been made available for reference use. It will be particularly useful to see how the controls in ASP.Net and Windows Forms have been done. Detailed instructions for how to set it up are here. One caveat: it&#8217;s not available for [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F17%2Fnet-source-code-now-available%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://weblogs.asp.net/scottgu" target="_blank">Scott Guthrie</a> has just announced that the source code for the .Net framework has <a href="http://weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx" target="_blank">just been made available for reference use</a>.</p>
<p>It will be particularly useful to see how the controls in ASP.Net and Windows Forms have been done.</p>
<p>Detailed instructions for how to set it up are <a href="http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx" target="_blank">here</a>.</p>
<p>One caveat: it&#8217;s not available for the Express editions of Visual Studio. Shame! I was just starting to have some fun with them too.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F17%2Fnet-source-code-now-available%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/17/net-source-code-now-available/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Efficient Software Development with Visual Studio Team System 2008</title>
		<link>http://www.richardbushnell.net/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/</link>
		<comments>http://www.richardbushnell.net/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 10:57:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Quaility]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Team System]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[.Net Visual Studio Team System 2008 development]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/</guid>
		<description><![CDATA[I was recently asked to do some research and give a presentation about using Visual Studio Team System 2008. I thought I&#8217;d experiment with Slideshare.net and post it here. The presentation was for a large corporation, and will possibly not come across as being very useful for an online presentation. I have been working on [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F12%2Fefficient-software-development-with-visual-studio-team-system-2008%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I was recently asked to do some research and give a presentation about using Visual Studio Team System 2008. I thought I&#8217;d experiment with Slideshare.net and post it here.</p>
<object width="400" height="328"><param name="movie" value="http://static.slideshare.net/swf/ssplayer2.swf?doc="/><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slideshare.net/swf/ssplayer2.swf?doc="  type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="328"></embed></object>
<p>The presentation was for a large corporation, and will possibly not come across as being very useful for an online presentation. I have been working on it for a while though, and I think it&#8217;s worth publishing, if only as an introduction.</p>
<p>I&#8217;ve used competitive products to Team System for a long time, mainly open source:</p>
<ul>
<li>SubVersion for source version control
<li>CruiseControl.Net as a build server
<li>NAnt for build scripts
<li>MbUnit for testing
<li>NCover for seeing my testing code coverage
<li>VersionOne for project planning, task management, tracking and reporting
<li>BugTracker for bug tracking
<li>A Wiki for project documentation and guidelines </li>
</ul>
<p>I wasn&#8217;t all-too impressed with the standard of Visual Studio Team Suite 2005, but I am a little bit happier with the 2008 version. It took a while to get it installed so that I could test it, and there were lots of seemingly undocumented points which hindered my progress, but once I&#8217;d got it up-and-running, I didn&#8217;t find it that bad. (I did have to install it on&nbsp; virtual machine, and run it on my laptop.)</p>
<p>I think the best thing about Team System is that all the things I need to do are now integrated into one place. I didn&#8217;t have to go make changes to a dozen XML files in order to get a build server working. I didn&#8217;t have to install a Wiki for the project documentation. Using Team System would also avoid resistance to using the products, as I&#8217;ve faced before trying to get other team members to use such tools. Everything would just be there, out-of-the-box, and no one would argue about using them.</p>
<p>What I didn&#8217;t like about Team System are some of the subtle details which don&#8217;t work as they should do. I did a basic test of multiple check-outs, using the new features, but it didn&#8217;t work. The Unit Tests that I generated were confusing and were buggy.</p>
<p>The biggest problem though is the price. It&#8217;s darn expensive. I wouldn&#8217;t be able to afford Team System if I was just a small development shop. However, for a large corporation, it really could make sense.</p>
<p>I actually wonder if there&#8217;s room for a better competitor (Rational is possibly the only serious contender, and I find their software pretty naff). It actually wouldn&#8217;t to be too hard for some Python hackers somewhere. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F12%2Fefficient-software-development-with-visual-studio-team-system-2008%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

