<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Write Less, Do More, &lt;br&gt;But Keep It Simple</title><link>http://weblogs.asp.net/bschooley/default.aspx</link><description>by Bernal Schooley</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Debug Build: 20510.895)</generator><item><title>Converting web site projects from Visual Studio 2008 Beta 2 to Visual Studio 2008 RTM</title><link>http://weblogs.asp.net/bschooley/archive/2007/11/21/converting-web-site-projects-from-visual-studio-2008-beta-2-to-visual-studio-2008-rtm.aspx</link><pubDate>Wed, 21 Nov 2007 15:44:38 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5305627</guid><dc:creator>bschooley</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=5305627</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=5305627</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/11/21/converting-web-site-projects-from-visual-studio-2008-beta-2-to-visual-studio-2008-rtm.aspx#comments</comments><description>&lt;p&gt;The good news is that this conversion was a lot less painful than the move from the final beta of .Net 2.0 to it's released version. The only breaking changes I ran into were minor changes in LINQ to SQL. While I was converting web site projects, I imagine since my breaking changes were limited to LINQ to SQL, that these changes would hold true for other projects as well.&lt;/p&gt;  &lt;p&gt;First, this is just one scenario, your personal experience may vary.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;LINQ to SQL Designer and the DBML File Encoding&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This change is self correcting for the most part. If you attempt to open a .dbml file or compile your app before making this change you will get a &amp;quot;Cannot load ...&amp;quot; error. When you click the &amp;quot;Ok&amp;quot; button, the dbml file will automatically be opened with the xml editor. All you need to do is click save, wait for the pause as it regenerates the classes, and then close the file. The error has to do with the utf encoding of the xml file.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Web.config Assembly Reference Change for System.Data.DataSetExtensions&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Open your web.config file and find and change the version number in this line from 2.0:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;add&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;assembly&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;System.Data.DataSetExtensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;/&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;to 3.5:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;add&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;assembly&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;then save and close.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LINQ to SQL Add and Remove Method Name Changes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;From here you can attempt to compile to find the remaining changes you need to make. The first ones you'll run into are the Add and Remove methods of the LINQ to SQL Table&amp;lt;&amp;gt; objects. The &lt;strong&gt;Add &lt;/strong&gt;method was changed to &lt;strong&gt;InsertOnSubmit &lt;/strong&gt;and the &lt;strong&gt;Remove &lt;/strong&gt;method was changed to &lt;strong&gt;DeleteOnSubmit&lt;/strong&gt;. While more wordy, the change was made to make them more explicitly describe their behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LINQ to SQL OnValidate Partial Method Change&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you were using the OnValidate partial method, then you'll need to make a small change. But this time you may want to make some alterations to your code logic as well.&lt;/p&gt;

&lt;p&gt;To put it simply, the OnValidate now has a ChangeAction parameter. This is a great change as it basically informs the method about the reason for the call. ChangeAction has four values: Insert, Update, Delete and None.&lt;/p&gt;

&lt;p&gt;So, find and change all your:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;partial&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; OnValidate()&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;to:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;partial&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; OnValidate(&lt;span style="color: rgb(43,145,175)"&gt;ChangeAction&lt;/span&gt; action)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Depending on what your doing you may want to consider wrapping the contents of your method in a simple:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (action == &lt;span style="color: rgb(43,145,175)"&gt;ChangeAction&lt;/span&gt;.Insert || action == &lt;span style="color: rgb(43,145,175)"&gt;ChangeAction&lt;/span&gt;.Update)
{&lt;br /&gt;...&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;You may want to skip that for now and revisit it when you are ready to think through the implications of this change. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My test applications made use of extension methods, nested master pages and a few other 3.5 features, but so far I haven't run into any other breaking changes. If I run across more, I'll update this blog post.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5305627" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category><category domain="http://weblogs.asp.net/bschooley/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>MSDN's 101 LINQ Samples</title><link>http://weblogs.asp.net/bschooley/archive/2007/10/25/msdn-s-101-linq-samples.aspx</link><pubDate>Thu, 25 Oct 2007 16:24:27 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4744429</guid><dc:creator>bschooley</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=4744429</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=4744429</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/10/25/msdn-s-101-linq-samples.aspx#comments</comments><description>&lt;p&gt;I've been reading about, studying, and using LINQ to SQL for a couple months now and after all this time I somehow never ran across this MSDN web page.&lt;/p&gt;  &lt;h4&gt;&lt;a href="http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx"&gt;101 LINQ Samples&lt;/a&gt; - C# Version&lt;/h4&gt;  &lt;h4&gt;&lt;a href="http://msdn2.microsoft.com/en-us/bb688088.aspx"&gt;101 LINQ Samples&lt;/a&gt; - VB Version (has lots more content and sections on LINQ to SQL, etc)&lt;/h4&gt;  &lt;p&gt;After just a few minutes of digging through it's samples, I've already learned more than I care to admit. These samples help to show that LINQ is a lot deeper and more expressive than you might think at first glance.&lt;/p&gt;  &lt;p&gt;[Update: 10-26]&lt;/p&gt;  &lt;p&gt;After some additional searches and aimless browsing on the MSDN site I discovered there are multiple flavors and versions of the &amp;quot;101 LINQ Samples&amp;quot;. I've updated the links above to include what I think are the most current pages.&lt;/p&gt;  &lt;p&gt;Here are some other links to similar &amp;quot;101 LINQ Samples&amp;quot; pages that I think are out of date but may still have nuggets of value buried in them:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/adonet/archive/2007/03/13/101-linq-samples-updated.aspx"&gt;101 LINQ to Entities Samples&lt;/a&gt; - Downloadable Zip from the ADO.Net Team (out-of-date)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2007/03/04/samples-update.aspx"&gt;LINQ Samples from Orcas March CTP&lt;/a&gt; - Another Downloadable Zip (out-of-date)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://download.microsoft.com/download/8/0/e/80e3f901-9bcc-4b8b-ba66-c49e24559fbc/vcs_samples_vs2008_beta2_02.exe"&gt;LINQ and language samples for Visual Studio 2008 Beta 2&lt;/a&gt; - Executable samples package from &lt;a href="http://msdn2.microsoft.com/en-us/vbasic/bb330936.aspx"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4744429" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category></item><item><title>ASP.Net : Using a STRUCT to make working with ViewState easier</title><link>http://weblogs.asp.net/bschooley/archive/2007/10/09/asp-net-using-a-struct-to-make-working-with-viewstate-easier.aspx</link><pubDate>Tue, 09 Oct 2007 15:31:57 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4510558</guid><dc:creator>bschooley</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=4510558</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=4510558</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/10/09/asp-net-using-a-struct-to-make-working-with-viewstate-easier.aspx#comments</comments><description>&lt;p&gt;Last week I wrote an &lt;a href="http://weblogs.asp.net/bschooley/archive/2007/09/29/asp-net-clean-up-your-use-of-viewstate.aspx" target="_blank"&gt;article&lt;/a&gt; suggesting cleaner ways of using ViewState to store local page variable state. I got some great feedback on that post and this blog is an extension on the topic that I came up with while experimenting with the various techniques brought up.&lt;/p&gt;  &lt;p&gt;One of the things I look for in page level coding techniques is simplicity. &amp;quot;Write less, do more&amp;quot;, but keep it simple. By simple, I mean easy to write and easy for someone else to read and maintain.&lt;/p&gt;  &lt;p&gt;One of the challenges of using ViewState in a simple and efficient way is that it can get wordy. What I mean is with most approaches you end up typing in the variable name three or four times to set up a local variable and an associated property or you set up a local variable and then save and load it during SaveViewState and LoadViewState. All in all, for every variable you end up with multiple lines of declarative code. &lt;/p&gt;  &lt;p&gt;After poking it over and over I came up with the idea to use a struct to remove the need for multiple lines of code per variable. Here's the result:&lt;/p&gt;  &lt;div&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;[Serializable()]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;struct&lt;/span&gt; ViewStateStruct&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; EmployeeID;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;}&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; ViewStateStruct _vs = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ViewStateStruct();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; SaveViewState() &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; { &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.ViewState.Add(&lt;span style="color: #006080"&gt;&amp;quot;_vs&amp;quot;&lt;/span&gt;, _vs); &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.SaveViewState(); }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; LoadViewState(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; savedState) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; { &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.LoadViewState(savedState); _vs = (ViewStateStruct)ViewState[&lt;span style="color: #006080"&gt;&amp;quot;_vs&amp;quot;&lt;/span&gt;]; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;As you can see, if you want to add a variable, you only have to declare it in one place inside the struct. Throughout your code all your ViewState variables are simply referenced by _vs.EmployeeID. If you don't like _vs, then it's easy to change it. Either way, be sure to set a standard name for the struct throughout your project.&lt;/p&gt;

&lt;p&gt;In my previous blog on the topic, Tony pointed out an article on CodeProject called &lt;a href="http://www.codeproject.com/aspnet/PersistAttribute.asp" target="_blank"&gt;Using Attributes for encapsulating ASP.Net Session and ViewState variables&lt;/a&gt;. Combining this with the struct idea yields you with the following code in your page.&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;[Serializable()]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;struct&lt;/span&gt; ViewStateStruct&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; EmployeeID;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;}&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;[PersistField(Location = PersistLocation.ViewState)]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; ViewStateStruct _vs = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ViewStateStruct();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;On the general topic of &amp;quot;write less, do more&amp;quot;, there's one other thing I remembered while playing with this and Linq to SQL in the same page. I found when I went to load data from my Linq object to my form fields, I also needed to store the same value in my ViewState variable so that when the page came back, I'd have access to the original value. In C# you can do this:&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;tbName.Text = _vs.Name = employee.Name;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;I seldom remember that but it's pretty useful.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4510558" width="1" height="1"&gt;</description></item><item><title>Impressions of Silverlight after attending SilverlightDevCamp Chicago</title><link>http://weblogs.asp.net/bschooley/archive/2007/09/30/impressions-of-silverlight-after-attending-silverlightdevcamp-chicago.aspx</link><pubDate>Sun, 30 Sep 2007 13:15:24 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4255759</guid><dc:creator>bschooley</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=4255759</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=4255759</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/09/30/impressions-of-silverlight-after-attending-silverlightdevcamp-chicago.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bschooley/WindowsLiveWriter/SilverlightDevCampChicagoWrapup_116F6/SilverlightDevCampChicago.jpg"&gt;&lt;img id="id" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 0px 15px; border-right-width: 0px" height="184" alt="SilverlightDevCampChicago" src="http://weblogs.asp.net/blogs/bschooley/WindowsLiveWriter/SilverlightDevCampChicagoWrapup_116F6/SilverlightDevCampChicago_thumb.jpg" width="244" align="right" border="0" /&gt;&lt;/a&gt; I just spent Friday night and Saturday at the &lt;a href="http://www.barcamp.org/SilverlightDevCampChicago" target="_blank"&gt;SilverlightDevCamp&lt;/a&gt; in Chicago. I'm glad I went as it gave me a chance to think for a couple days about Silverlight and other related topics. I knew a bit about Silverlight before going as I've been following it for a while now, but I admit I haven't written anything in it yet. Overall, the sessions were pretty good given the nature of this type of event is to have everything presented by the people attending it rather than bring someone in to present on a topic and have everyone else listen.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;So what are my impressions of Silverlight now?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Well, it really reminds me of the early days of java applets in MCSA Mosaic. When everyone was rushing to make news tickers and couldn't think of much else to do with it. &lt;em&gt;(And yes, I was there and actually wrote one of the first ticker applets used in production on the web.)&lt;/em&gt; Obviously it took some time for people to get past that stage and it took time for the technology to mature enough to allow more interesting things to be done. &lt;/p&gt;  &lt;p&gt;I have no doubt that Silverlight will be a big success and I personally think it will be bigger than most people realize. The folks at Microsoft that are behind it's development undoubtedly realize it too but so far they aren't selling it as the &amp;quot;next significant technology move for web delivered applications&amp;quot; that it has the potential to become.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Why do I think that?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;1. It leverages the .Net framework as the development platform, and let's face it, there are millions of .Net developers who will soon be able to create rich applications that delivered and live in the browser. &lt;em&gt;(Of course I'm speaking of Silverlight 1.1, 1.0 is a technology teaser destined to be replaced by the real Silverlight.)&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;2. It's really going to be cross platform (Windows, Mac AND Linux). It is worth noting that this is the first time Microsoft has ever blessed and supported a development effort for Linux.&lt;/p&gt;  &lt;p&gt;3. It does away with the compatibility problems we currently face with developing rich applications in html and javascript that need to support multiple browsers on multiple operating systems.&lt;/p&gt;  &lt;p&gt;4. Like it or not... accept it or not... richer applications are going to be future. By rich applications I'm speaking more of the improved user experience they provide and not the pretty bells and whistles. It may not be as clearly delineated as the move from green screens to windows style interfaces, but there will be a clear difference html driven websites of today and the Silverlight driven websites of tomorrow.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;When will this happen?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;It's going to take time for this to happen and it could implode on itself along the way. First we'll see early adopters scrambling to make something of it, then slowly it will move into the main stream. It's hard to tell how fast this will happen, but my guess is that we're looking at one to two years before it really gets started and another year or two before users start asking for it. During this period, several hurdles must be overcome and competing technologies such as Adobe's Flex/Air could speed or slow the process.&lt;/p&gt;  &lt;p&gt;Well, that's my take on it. I could be completely wrong about everything I've written here so far, but at least on this next note, I know I'm right... Silverlight is definitely something worth keeping an eye on and if you're a .Net developer it will be worth your time to learn more about it. I'll even go one step further and say if you are a .Net developer you should plan to learn to how to develop with it.&lt;/p&gt;  &lt;p&gt;For now, keep moving forward with what you're doing, but prepare yourself for the changes ahead.&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:52de9142-0008-48f3-986b-cf4f09e7d207" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Flickr Tags:  		&lt;a href="http://flickr.com/photos/tags/Silverlight" rel="tag"&gt;Silverlight&lt;/a&gt; 		,  		&lt;a href="http://flickr.com/photos/tags/DevCamp" rel="tag"&gt;DevCamp&lt;/a&gt; 		,  		&lt;a href="http://flickr.com/photos/tags/SilverlightDevCamp" rel="tag"&gt;SilverlightDevCamp&lt;/a&gt; 		&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4255759" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>LINQ to SQL : Handling disconnected CRUD operations in ASP.Net</title><link>http://weblogs.asp.net/bschooley/archive/2007/09/29/linq-to-sql-handling-disconnected-crud-operations-in-asp-net.aspx</link><pubDate>Sat, 29 Sep 2007 21:37:13 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4248179</guid><dc:creator>bschooley</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=4248179</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=4248179</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/09/29/linq-to-sql-handling-disconnected-crud-operations-in-asp-net.aspx#comments</comments><description>&lt;p&gt;Most LINQ to SQL examples you find on the net tend to be written in a way that assumes a connected environment. I've found some blogs and posts that also talk about disconnected operations, but many of those are coming from the perspective of multiple tiers where the entity objects are being passed between tiers. &lt;/p&gt;  &lt;p&gt;For this example, I've got a page that handles simple CRUD operations for a single employee record. It takes a query string parameter of &amp;quot;id&amp;quot;. When it's not passed or &amp;quot;0&amp;quot; is passed, then it assumes you are going to be adding a new employee record, otherwise it loads the employee and allows you to edit it.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Note: I'm using module (page) level variables that are read from and stored in ViewState by overridden SaveViewState and LoadViewState. See my blog &lt;a href="http://weblogs.asp.net/bschooley/archive/2007/09/29/asp-net-clean-up-your-use-of-viewstate.aspx" target="_blank"&gt;post on the subject&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Here's the OnLoad method... &lt;/p&gt;  &lt;div&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnLoad(EventArgs e)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!IsPostBack)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; _EmployeeID = Convert.ToInt32(Request.QueryString[&lt;span style="color: #006080"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;] ?? &lt;span style="color: #006080"&gt;&amp;quot;0&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_EmployeeID != 0)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; DB.NW.DataContext dc = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.DataContext();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; DB.NW.Employee employee = dc.Employees.Single(em =&amp;gt; em.EmployeeID == _EmployeeID);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; _Version = employee.Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; tb_FirstName.Text = employee.FirstName;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; tb_LastName.Text = employee.LastName;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; tb_HomePhone.Text = employee.HomePhone;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.OnLoad(e);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;I'm going to assume you seen the reading of a single record from a LINQ to SQL DataContext, if not Scott Guthrie has a good &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx" target="_blank"&gt;introduction to LINQ to SQL&lt;/a&gt; you should read first. I'm also using the null coalescing operator, if that's new to you then again, Scott &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/09/20/the-new-c-null-coalescing-operator-and-using-it-with-linq.aspx" target="_blank"&gt;covered that too&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Notice here that I'm storing two values to local variables (ie ViewState). The _EmployeeID and the _Version. The version field is a row TimeStamp field I've added to the Employee table to facilitate concurrency checking on updates. I'll talk more about this later in this post.&lt;/p&gt;

&lt;p&gt;Now for the good stuff. Here's the Save method...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Save(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; source, EventArgs e)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; DB.NW.DataContext dc = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.DataContext();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; DB.NW.Employee employee = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.Employee();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_EmployeeID == 0)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; dc.Employees.Add(employee);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; employee.EmployeeID = _EmployeeID;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; employee.Version = _Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; dc.Employees.Attach(employee);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; dc.SubmitChanges();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; Response.Redirect(&lt;span style="color: #006080"&gt;&amp;quot;~/Employees.aspx&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;catch&lt;/span&gt; (Exception ex)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; {&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; lbl_Message.Text = ex.Message;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Let's break it down. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;When adding a new employee record.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Start by creating a DataContext and a new Employee object. Then set the properties based on the form control values and &amp;quot;Add&amp;quot; the employee object to the DataContext's Employees table. Finish with a SubmitChanges().&lt;/p&gt;

&lt;p&gt;&lt;em&gt;When updating things get a bit more interesting.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Start by setting the id and version properties of the new employee object and then &amp;quot;Attach&amp;quot; the it to the DataContext's Employees table. After it is attached, move the form control values to the employee object and finish by calling SubmitChanges().&lt;/p&gt;

&lt;p&gt;It is important to understand why this sequence of steps is required for an update. The attach step is the key and it relates to how the DataContext tracks changes to objects. The &amp;quot;Attach&amp;quot; tells the DataContext that we have this employee object with an id of _EmployeeID and a row version timestamp of _Version and we want you to know about it and track any further changes so you can save them. When calling SubmitChanges(), the DataContext will create it's update statement and only include fields that changed after the object was attached.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variations on the Update&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While the code above is a complete workable approach, there are some assumptions and pre-requisites that led to it. First, the table has a timestamp field to track the row version for concurrency checking. If your table doesn't have one, in most cases I'd suggest adding it. If that's not possible then you have a few options, all of which involve more work on your part. &lt;/p&gt;

&lt;p&gt;The easiest option is to turn off concurrency checking by changing the &amp;quot;update check&amp;quot; attribute for each field and setting it to &amp;quot;never&amp;quot;. By default, LINQ to SQL sets the attribute to &amp;quot;always&amp;quot;. This will allow the above code above to continue to work (just remove the lines dealing with the Version field). But removing concurrency checking isn't a good idea unless you intend to handle it yourself by adding your own code for it to the update process above.&lt;/p&gt;

&lt;p&gt;What you shouldn't do is simply re-retrieve the employee record and change its properties. If you are handling the currency checking on your own via a property by property compare or a last updated date time value, you may think you are covered. The problem is that you are either trying to ignore concurrency checking or you are doing your own concurrency checking while still allowing LINQ to SQL to do it's concurrency checks as well so you may get a LINQ to SQL concurrency error if two users try to update a record a the same time, even if you are doing some amount of your own checking. It the end you either need to use LINQ to SQL's concurrency process or to turn it off.&lt;/p&gt;

&lt;p&gt;Here's a bit more food for thought on this subject. Below are approaches to updates and the SQL statements generated by LINQ to SQL.&lt;/p&gt;

&lt;p&gt;First, the code from above that makes use of a Version field and LINQ to SQL's concurrency checking...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;DB.NW.Employee employee = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.Employee();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.EmployeeID = _EmployeeID;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.Version = _Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;dc.Employees.Attach(employee);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;dc.SubmitChanges();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;...generates...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;UPDATE [dbo].[Employees]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;SET [LastName] = @p2, [FirstName] = @p3, [HomePhone] = @p4&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;WHERE ([EmployeeID] = @p0) AND ([Version] = @p1)&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Turning off update checks on your fields and removing the Version field leaves you vulnerable to concurrency issues, but here's the code...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;DB.NW.Employee employee = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.Employee();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.EmployeeID = _EmployeeID;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;dc.Employees.Attach(employee);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;dc.SubmitChanges();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;...generates...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;UPDATE [dbo].[Employees]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;SET [LastName] = @p1, [FirstName] = @p2, [HomePhone] = @p3&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;WHERE [EmployeeID] = @p0&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;And last, if you leave the Update Check on and re-retrieve the employee record the code ...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee = dc.Employees.Single(em =&amp;gt; em.EmployeeID == _EmployeeID);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;dc.SubmitChanges();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;...generates... &lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;UPDATE [dbo].[Employees]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;SET [HomePhone] = @p15&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;WHERE ([EmployeeID] = @p0) AND ([LastName] = @p1) AND ([FirstName] = @p2) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;AND ([Title] = @p3) AND ([TitleOfCourtesy] = @p4) AND ([BirthDate] = @p5) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;AND ([HireDate] = @p6) AND ([Address] = @p7) AND ([City] = @p8) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;AND ([Region] IS NULL) AND ([PostalCode] = @p9) AND ([Country] = @p10) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;AND ([HomePhone] = @p11) AND ([Extension] = @p12) AND ([ReportsTo] = @p13) &lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;AND ([PhotoPath] = @p14)&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the last of the three update statements, LINQ to SQL is sending every field as part of the update statement's where clause. I'll also point out here that in this case, LINQ to SQL does know that the FirstName and LastName fields were not changed to new values and therefore didn't include those in the update.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Note on Change Tracking&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a more advanced and situational concept, but one of the interesting possibilities that LINQ to SQL enables is the ability to build change tracking into the SubmitChanges() process. The basic idea is that any time you make changes to the database, you first go through the list of changes being made and record them in a generic change log table. I'll be writing about this idea in a later post, but for now, I'll throw out a warning that may effect how you handle updates. &lt;/p&gt;

&lt;p&gt;When using the model I suggest at the beginning of this post, the DataContext doesn't know the original values of the three fields (FirstName, LastName and HomePhone) that are being set. As far as the DataContext is aware, the original values are null so, even if nothing is really changing, the fields will be added to the change set. This means when you go through the change set during SubmitChanges() the original values will be empty. So, if you want to really know, at a field level what is truly changing, you'll need to take a different approach that what is shown above. &lt;/p&gt;

&lt;p&gt;Here I've stored the original values for all fields that can be updated and I set the employee object to those values before attaching it to the Employees table.&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;DB.NW.Employee employee = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DB.NW.Employee();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.EmployeeID = _EmployeeID;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.Version = _Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.FirstName = _FirstName;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.LastName = _LastName;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.HomePhone = _HomePhone;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;dc.Employees.Attach(employee);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.FirstName = tb_FirstName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;employee.LastName = tb_LastName.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;employee.HomePhone = tb_HomePhone.Text;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;dc.SubmitChanges();&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;...generates...&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;UPDATE [dbo].[Employees]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;SET [HomePhone] = @p2&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;WHERE ([EmployeeID] = @p0) AND ([Version] = @p1)&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;In this case, LINQ to SQL knows the original values for the properties and as in the third example I gave earlier it generates the update set statement with the HomePhone, which was the only field that changed. During the SubmitChanges, if you look into the ChangeSet and retrieve the list of changed properties, you'll find that only the HomePhone is given and it has the original and new values. This will enable you to record the changes in your generic change log table.&lt;/p&gt;

&lt;p&gt;So, in order to do both change tracking at the field level and concurrency checking in a disconnected environment such as ASP.Net, you need to be able to create your employee object and set all the properties that you may be changing to their original values before you attach and then update those properties.&lt;/p&gt;

&lt;p&gt;I'll be writing a post later that goes into more detail and presents the pros and cons on a few different approaches to tracking original values for objects that you will be updating. &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4248179" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category><category domain="http://weblogs.asp.net/bschooley/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>ASP.Net : Clean up your use of ViewState</title><link>http://weblogs.asp.net/bschooley/archive/2007/09/29/asp-net-clean-up-your-use-of-viewstate.aspx</link><pubDate>Sat, 29 Sep 2007 16:37:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4246119</guid><dc:creator>bschooley</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=4246119</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=4246119</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/09/29/asp-net-clean-up-your-use-of-viewstate.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note: After reading this article, be sure to check out the followup article, &amp;quot;&lt;/strong&gt;&lt;/em&gt;&lt;a href="http://weblogs.asp.net/bschooley/archive/2007/10/09/asp-net-using-a-struct-to-make-working-with-viewstate-easier.aspx"&gt;&lt;em&gt;&lt;strong&gt;Using a STRUCT to make working with ViewState easier&lt;/strong&gt;&lt;/em&gt;&lt;/a&gt;&lt;em&gt;&lt;strong&gt;&amp;quot;, where I take the ideas here and go one step further.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;This is a big pet peeve of mine, so bear with me. I'll keep it short and to the point.&lt;/p&gt;  &lt;p&gt;I'm a stickler for organized readable code and the use of ViewState is one of those things that can and should be standardized.&lt;/p&gt;  &lt;p&gt;Typically you see developers directly access ViewState in the middle of doing other things. When you use this approach it means that when someone else reads through the code to maintain or enhance it, they have to look through all the code to see what is being placed in ViewState in order to know what they have to work with.&lt;/p&gt;  &lt;p&gt;Let's jump right to my suggested approach and look at the code...&lt;/p&gt;  &lt;div&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; _EmployeeID = 0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] _Version;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;#xA0;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; SaveViewState()&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.ViewState.Add(&lt;span style="color: #006080"&gt;&amp;quot;_EmployeeID&amp;quot;&lt;/span&gt;, _EmployeeID);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.ViewState.Add(&lt;span style="color: #006080"&gt;&amp;quot;_Version&amp;quot;&lt;/span&gt;, _Version);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.SaveViewState();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;}&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; LoadViewState(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; savedState)&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.LoadViewState(savedState);&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; _EmployeeID = (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;)ViewState[&lt;span style="color: #006080"&gt;&amp;quot;_EmployeeID&amp;quot;&lt;/span&gt;];&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; _Version = (Byte[])ViewState[&lt;span style="color: #006080"&gt;&amp;quot;_Version&amp;quot;&lt;/span&gt;];&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Place the private variable declarations at the top of your module. You can follow immediately with the save and load overrides or move them elsewhere. I like having the save and load at the bottom of my code because I like to have my OnLoad be the first method in my code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why does it matter?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1. As I mentioned above it makes it easier for someone else to read your code and know what module level variables are being used. &lt;em&gt;(Be sure to use a module level variable naming convention such as the _ prefix as well.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;2. ViewState is read once and written to once per variable rather than reading (and casting) it every time it's used in your code.&lt;/p&gt;

&lt;p&gt;3. Because it makes you look like a professional. Seriously, take time to write clean organized code that follow standards and best practice approaches even when the module or page is very small.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;u&gt;[Edited 10/2/2007]&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Alternate Approaches Suggested in the Comments Below&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I like feedback and this post has some great feedback. My favorite, which honestly blew me away, is the attribute technique suggested by Tony. The full details can be found in the Code Project article, &lt;a href="http://www.codeproject.com/aspnet/PersistAttribute.asp" target="_blank"&gt;Using Attributes for encapsulating ASP.Net Session and ViewState variables&lt;/a&gt;. Declaring a variable on a page that is to be stored in ViewState is as simple as:&lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;[PersistField(Location = PersistLocation.ViewState)]&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; _EmployeeID;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;To enable this you need to override the page class and create a custom attribute. If you already override the page class in your project then moving to this technique is almost a no-brainer.&lt;/p&gt;

&lt;p&gt;What's so elegant about this besides the reduced about of code required, is that what you're doing is completely obvious since the variable is declared at the same place where you flag it for persistence via ViewState.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Another Approach&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Michael Teper suggested the property approach. &lt;/p&gt;

&lt;div&gt;
  &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; _EmployeeId&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt; get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;) ViewState[&lt;span style="color: #006080"&gt;&amp;quot;_EmployeeId&amp;quot;&lt;/span&gt;]; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt; set { ViewState[&lt;span style="color: #006080"&gt;&amp;quot;_EmployeeId&amp;quot;&lt;/span&gt;] = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;; }&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#x27;Courier New&amp;#x27;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;If the attribute approach doesn't work for you, then this is probably the best way to handle ViewState, I've used it myself, but for some reason I've never changed my default coding approach to it. While this potentially reads values from ViewState multiple times, it doesn't do any reads if you don't use the property.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4246119" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>LINQ to SQL : Start with a good foundation by defining a custom DataContext</title><link>http://weblogs.asp.net/bschooley/archive/2007/09/28/linq-to-sql-start-with-a-good-foundation-by-defining-a-custom-datacontext.aspx</link><pubDate>Fri, 28 Sep 2007 05:43:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4224356</guid><dc:creator>bschooley</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/rsscomments.aspx?PostID=4224356</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bschooley/commentapi.aspx?PostID=4224356</wfw:comment><comments>http://weblogs.asp.net/bschooley/archive/2007/09/28/linq-to-sql-start-with-a-good-foundation-by-defining-a-custom-datacontext.aspx#comments</comments><description>&lt;P&gt;This is my first post in a what will become a series of posts on LINQ to SQL. As the title of my blog suggests, I'm going to be "thinking out loud" as I write these and I'm hoping for feedback from others on the pro's and con's of the approaches I'm suggesting. That said, the point of this series of posts will be to define a loose set of best practices around developing with LINQ to SQL in an ASP.Net web environment. The first couple posts will be pretty simplistic but I wanted to get these in place first so I can refer to the concepts later without having to go into detail.&lt;/P&gt;
&lt;P&gt;On with the topic at hand...&lt;/P&gt;
&lt;P&gt;When starting a new project with LINQ to SQL, it's a good idea to wrap the DataContext with your own class that inherits from the DataContext generated by the designer (or SQLMetal).&lt;/P&gt;
&lt;P&gt;Before we do that though, let's take a small step back and talk about namespaces. In the designer, if you click on design surface background, you'll select the DataContext. In the properties window there will be two entries related to namespaces. One is the Context Namespace and the other is the Entity Namespace. Typically you'd put both the generated DataContext and the entity classes within the same namespace, but since we're going to be creating our own DataContext that inherits from the one that is generated for us, I'd suggest that we set these up with different namespaces.&lt;/P&gt;
&lt;P&gt;I like to have my data layer to have its own namespace and as such I tend to go with a DB.such_and_such namespace where such_and_such is the name of our database or other logical context name. I'll be using the northwind database for this series of posts, so the logical namespace name is DB.NW. This is what we should put in the Entity Namespace property, but for the Context Namespace, we should set the value to DB.NW.Base to leave room for our custom version in the DB.NW namespace. With our DataContext's name set as simply DataContext as well, we'll get the following generated for us by the designer...&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;namespace&lt;/FONT&gt; DB.NW.Base &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ... &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;public partial class&lt;/FONT&gt; DataContext : System.Data.Linq.DataContext &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ... &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;} &lt;BR&gt;&lt;FONT color=#0000ff&gt;namespace&lt;/FONT&gt; DB.NW &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ... &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Table...] &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;public partial class&lt;/FONT&gt; table_name... etc. &lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;Next we need to create a new class file in app_code. In this case I'll keep the name the same as the namespace it contains and go with DB.NW.cs. &lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System; &lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Data.Linq; &lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Data.Linq.Mapping; &lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Linq; &lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Runtime.Serialization; &lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Reflection; &lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;namespace&lt;/FONT&gt; DB.NW &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;public class&lt;/FONT&gt; DataContext : DB.NW.Base.DataContext &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;Now, it may seem silly at this point to go through all this but doing this now, even with no custom or overridden methods, will save us from having to change our code later that when we need to override methods such as SubmitChanges()..&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4224356" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bschooley/archive/tags/LINQ+to+SQL/default.aspx">LINQ to SQL</category></item></channel></rss>