<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-519334379594158959</atom:id><lastBuildDate>Mon, 07 Oct 2024 06:03:18 +0000</lastBuildDate><title>{elsharpo}</title><description>IAdaptable</description><link>http://elsharpo.blogspot.com/</link><managingEditor>noreply@blogger.com (elsharpo)</managingEditor><generator>Blogger</generator><openSearch:totalResults>16</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-3341748068311690359</guid><pubDate>Tue, 05 May 2009 23:22:00 +0000</pubDate><atom:updated>2009-05-06T09:22:44.153+10:00</atom:updated><title>Damn, Where did my isolated storage’d file go?</title><description>&lt;p&gt;LOL, I was doing some work writing some data to IsolatedStorage. Loaded the app, loaded some data that also was supposed to be copied to IsolatedStorage and then I though, cool, no errors, let’s look at that file! ;-)&lt;/p&gt;  &lt;p&gt;Problem being that I could not file the little bastard, I look where where the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/3ak841sy.aspx&quot; target=&quot;_blank&quot;&gt;MSDN&lt;/a&gt; article tells you to .. (below) and couldn’t find it either. (MSDN says look for Microsoft/IsolatedStorage)&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1P9IXnmHfngOtG5fTvb17x3bRUcCDfB40iDlIM-aND1JRbXnxI1fNx-JxBT54iab9VhqfFIzrbZ7hw_wHzaXqMGIBxYGqJHvI85Isn4Rv-yeY2xO1Rx1VwZZvDjtCi2a3kOh_1_k3O-3R/s1600-h/image4.png&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;179&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikn5SuKv0dTYMC6YOrQJKBt0PSC4AWAjIOwsuNJ8frlbi19fCXvkXvXhymWhAdI2VmgjJpnWBEQ_U7_TNnngCg4IbVAO0G4Rbb-lYO8uJFzsSN_tZ2C7kIMou0d52cVyRGJIxUbXBnSwrk/?imgmax=800&quot; width=&quot;911&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I couldn’t find it.. &lt;/p&gt;  &lt;p&gt;Ok, so I went to trusty? old search in WindowsXP and got it, the trick was to search with “Search hidden files and folders” ticked…&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhi9rp1MqHtQMNd1YPkhDD76TR6xAocZGpa_JYfZ9hgrFeqFzl8L9DDNLecLfJNfTjQu2V0_9wdZP1EoTzDqGwRG2A6YE825OUXJIo7Nqd7fcXfShgF7PJ6nmGZ2I5PB_NDeKaNhPn7MgD5/s1600-h/image9.png&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;385&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidBV_OKbkw4yeOVvzzxLF6dv65MnCNvO0SexNQKyytgaxdI5WkWUoLIZdMCQLozzpv76D4nCYbsaP9JWqhsp13ebWzp3_lulhFgYvhzc7XrBGem0E1A-_dZXWiMM7UOjc-045dW9TYLsl1/?imgmax=800&quot; width=&quot;192&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;End result after 2 days..&lt;/p&gt;  &lt;p&gt;Note that the MSDN article was correct! but since I don’t always read instructions I couldn’t find it at first!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgX4CiCC6Mh6qimlqwRRQd_6PW2UIVSBXInMXYujRPMV7SlVv6RKfH7mc5igpSqSNWkJ55T9ROtq9ebKED0rVyaLCRzscZY9Li2YMxzWtFmuH72h4cbtXEbeX0SY5SZud4pV1SMUcsHWW9_/s1600-h/image14.png&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;94&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNfhdUw8zVEzYC3pVescDEQeySTANot4ovnnh-THh_fMgYXqBwgN-IblJKdldiZ82JplBw80EsJkW56o2qnXzLU_lrB4PL_8Y6G-MOiiOH-K5t4WOCKeswMPaxOegzRRZgPeB2nxHs0NeA/?imgmax=800&quot; width=&quot;1045&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  </description><link>http://elsharpo.blogspot.com/2009/05/damn-where-did-my-isolated-storaged.html</link><author>noreply@blogger.com (elsharpo)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikn5SuKv0dTYMC6YOrQJKBt0PSC4AWAjIOwsuNJ8frlbi19fCXvkXvXhymWhAdI2VmgjJpnWBEQ_U7_TNnngCg4IbVAO0G4Rbb-lYO8uJFzsSN_tZ2C7kIMou0d52cVyRGJIxUbXBnSwrk/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-4296032487820096561</guid><pubDate>Fri, 24 Apr 2009 01:31:00 +0000</pubDate><atom:updated>2009-04-24T11:31:04.521+10:00</atom:updated><title>Sharepoint Web Services: creating Lists</title><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Just felt like posting this snippet that shows how to create a List in Sharepoint using the web service that SP provides. Those web services could be found at &lt;a title=&quot;http://superDomain/sites/elSite/_vti_bin/Lists.asmx&quot; href=&quot;http://superDomain/sites/elSite/_vti_bin/Lists.asmx&quot;&gt;hhtp://superDomain/sites/elSite/_vti_bin/Lists.asmx&lt;/a&gt;. Once you add the service reference to your project and create the proxy, the function of interest here is AddList() also how you define your InnerXml property for the new fields node is important and easy to make a mistake with. So as always with Sharepoint is very trial and errorish at least for me (scarred for ever by SP)&lt;/p&gt;  &lt;p&gt;I hope this snippet stands the test of time in this blog post and I can easily find it one day, or if I can help some poor soul traveling through the Sharepoint ‘realms* the better/ &lt;/p&gt;  &lt;p&gt;More information can be found in MSDN at&amp;#160; &lt;a title=&quot;http://msdn.microsoft.com/en-us/library/lists.lists.addlist.aspx&quot; href=&quot;http://msdn.microsoft.com/en-us/library/lists.lists.addlist.aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/lists.lists.addlist.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div id=&quot;codeSnippetWrapper&quot; style=&quot;border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4; text-align: left; max-height: 200px&quot;&gt;   &lt;div id=&quot;codeSnippet&quot; style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; CreateList()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     var listService = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Lists&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;       Url = &lt;span style=&quot;color: #006080&quot;&gt;@&amp;quot;http://superDomain/sites/elSite/_vti_bin/Lists.asmx&amp;quot;&lt;/span&gt;,&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;       Credentials = CredentialCache.DefaultCredentials&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     &lt;span style=&quot;color: #0000ff&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;string&lt;/span&gt; listName = &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Web Service Created&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     var listCreatedAsXmlNode = listService.AddList(listName, &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Web Service Created List Description&amp;quot;&lt;/span&gt;, 100);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     var listRetrieved = listService.GetList(listName);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     XmlNode version = listRetrieved.Attributes[&lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Version&amp;quot;&lt;/span&gt;]; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     var xmlDoc = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; XmlDocument();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     var titleAttrib = (XmlAttribute)xmlDoc.CreateNode(XmlNodeType.Attribute, &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Title&amp;quot;&lt;/span&gt;, String.Empty);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     var descriptionAttrib = (XmlAttribute)xmlDoc.CreateNode(XmlNodeType.Attribute, &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Description&amp;quot;&lt;/span&gt;, String.Empty);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     titleAttrib.Value = listName; &lt;span style=&quot;color: #008000&quot;&gt;// Can use this to update it&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     &lt;span style=&quot;color: #008000&quot;&gt;//descriptionAttrib.Value = &amp;quot;!&amp;quot; ;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     var properties = xmlDoc.CreateNode(XmlNodeType.Element, &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;List&amp;quot;&lt;/span&gt;, String.Empty);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     properties.Attributes.Append(titleAttrib);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     properties.Attributes.Append(descriptionAttrib); &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     var newFields = xmlDoc.CreateNode(XmlNodeType.Element, &lt;span style=&quot;color: #006080&quot;&gt;&amp;quot;Fields&amp;quot;&lt;/span&gt;, String.Empty);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     newFields.InnerXml =&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     &lt;span style=&quot;color: #006080&quot;&gt;@&amp;quot;&amp;lt;Method ID=&#39;1&#39;&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;       &amp;lt;Field Type=&#39;DateTime&#39; DateOnly=&#39;TRUE&#39; DisplayName=&#39;My Date Field&#39; FromBaseType=&#39;TRUE&#39; Required=&#39;TRUE&#39; Description=&#39;some text description&#39;/&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     &amp;lt;/Method&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     &amp;lt;Method ID=&#39;2&#39;&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;       &amp;lt;Field Type=&#39;Number&#39; MAX=&#39;10&#39; DisplayName=&#39;NumberColumn&#39; FromBaseType=&#39;TRUE&#39; Required=&#39;TRUE&#39; Description=&#39;This is a Number&#39; /&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     &amp;lt;/Method&amp;gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     &amp;lt;Method ID=&#39;3&#39;&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;       &amp;lt;Field Type=&#39;Calculated&#39; DisplayName=&#39;MyCalcField&#39; ResultType=&#39;Text&#39;&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;         &amp;lt;Formula&amp;gt;=Title&amp;amp;amp;NumberColumn&amp;lt;/Formula&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;           &amp;lt;FormulaDisplayNames&amp;gt;=Title&amp;amp;amp;NumberColumn&amp;lt;/FormulaDisplayNames&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;           &amp;lt;FieldRefs&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;             &amp;lt;FieldRef Name=&#39;Title&#39;/&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;             &amp;lt;FieldRef Name=&#39;NumberColumn&#39;/&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;            &amp;lt;/FieldRefs&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;           &amp;lt;/Field&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;       &amp;lt;/Method&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;       &amp;lt;Method ID=&#39;4&#39;&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;         &amp;lt;Field ReadOnly=&#39;TRUE&#39; Type=&#39;Counter&#39; PrimaryKey=&#39;FALSE&#39; DisplayName=&#39;Counter&#39; FromBaseType=&#39;TRUE&#39; /&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;       &amp;lt;/Method&amp;gt;&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;     var returnedNode = listService.UpdateList(listName ,properties,  newFields, &lt;span style=&quot;color: #0000ff&quot;&gt;null&lt;/span&gt;, &lt;span style=&quot;color: #0000ff&quot;&gt;null&lt;/span&gt;, version.Value);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none&quot;&gt;     Console.WriteLine(returnedNode.OuterXml);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style=&quot;padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none&quot;&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;  </description><link>http://elsharpo.blogspot.com/2009/04/sharepoint-web-services-creating-lists.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-5939159537428865108</guid><pubDate>Tue, 30 Sep 2008 12:20:00 +0000</pubDate><atom:updated>2008-09-30T22:32:49.368+10:00</atom:updated><title>Coming back</title><description>Hi there,

I really want to get back into blogging. I really enjoyed it and I learned a lot when I was posting.
Lately I been receiving some comments and emails from people that have found my (old) posts useful. I find this very humbling and extremely rewarding.  It&#39;s great to know you helped someone somewhere.

I&#39;m posting this entry to say I will do my best to get back into it. In the last few months I been very busy settling into a new job and with other personal things here and there.  I haven&#39;t neglected my own learning though, although I would have liked to spend more time with my own projects etc.

At the moment (and in the past few months) I been playing around with:

&lt;ul&gt;&lt;li&gt;Continuos Integration via CruiseControl.net.&lt;/li&gt;&lt;li&gt;NAnt
&lt;/li&gt;&lt;li&gt;nUnit
&lt;/li&gt;&lt;li&gt;ASP.net MVC&lt;/li&gt;&lt;li&gt;jQuery&lt;/li&gt;&lt;li&gt;Mocking concepts&lt;/li&gt;&lt;li&gt;IoC (using the Repository pattern)&lt;/li&gt;&lt;li&gt;Silverlight and WPF (tiny wee bit)&lt;/li&gt;&lt;/ul&gt;I&#39;m not totally sure how I&#39;m going to tackle the *comeback* but maybe having a target of one post a week could be a good starting point.

Look forward to my first post,

Cheers,

Juan</description><link>http://elsharpo.blogspot.com/2008/09/coming-back.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-4292491304653707100</guid><pubDate>Mon, 21 Apr 2008 14:42:00 +0000</pubDate><atom:updated>2008-04-22T00:53:49.098+10:00</atom:updated><title>Handling concurrency conflicts in LINQ to SQL</title><description>&lt;p&gt;Apologies for the delay in getting this post out, been busy and been playing around with too many good things lately. Virtual machines, Unit Testing etc, recording some radio from the net etc. Anyways, I have been planning to write about my experiences with Conflict handling in Linq2Sql, what happens when two users want to update the infamous Product table in Northwind.&lt;/p&gt;  &lt;p&gt;When I started playing around with it I also discovered that I needed to investigate a bit more about DataContexts, so I went reading around the web to gather info, did some experiments in my machine etc and interestingly enough I had a very related question at work where a colleague asked about the what are best practises when instantiating them. He had a static DataContext to be shared amongst his data access for a particular entity. My opinion on this is that DataContexts are cheap and light to create and that creating them for a single unit of work I do say in my GetProductByID function should be good enough. I&#39;ll say that persisiting DataContexts would create a bit of an overhead when I think that it has to keep all the mappings and ObjectTracking etc ( mind due, you can turn off ObjectTracking if you are going to do read only..). Also DCxt are not thread safe so that&#39;s another big reason not to do that. But when I started Linqing2Sql I did entertain the idea of sharing my DataContext through a singleton. Hrmm.&lt;/p&gt;  &lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;public void&lt;/span&gt;TurnOffObjectTracking()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;dal = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ProductDataContext&lt;/span&gt;();     &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style=&quot;color: green&quot;&gt;// If you don&#39;t want to update stuff, don&#39;t track them.      &lt;br /&gt;&amp;#160; &lt;/span&gt;dal.ObjectTrackingEnabled = &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;Product&lt;/span&gt;prod = dal.Products.SingleOrDefault(a =&amp;gt; a.ProductID == &lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;.NewGuid());     &lt;br /&gt;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;Assert&lt;/span&gt;.IsNull(prod);     &lt;br /&gt;}&lt;/p&gt; &lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;  &lt;p&gt;Let me not get sidetracked, it&#39;s 10:30pm and I want to go to sleep, back to our interleaving..&lt;/p&gt;  &lt;p&gt;First of all, let&#39;s recreate the case where &amp;lt;&amp;#160; 1 users are updating a record and our sys throws up a smelly. But before I do that, let&#39;s keep in mind that DataContexts are neat, I mean sometimes too neat since some stuff happens behind the scenes kind of magic, but that maybe just me :-( &lt;/p&gt;  &lt;p&gt;For example, note how in the example below two products are obtained from the db and one of them is upated, and then without committing this change to storage, I get another instance of the same product, then I commit the original change and then compare the Products....&lt;/p&gt;  &lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]
&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;Test_DataContext_Product_Independence()
{
    &lt;span style=&quot;color: blue&quot;&gt;using &lt;/span&gt;(System.Transactions.&lt;span style=&quot;color: #2b91af&quot;&gt;TransactionScope &lt;/span&gt;scope = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;System.Transactions.&lt;span style=&quot;color: #2b91af&quot;&gt;TransactionScope&lt;/span&gt;())
    {
        &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;linq = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ProductDataContext&lt;/span&gt;();
        linq.Log = &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.Out;

        &lt;span style=&quot;color: #2b91af&quot;&gt;Guid &lt;/span&gt;productID = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;50BD6DEB52C14D65BEC268957601EF25&amp;quot;&lt;/span&gt;);
        
        &lt;span style=&quot;color: green&quot;&gt;// Get a Product with the ID as above
        &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Product &lt;/span&gt;product_1 = linq.Products.Single(p =&amp;gt; p.ProductID == productID);
        
        &lt;span style=&quot;color: green&quot;&gt;// check that the Prod Name is as expected..
        &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Tourti&amp;#232;re&amp;quot;&lt;/span&gt;, product_1.ProductName);
        &lt;span style=&quot;color: green&quot;&gt;// Update the Prod Name
        &lt;/span&gt;product_1.ProductName = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;C&amp;#244;te de Tourti&amp;#232;re&amp;quot;&lt;/span&gt;;
        &lt;span style=&quot;color: green&quot;&gt;// Let&#39;s retrieve the product again, note that the update above has not been committed to the database
        &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Product &lt;/span&gt;product_2 = linq.Products.Single(p =&amp;gt; p.ProductID == productID);
        
        &lt;span style=&quot;color: green&quot;&gt;// Now commit!
        &lt;/span&gt;linq.SubmitChanges();

        &lt;span style=&quot;color: green&quot;&gt;// Note that the new name of Product2...obtained from DataContext Memory, before I committed the change! neat!
        &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;C&amp;#244;te de Tourti&amp;#232;re&amp;quot;&lt;/span&gt;, product_2.ProductName);               
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.ggpht.com/elsharpo/SAyn3v30joI/AAAAAAAAASU/Oc1YETGarLE/s1600-h/firstTestPassed4.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;101&quot; alt=&quot;firstTestPassed&quot; src=&quot;http://lh6.ggpht.com/elsharpo/SAyn4f30jpI/AAAAAAAAASc/k9p5Lpq5Kvk/firstTestPassed_thumb2.jpg?imgmax=800&quot; width=&quot;432&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;OK, so to simulate interleaving, we&#39;ll simulate the two users trying to update a record in the db. The record look like below&lt;/p&gt;

&lt;p&gt;ProductID / ProductName / UnitPrice&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&amp;#160;&lt;/a&gt;455984DB-92DD-4BA4-87D7-91E4E2E5E00D&amp;#160;&amp;#160;&amp;#160; Camomile Tea&amp;#160;&amp;#160;&amp;#160; 10.00&amp;#160;&amp;#160;&amp;#160; ....&lt;/p&gt;

&lt;p&gt;Now our two unsuspecting users are going to try to update the Product Name for this product&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]
&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;RecreateInterleavingCondition()
{
    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;user1DAL = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ProductDataContext&lt;/span&gt;();
    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;user2DAL = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ProductDataContext&lt;/span&gt;();

    &lt;span style=&quot;color: #2b91af&quot;&gt;Guid &lt;/span&gt;pID = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;455984db-92dd-4ba4-87d7-91e4e2e5e00d&amp;quot;&lt;/span&gt;);
    &lt;span style=&quot;color: #2b91af&quot;&gt;Product &lt;/span&gt;user1Product = user1DAL.Products.Single(p =&amp;gt; p.ProductID == pID);
    user1Product.ProductName = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Royal Camomile Tea&amp;quot;&lt;/span&gt;;
    &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(user1Product.ProductName);

    &lt;span style=&quot;color: #2b91af&quot;&gt;Product &lt;/span&gt;user2Product = user2DAL.Products.Single(p =&amp;gt; p.ProductID == pID);
    user2Product.ProductName = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Royal Camomile Tigris Tea&amp;quot;&lt;/span&gt;;
    &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(user1Product.ProductName);

    user1DAL.SubmitChanges();
    &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(user2Product.ProductName);
    user2DAL.SubmitChanges();
}&lt;/pre&gt;

&lt;p&gt;Run the above results in: Conflict Exception!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh4.ggpht.com/elsharpo/SAyn4_30jqI/AAAAAAAAASk/PiP0bj3hOkc/s1600-h/interleavingException%5B5%5D.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;133&quot; alt=&quot;interleavingException&quot; src=&quot;http://lh3.ggpht.com/elsharpo/SAyn5v30jrI/AAAAAAAAASs/8zchEYrX4y4/interleavingException_thumb%5B3%5D.jpg?imgmax=800&quot; width=&quot;862&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;User1 retrieves our Product and changes the name to &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Royal Camomile Tea&amp;quot;&lt;/span&gt;, not Submitting the changes, now User2 gets in there, retrieves the record changes to &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Royal Camomile Tigris Tea&amp;quot;&lt;/span&gt;, but User1 submits his changes and &lt;strong&gt;&lt;em&gt;then&lt;/em&gt;&lt;/strong&gt; User2 submits her changes. End result Conflict!&lt;/p&gt;

&lt;h2&gt;Now...How does LINQ to SQL handles it?&lt;/h2&gt;

&lt;p&gt;L2SQL uses what is called &lt;strong&gt;&lt;em&gt;Optimistic concurrency&lt;/em&gt;&lt;/strong&gt; is based on the un-healthy assumption that db transactions won&#39;t conflict with other transactions, that basically the chances of two users at exactly the same time wont happen that often. The alternative is &lt;strong&gt;&lt;em&gt;pessimistic concurrency&lt;/em&gt;&lt;/strong&gt;, this is where users will retrieve a record from the database and hold a lock on that record/table to prevent other pesky users to change that, problem is that is not scalable at all, systems grew larger and records where locked for ages before the sys will release the handle on it, also the emergence of the Web, the demands of stateles apps made it totally unfeasible for the pessimistic model.&lt;/p&gt;

&lt;p&gt;LINQ to SQL uses Optimistic concurrency, what it does is that when updating a record, it checks the previous values of that record, it the values where unchanged, then is all good, otherwise it will complain and it will throw an exception and then you can handle it and choose how to handle the conflict.&lt;/p&gt;

&lt;p&gt;In effect, if you turn Log on when Updating a record, and see the query passed onto the db you&#39;ll see that &lt;strong&gt;all &lt;/strong&gt;values will be passed in the WHERE clause to compare against the columns in your table, not only the ID for the record. See below for the updates we tried to do above. MSDN has a good article on LINQ to SQL and Optimistic conc &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/bb399373.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Royal 
    &lt;br /&gt;UPDATE [dbo].[Product] 
    &lt;br /&gt;SET [ProductName] = @p5 
    &lt;br /&gt;WHERE ([ProductID] = @p0) AND ([ProductName] = @p1) AND ([UnitPrice] = @p2) AND ([CategoryID] = @p3) AND ([CreatedDate] = @p4) 
    &lt;br /&gt;-- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [455984db-92dd-4ba4-87d7-91e4e2e5e00d] 
    &lt;br /&gt;-- @p1: Input VarChar (Size = 12; Prec = 0; Scale = 0) [Camomile Tea] 
    &lt;br /&gt;-- @p2: Input Money (Size = 0; Prec = 19; Scale = 4) [10.0000] 
    &lt;br /&gt;-- @p3: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [c9735b53-9c82-4f11-891a-8a53dc89565d] 
    &lt;br /&gt;-- @p4: Input DateTime (Size = 0; Prec = 0; Scale = 0) [17/04/2242 11:07:05 PM] 
    &lt;br /&gt;-- @p5: Input VarChar (Size = 5; Prec = 0; Scale = 0) [Royal] 
    &lt;br /&gt;
&lt;/p&gt;

  &lt;p&gt;Royal Tigris 
    &lt;br /&gt;UPDATE [dbo].[Product] 
    &lt;br /&gt;SET [ProductName] = @p5 
    &lt;br /&gt;WHERE ([ProductID] = @p0) AND ([ProductName] = @p1) AND ([UnitPrice] = @p2) AND ([CategoryID] = @p3) AND ([CreatedDate] = @p4) 
    &lt;br /&gt;-- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [455984db-92dd-4ba4-87d7-91e4e2e5e00d] 
    &lt;br /&gt;-- @p1: Input VarChar (Size = 12; Prec = 0; Scale = 0) [Camomile Tea] 
    &lt;br /&gt;-- @p2: Input Money (Size = 0; Prec = 19; Scale = 4) [10.0000]   
  &lt;br /&gt;-- @p3: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [c9735b53-9c82-4f11-891a-8a53dc89565d] 
    &lt;br /&gt;-- @p4: Input DateTime (Size = 0; Prec = 0; Scale = 0) [17/04/2242 11:07:05 PM] 
    &lt;br /&gt;-- @p5: Input VarChar (Size = 12; Prec = 0; Scale = 0) [Royal Tigris]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;DataContext implements Optimistic by default, but you can turn it off, imagine that it passed lots of parameters, this will certainly cause performance dramas or you simply could streamline it. You can fix your mappings with the UpdateCheck attribute. More info check MSDN article &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.mapping.columnattribute.updatecheck.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/bb399394.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, so the above will throw an exception when the Update query cannot find a match, it will interpret this as &amp;quot;oh, someone else got in first and changed something before I did! :-_(&amp;#160; , I&#39;ll throw a ChangeConflictException then...&amp;quot;&lt;/p&gt;

&lt;h2&gt;The art of handling conflicts&lt;/h2&gt;

&lt;p&gt;First of all, wrap your potentially conflict-causing code in a try/catch block and look out for a &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.changeconflictexception.aspx&quot; target=&quot;_blank&quot;&gt;ChangeConflictException&lt;/a&gt; as below..&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;try
&lt;/span&gt;{
    linq_2.SubmitChanges();
    &lt;span style=&quot;color: green&quot;&gt;// Here the values change!
    &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;result = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ProductDataContext&lt;/span&gt;();
    &lt;span style=&quot;color: #2b91af&quot;&gt;Product &lt;/span&gt;product13 = result.Products.Single(i =&amp;gt; i.ProductID == pID);
    linq.SubmitChanges();    
}
&lt;span style=&quot;color: blue&quot;&gt;catch &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;ChangeConflictException &lt;/span&gt;ex)
{ ....&lt;/pre&gt;

&lt;p&gt;Once you catch the exception, you have choices of how to handle the conflict by basically examining the guilty entities and members and choosing which values to keep, be it Database, overwriting database&amp;#160; values&amp;#160; or merging changes with the database. Let&#39;s have a look first at how to retrieve the conflicting data. &lt;/p&gt;

&lt;p&gt;DataContext objects have a property called &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.datacontext.changeconflicts.aspx&quot; target=&quot;_blank&quot;&gt;ChangeConflicts&lt;/a&gt; thats gives you a &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.changeconflictcollection.aspx&quot; target=&quot;_blank&quot;&gt;ChangeConflictCollection&lt;/a&gt; of &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.objectchangeconflict.aspx&quot; target=&quot;_blank&quot;&gt;ObjectChangeConflict&lt;/a&gt; objects that eventually you can enumerate and then drill down and get at the values that are in conflict. &lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;catch &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;ChangeConflictException &lt;/span&gt;ex)
{
    &lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;ObjectChangeConflict &lt;/span&gt;occ &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;linq.ChangeConflicts)
    {
    ...........................................&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;    ...........................................&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;font face=&quot;Trebuchet MS&quot;&gt;Each ObjectChangeConflict object represents the conflicted instance, in plain english it encapsulates the update attempt that caused the conflict. &lt;/font&gt;&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;font face=&quot;Trebuchet MS&quot;&gt;We can then iterate over the entities that &amp;quot;participated&amp;quot; in that conflict and get specific information about them. You do this by accessing the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.objectchangeconflict.memberconflicts.aspx&quot; target=&quot;_blank&quot;&gt;MemberConflicts&lt;/a&gt; collection of ObjectChangeConflict.&lt;/font&gt;&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;catch &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;ChangeConflictException &lt;/span&gt;ex)
{
    &lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;ObjectChangeConflict &lt;/span&gt;occ &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;linq.ChangeConflicts)
    {
        &lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;MemberChangeConflict &lt;/span&gt;mcc &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;occ.MemberConflicts)
        {
            &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Original: &amp;quot; &lt;/span&gt;+ mcc.OriginalValue);
            &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Database value: &amp;quot; &lt;/span&gt;+ mcc.DatabaseValue);
            &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;CurrentValue: &amp;quot; &lt;/span&gt;+ mcc.CurrentValue);
        }&lt;/pre&gt;

&lt;p&gt;Here is where it starts to get juicy. See how above we can get the different values for a particular entity through the &amp;quot;stages&amp;quot; of the conflict. This information could be very useful, you might want to present this details to the user and let her make a decision of what to submit to the database in&amp;#160; a grid or something...right?&lt;/p&gt;

&lt;p&gt;But of course, you can explicitly resolve the conflicts by once the error is detected, choosing to keep current values, database values or merge. Let&#39;s have a look at it.&lt;/p&gt;

&lt;h3&gt;Overwriting Database values&lt;/h3&gt;

&lt;p&gt;First up we are going to solve the conflict by merging the database values with User1 only since User1 got in there last, he is the last committer (is there such a work in english?), User2 won&#39;t see anything since her changes when in and the Optimistic handling didn&#39;t detect any changes. So the conflict is a conflict from the context of User1, he created the conflict, he is the last in so he has to tell the database what to do with the changes..The database if it talked, it could ask the question..&amp;quot;oh..you are last in...what do you we do with your changes...&amp;quot;, That&#39;s the way I see it. :-)&amp;#160; Shut up and show me code please ... thanks. &lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]
&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;CheckProductNamesWithTwoDataContexts()
{           
    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;linq = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ProductDataContext&lt;/span&gt;();
    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;linq_2 = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ProductDataContext&lt;/span&gt;();
    &lt;span style=&quot;color: #2b91af&quot;&gt;Guid &lt;/span&gt;pID  = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;455984db-92dd-4ba4-87d7-91e4e2e5e00d&amp;quot;&lt;/span&gt;);

    &lt;span style=&quot;color: #2b91af&quot;&gt;Product &lt;/span&gt;p1 = linq.Products.Single(p =&amp;gt; p.ProductID == pID );
    p1.ProductName = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Red Tea&amp;quot;;&lt;/span&gt;
    p1.CategoryID = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot;&lt;/span&gt;);
    
    &lt;span style=&quot;color: #2b91af&quot;&gt;Product &lt;/span&gt;p2 = linq_2.Products.Single(p =&amp;gt; p.ProductID == pID);
    p2.ProductName = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Green Tea&amp;quot;&lt;/span&gt;;
    p2.CategoryID = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Guid&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;90000000-1111-1111-1111-000000000000&amp;quot;&lt;/span&gt;);
    p2.UnitPrice = 10.0M;
    
    &lt;span style=&quot;color: blue&quot;&gt;try
    &lt;/span&gt;{
        linq_2.SubmitChanges();
        &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;result = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ProductDataContext&lt;/span&gt;();
        linq.SubmitChanges(&lt;span style=&quot;color: #2b91af&quot;&gt;ConflictMode&lt;/span&gt;.ContinueOnConflict);  
    }
    &lt;span style=&quot;color: blue&quot;&gt;catch &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;ChangeConflictException &lt;/span&gt;ex)
    {
        &lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;ObjectChangeConflict &lt;/span&gt;occ &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;linq.ChangeConflicts)
        {
            &lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;MemberChangeConflict &lt;/span&gt;mcc &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;occ.MemberConflicts)
            {
                &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Original: &amp;quot; &lt;/span&gt;+ mcc.OriginalValue);
                &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Database value: &amp;quot; &lt;/span&gt;+ mcc.DatabaseValue);
                &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;CurrentValue: &amp;quot; &lt;/span&gt;+ mcc.CurrentValue);
            }  &lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;            linq.ChangeConflicts.ResolveAll(&lt;span style=&quot;color: #2b91af&quot;&gt;RefreshMode&lt;/span&gt;.KeepCurrentValues);&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;pre class=&quot;code&quot;&gt;        }
        &lt;strong&gt;linq.SubmitChanges(); &lt;/strong&gt;
    }
}&lt;/pre&gt;

&lt;p&gt;Quite a lengthy function yeah, but let&#39;s see what happens. User1 retrieved the Product, changed the name and the CategoryID, User2 changed the name, CategoryID and UnitPrice. Now note that I called &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.refreshmode.aspx&quot; target=&quot;_blank&quot;&gt;ResolveAll&lt;/a&gt; of the ChangeConflict fame from our DataContext, in this case I called it linq. ( You can also call ObjectChangeConflict&#39;s Resolve function) This function takes an enum that could be KeepCurrentValues, KeepChanges and OverwriteCurrentValues. In this case we use &lt;strong&gt;&lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.refreshmode.keepcurrentvalues.aspx&quot; target=&quot;_blank&quot;&gt;KeepCurrentChanges&lt;/a&gt;&lt;/strong&gt;, which means we&#39;ll keep our changes for the data that clashed with User2, anything else that User2 changed will be ignored and the database defaults will be reinstated. I suppose the way to interpret it is KeepCurrentValues keeps the current values in the DataContext, in this case User1. Note: Make sure once you handle the exception any way you see fit, make sure you SubmitChanges() again for that DataContext otherwise nothing will happen! :-)&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Also note that you can choose also when to handle the exceptions. Say you have a lot of changes that the user made and it will be too cumbersome for them to re-enter all those values again. Since SubmitChanges accumulates all conflicts in a batch, the number of changes could be huge.. Good news is that SubmitChanges() can be overloaded to take a ConflictMode enum that will either tell the instance of DataContext to either throw the ConflictException as soon as it detects it (thus minimising the potential hassles for users) called &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.conflictmode.failonfirstconflict.aspx&quot; target=&quot;_blank&quot;&gt;FailOnFirstConflict&lt;/a&gt;, or to keep accumulating and attempting to update data and then report the whole lot (&lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.conflictmode.continueonconflict.aspx&quot; target=&quot;_blank&quot;&gt;ContinueOnConflict&lt;/a&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;table cellspacing=&quot;1&quot; cellpadding=&quot;2&quot; width=&quot;464&quot; border=&quot;1&quot;&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;108&quot;&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;102&quot;&gt;ProductName&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;140&quot;&gt;CategoryID&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;105&quot;&gt;UnitPrice&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;106&quot;&gt;Database row (originally) &lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;104&quot;&gt;Camomile Tea&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;140&quot;&gt;E1422E86-....&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;105&quot;&gt;&lt;strong&gt;0.00&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;106&quot;&gt;User 1&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;105&quot;&gt;&lt;strong&gt;Red Tea&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;140&quot;&gt;&amp;#160;&lt;strong&gt;Guid.Empty&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;105&quot;&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;104&quot;&gt;User 2&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;106&quot;&gt;Green Trea&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;140&quot;&gt; Guid.NewGuid&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;105&quot;&gt;10&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Result is:&lt;/p&gt;

&lt;table cellspacing=&quot;1&quot; cellpadding=&quot;2&quot; width=&quot;467&quot; border=&quot;1&quot;&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;93&quot;&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;123&quot;&gt;ProductName&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;132&quot;&gt;CategoryID&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;109&quot;&gt;UnitPrice&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;93&quot;&gt;database&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;123&quot;&gt;RedTea&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;132&quot;&gt;Guid.Empty&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;109&quot;&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;



&lt;p&gt;Note that User1 values went it , User2 data was thrown out and even though in UnitPrice there wasn&#39;t a clash between the users, the database value was the chosen one!&lt;/p&gt;
&lt;h3&gt;Retaining Database values&lt;/h3&gt;
&lt;p&gt;This is in fact the opposite as our previous example, User2 values are merged with the database and User1&#39;s values are chucked out of the window. Basically User1 says, &amp;quot;I&#39;ll let the other guy&#39;s values go through..I&#39;ll comeback to it again and see where my data fits with hers..&amp;quot;. The term OverwriteCurrentValues, the way I interpret it that is read from the point of view of User1, &amp;quot;Overwrite my current values, therefore since User2 has committed and are in the db, keep them..&amp;quot;.&amp;#160; This is achieved by using:&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;linq.ChangeConflicts.ResolveAll(&lt;span style=&quot;color: #2b91af&quot;&gt;RefreshMode&lt;/span&gt;.OverwriteCurrentValues);&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;Table please...&lt;/p&gt;

&lt;table cellspacing=&quot;1&quot; cellpadding=&quot;2&quot; width=&quot;460&quot; border=&quot;1&quot;&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;111&quot;&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;128&quot;&gt;ProductName&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;117&quot;&gt;CategoryID&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;95&quot;&gt;UnitPrice&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;111&quot;&gt;Database&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;128&quot;&gt;Camomile&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;117&quot;&gt;E1422E86&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;97&quot;&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;111&quot;&gt;User1&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;128&quot;&gt;Red Tea&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;117&quot;&gt;Guid.Empty&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;98&quot;&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;111&quot;&gt;User2 &lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;128&quot;&gt;Green Tea&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;117&quot;&gt;NewGuid&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;99&quot;&gt;10.0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;


&lt;table cellspacing=&quot;1&quot; cellpadding=&quot;2&quot; width=&quot;458&quot; border=&quot;1&quot;&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;115&quot;&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;120&quot;&gt;ProductName&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;110&quot;&gt;CategoryID&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;104&quot;&gt;UnitPrice&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;115&quot;&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;120&quot;&gt;Green Tea&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;112&quot;&gt;NewID&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;105&quot;&gt;10.0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;


&lt;h3&gt;Merging Conflicts with Database values&lt;/h3&gt;

&lt;p&gt;Using ResolveAll(RefreshMode.KeepChanges) means keeps all the changes in the DataContext and merge them with the database, overwritting the row values if necessary.&lt;/p&gt;


&lt;table cellspacing=&quot;1&quot; cellpadding=&quot;2&quot; width=&quot;400&quot; border=&quot;1&quot;&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;ProductName&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;CategoryID&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;UnitPrice&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;Database&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;Camomile&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;E1422E86&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;User1&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;Red Tea&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;&amp;#160;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;User2 &lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;Green Tea&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;NewGuid&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;10.0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;The results are as expected, User1 keeps his Red Tea, and User2 keep hers NewGuid and UnitPrice, in effect the merging has been &amp;quot;between&amp;quot; the users only.&lt;/p&gt;

&lt;table cellspacing=&quot;1&quot; cellpadding=&quot;2&quot; width=&quot;400&quot; border=&quot;1&quot;&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;ProductName&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;CategoryID&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;UnitPrice&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;&amp;#160;&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;Red Tea&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;NewID&lt;/td&gt;

      &lt;td valign=&quot;top&quot; width=&quot;100&quot;&gt;10.0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Apologies for the delay in getting this one out, I have been busy and I was writing this up when we noted our beautiful and very lively ( and vicious) pet parrot passed away last Thursday night.&amp;#160; Very sad and it all was all of a sudden. I hope the poor fella didn&#39;t suffer and I certainly miss his singing in the mornings. Lesson learned though, birds should be free and while they are very entertaining I don&#39;t think is fair to keep them [birds] inside for our selfish gratification. &lt;/p&gt;

&lt;p&gt;I certainly enjoyed writing this post and playing around with the DataContext, generating errors etc. There is ton of info on this in MSDN, I certainly think the guys have done a great job in documenting Conflict resolution in LINQ to SQL.&lt;/p&gt;

&lt;p&gt;Place for you to start would be:&lt;/p&gt;

&lt;p&gt;&lt;a title=&quot;http://msdn2.microsoft.com/en-us/library/bb399389.aspx&quot; href=&quot;http://msdn2.microsoft.com/en-us/library/bb399389.aspx&quot;&gt;http://msdn2.microsoft.com/en-us/library/bb399389.aspx&lt;/a&gt;&lt;/p&gt;  </description><link>http://elsharpo.blogspot.com/2008/04/handling-concurrency-conflicts-in-linq.html</link><author>noreply@blogger.com (elsharpo)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/elsharpo/SAyn4f30jpI/AAAAAAAAASc/k9p5Lpq5Kvk/s72-c/firstTestPassed_thumb2.jpg?imgmax=800" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-7053908838115364075</guid><pubDate>Sun, 06 Apr 2008 12:30:00 +0000</pubDate><atom:updated>2008-04-07T09:25:34.125+10:00</atom:updated><title>Playing around with LINQ to SQL: Enumerable.ToDictionary Extension Method</title><description>&lt;p&gt;Playing around with Linq I noticed the Enumerable.&lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.linq.enumerable.todictionary.aspx&quot; target=&quot;_blank&quot;&gt;ToDictionary&lt;/a&gt; extension method on IEnumerables&amp;lt;T&amp;gt;. I found it very handy to store expected results for Unit Testing. Yesterday I was playing around with general stuff and I did some looking around a ToDictionary.&lt;/p&gt;  &lt;p&gt;We all know what &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx&quot; target=&quot;_blank&quot;&gt;Dictionaries&lt;/a&gt; are in the programming world right? Well they are data structures where each element is a key/value pair. Each key in it must be unique and it cannot be null. They are blindly fast for element retrieval by key too, O(n) . So basically they are very efficient at locating, inserting, deleting records associated by keys,&lt;/p&gt;  &lt;p&gt;The ToDictionary&amp;lt;TSource,TKey&amp;gt; can be used in many ways, 4 to be more precise but in all them you have to specify a Func&amp;lt;TSource,TElement&amp;gt; to be your key selector, you also have the choice to provide another function to your element selector, ie the object that you are going to store as key and finally you can pass on a IEqualityComparer&amp;lt;T&amp;gt; to compare keys so the underlying HashTable can determine whether keys used are equal. (If you try to add an Element with a Key that already exists in the Dictionary, an ArgumentException will be thrown.&lt;/p&gt;  &lt;p&gt;Examples:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]&lt;br /&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;CreateDictionary_of_AnonymousTypes()&lt;br /&gt;{&lt;br /&gt;           &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;ps = &lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;p &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;productos&lt;br /&gt;                    &lt;span style=&quot;color: blue&quot;&gt;orderby &lt;/span&gt;p.ProductID &lt;span style=&quot;color: blue&quot;&gt;ascending&lt;br /&gt;                    select new &lt;/span&gt;{ p.ProductID,p.ProductName};&lt;br /&gt;&lt;br /&gt;           &lt;span style=&quot;color: green&quot;&gt;// Creates a Dictionary where the Key is a productID and the Object is an Anonymous type.&lt;br /&gt;           &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;dummyDictionary =  ps.ToDictionary(a =&amp;gt; a.ProductID); &lt;span style=&quot;color: green&quot;&gt;// Note the Ano&lt;/span&gt;&lt;br /&gt;           &lt;br /&gt;           &lt;span style=&quot;color: green&quot;&gt;// 77 Products in Northwind...&lt;br /&gt;           &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Assert&lt;/span&gt;.AreEqual(77, dummyDictionary.Count());&lt;br /&gt;           &lt;span style=&quot;color: #2b91af&quot;&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Tofu&amp;quot;&lt;/span&gt;, dummyDictionary[14].ProductName);           &lt;br /&gt;           &lt;span style=&quot;color: green&quot;&gt;// Is it a fib that oo.ProductName is not &#39;Chai tea&#39; ?&lt;br /&gt;           &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Assert&lt;/span&gt;.IsFalse(dummyDictionary[1].ProductName != &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Chai tea&amp;quot;&lt;/span&gt;); &lt;span style=&quot;color: green&quot;&gt;// Product Name of Product with ID = 1 is &#39;Chai tea&#39;&lt;br /&gt;&lt;/span&gt;}&lt;/pre&gt;

&lt;br /&gt;

&lt;p&gt;In this example all I did was crated a Dictionary by using the productID as key selector, the dictionary will have a integer as key and the Element will be an Anonymous type.
  &lt;br /&gt;&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]&lt;br /&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;CreateDictionary2()&lt;br /&gt;{&lt;br /&gt;          &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;ps = &lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;p &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;productos&lt;br /&gt;                   &lt;span style=&quot;color: blue&quot;&gt;orderby &lt;/span&gt;p.ProductID &lt;span style=&quot;color: blue&quot;&gt;ascending&lt;br /&gt;                   select new &lt;/span&gt;{ p.ProductID, p.ProductName };   &lt;br /&gt;&lt;br /&gt;           &lt;span style=&quot;color: green&quot;&gt;// Creating a dictionary by explicitly determining the Key and the Element            &lt;br /&gt;           &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;int&lt;/span&gt;,&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;&amp;gt; elDictionary = ps.ToDictionary(a =&amp;gt; a.ProductID, a=&amp;gt;a.ProductName);&lt;br /&gt;           &lt;span style=&quot;color: #2b91af&quot;&gt;CollectionAssert&lt;/span&gt;.AllItemsAreUnique(elDictionary.Keys);&lt;br /&gt;}&lt;/pre&gt;

&lt;br /&gt;

&lt;p&gt;This example a function to select keys and select Elements was done, resulting in a Dictionary&amp;lt;int,String&amp;gt;&lt;/p&gt;

&lt;p&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]

  &lt;br /&gt;&lt;span style=&quot;color: blue&quot;&gt;public void&lt;/span&gt;Dictionary_Products_By_CategoryName()

  &lt;br /&gt;{

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: #2b91af&quot;&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Product&lt;/span&gt;&amp;gt;&amp;gt; superDictionary;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;prods = &lt;span style=&quot;color: blue&quot;&gt;from&lt;/span&gt;p &lt;span style=&quot;color: blue&quot;&gt;in&lt;/span&gt;productos

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;group&lt;/span&gt;p &lt;span style=&quot;color: blue&quot;&gt;by&lt;/span&gt;p.Category.CategoryName &lt;span style=&quot;color: blue&quot;&gt;into&lt;/span&gt;prods_by_categoryName

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;select new
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;{&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; list = prods_by_categoryName

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; superDictionary = prods.ToDictionary(a =&amp;gt; a.list.Select(b =&amp;gt; b.Category.CategoryName).First(), c =&amp;gt; c.list.Select(y=&amp;gt;y));

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;IOrderedEnumerable&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;&amp;gt; coll = superDictionary.Keys.OrderBy(a=&amp;gt;a);

  &lt;br /&gt;

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;&amp;gt; orderedCategories = ((&lt;span style=&quot;color: blue&quot;&gt;from&lt;/span&gt;cats &lt;span style=&quot;color: blue&quot;&gt;in&lt;/span&gt;ctx.Categories

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: blue&quot;&gt;select&lt;/span&gt;cats.CategoryName).OrderBy(h=&amp;gt;h));

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;&amp;gt; myList = coll.ToList();

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; myList.Reverse();

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; myList.Reverse();

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: #2b91af&quot;&gt;CollectionAssert&lt;/span&gt;.AreEqual(myList , orderedCategories.ToList());

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt; }&lt;/p&gt;

&lt;br /&gt;

&lt;p&gt;This example I created a Dictionary of Products, where the key is the CategoryName and the Elements is a IEnumberable of Products for that Category.&lt;/p&gt;

&lt;p&gt;The same can be achieved as below, much simpler!&lt;/p&gt;

&lt;br /&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]&lt;br /&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;Dictionary_Products_By_CategoryName_Simple()&lt;br /&gt;{&lt;br /&gt;            &lt;span style=&quot;color: #2b91af&quot;&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: #2b91af&quot;&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Product&lt;/span&gt;&amp;gt;&amp;gt; superDictionary;&lt;br /&gt;            superDictionary = (&lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;categories &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;ctx.Categories&lt;br /&gt;                               &lt;span style=&quot;color: blue&quot;&gt;select new&lt;br /&gt;                                &lt;/span&gt;{&lt;br /&gt;                                  categories.CategoryName,&lt;br /&gt;                                  ProductsInCategory = categories.Products.Select(a=&amp;gt;a)&lt;br /&gt;                                 }).ToDictionary(a=&amp;gt;a.CategoryName,a=&amp;gt;a.ProductsInCategory);&lt;br /&gt;&lt;br /&gt;            &lt;span style=&quot;color: #2b91af&quot;&gt;Assert&lt;/span&gt;.IsInstanceOfType(superDictionary, &lt;span style=&quot;color: blue&quot;&gt;typeof&lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: #2b91af&quot;&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Product&lt;/span&gt;&amp;gt;&amp;gt;));&lt;br /&gt;}&lt;/pre&gt;

&lt;br /&gt;

&lt;p&gt;Here I want to list all Products by Category, and sort them by total unit price...&lt;/p&gt;

&lt;br /&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]&lt;br /&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;do_all_that_stuff_in_less_lines_of_code()&lt;br /&gt;{&lt;br /&gt;    &lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext &lt;/span&gt;ctx = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();&lt;br /&gt;    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;result = (&lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;categories &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;ctx.Categories&lt;br /&gt;                 &lt;span style=&quot;color: blue&quot;&gt;select new&lt;br /&gt;                 &lt;/span&gt;{&lt;br /&gt;                     CategoryName = categories.CategoryName,&lt;br /&gt;                     TotalCategoryUnitPrice = categories.Products.Sum(a=&amp;gt;a.UnitPrice),&lt;br /&gt;                     Products = categories.Products.OrderByDescending(a=&amp;gt;a.UnitPrice).ToList()&lt;br /&gt;                 }).OrderByDescending(a=&amp;gt;a.TotalCategoryUnitPrice);&lt;br /&gt;&lt;br /&gt;   &lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;category &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;result)&lt;br /&gt;   {&lt;br /&gt;        &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine( &lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;.Format(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;\n{0}:\n{1}&amp;quot;&lt;/span&gt;,category.CategoryName, &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&#39;-&#39;&lt;/span&gt;, category.CategoryName.Length  )));&lt;br /&gt;        category.Products.ForEach(p =&amp;gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;.Format(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;{0,-35} {1,10:C2}&amp;quot;&lt;/span&gt;, p.ProductName, p.UnitPrice)));&lt;br /&gt;        &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;.Format( &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;{0,46}&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;(&lt;span style=&quot;color: #a31515&quot;&gt;&#39;-&#39;&lt;/span&gt;,6) ));&lt;br /&gt;        &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Total:&amp;quot;&lt;/span&gt;.PadLeft(38) + &lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;.Format(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;{0,8:C2}&amp;quot;&lt;/span&gt;, category.TotalCategoryUnitPrice));&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;

&lt;br /&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R_jCTT9p1MI/AAAAAAAAASE/CaAvtmTQrKM/Capture%5B3%5D.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;350&quot; alt=&quot;Capture&quot; src=&quot;http://lh3.google.com/elsharpo/R_jCUT9p1NI/AAAAAAAAASM/ShKrwvPld-s/Capture_thumb%5B1%5D.jpg&quot; width=&quot;492&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;br /&gt;

&lt;p&gt;Also the ToLookup function is worth mentioning. What it does is creates a &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/bb460184.aspx&quot; target=&quot;_blank&quot;&gt;Lookup&amp;lt;TKey,TElement&amp;gt;&lt;/a&gt; ( basically a collection where each key is mapped to one or more values)&lt;/p&gt;

&lt;br /&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]&lt;br /&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;ToLookup()&lt;br /&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;prods_by_cat = &lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;products &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;ctx.Products&lt;br /&gt;                       &lt;span style=&quot;color: blue&quot;&gt;select &lt;/span&gt;products;                      &lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: #2b91af&quot;&gt;Lookup&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;&amp;gt; ellookup = (&lt;span style=&quot;color: #2b91af&quot;&gt;Lookup&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;&amp;gt;)prods_by_cat.ToLookup(&lt;br /&gt;                                                a =&amp;gt; a.Category.CategoryName, &lt;span style=&quot;color: green&quot;&gt;// Key&lt;br /&gt;                                                &lt;/span&gt;a =&amp;gt; a.ProductName); &lt;span style=&quot;color: green&quot;&gt;// Value&lt;br /&gt;    // Iterate through each IGrouping in the Lookup and output the contents.&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;IGrouping&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;&amp;gt; packageGroup &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;ellookup)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: green&quot;&gt;// Print the key value of the IGrouping.&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(packageGroup.Key);&lt;br /&gt;        &lt;span style=&quot;color: green&quot;&gt;// Iterate through each value in the IGrouping and print its value.&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;str &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;packageGroup)&lt;br /&gt;            &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot; {0}&amp;quot;&lt;/span&gt;, str);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;br /&gt;

&lt;p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;and the output..note that packageGroup is just a collection of Products for that particular Category. Cool eh?&lt;/p&gt;

&lt;blockquote&gt;
  &lt;br /&gt;

  &lt;p&gt;Beverages
    &lt;br /&gt;Chai tea

    &lt;br /&gt;Changta

    &lt;br /&gt;Guaran&amp;#225; Fant&amp;#225;stica

    &lt;br /&gt;Steeleye Stout

    &lt;br /&gt;C&amp;#244;te de Blaye

    &lt;br /&gt;Chartreuse verte

    &lt;br /&gt;Ipoh Coffee

    &lt;br /&gt;Laughing Lumberjack Lager

    &lt;br /&gt;Outback Lager

    &lt;br /&gt;Rh&amp;#246;nbr&amp;#228;u Klosterbier

    &lt;br /&gt;Lakkalik&amp;#246;&amp;#246;ri

    &lt;br /&gt;Condiments

    &lt;br /&gt;Aniseed Syrup

    &lt;br /&gt;Chef Anton&#39;s Cajun Seasoning

    &lt;br /&gt;Chef Anton&#39;s Gumbo Mix

    &lt;br /&gt;Grandma&#39;s Boysenberry Spread

    &lt;br /&gt;Northwoods Cranberry Sauc&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Anyway, I don&#39;t think I&#39;ll be playing with any Dictionaries for a while, done and dusted!&lt;/p&gt;  </description><link>http://elsharpo.blogspot.com/2008/04/playing-around-with-linq-to-sql.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-2879731097011151403</guid><pubDate>Sun, 06 Apr 2008 09:52:00 +0000</pubDate><atom:updated>2008-04-06T20:54:37.060+10:00</atom:updated><title>Playing around with LINQ to SQL: Using DataLoadOptions. Immediate loading and filtering of related data</title><description>&lt;p&gt;Linq2Sql use deferred execution of queries, &lt;em&gt;lazy loading &lt;/em&gt;to get data from the database. The data is only retrieved from the source when we are ready to use it, say when we iterate through it and do something to it. The are advantages to it, say when we have hierarchical data like Orders-&amp;gt;OrderDetails, iterating through the Order will not get you all the child objects ( the OrderDetails) by default. This is a good thing of course, it returns you what you want but it can also lead to unexpected results and unnecessary round trips to the database if not careful.&lt;/p&gt; &lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R_idaj9p1KI/AAAAAAAAAR0/LY1X0q90YTg/order_order_details_Products%5B4%5D.jpg&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;352&quot; alt=&quot;order_order_details_Products&quot; src=&quot;http://lh3.google.com/elsharpo/R_idbT9p1LI/AAAAAAAAAR8/yMt0P1yvss4/order_order_details_Products_thumb%5B2%5D.jpg&quot; width=&quot;604&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]
&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;Lazy_Load_Them()
{
&lt;span style=&quot;color: blue&quot;&gt;    var &lt;/span&gt;context = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();
    context.Log = &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.Out;

    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;mydata = &lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;order &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;context.Orders
                 &lt;span style=&quot;color: blue&quot;&gt;select &lt;/span&gt;order;

    &lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;Order &lt;/span&gt;o &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;mydata)
    {
          &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(o.OrderID);
    }
}&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The only query generated is below:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]&lt;br&gt;FROM [dbo].[Orders] AS [t0]&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;In the above case it all very simple, all we wanted was the Order(s) entities, network bandwidth was minimised, memory consumption minimised etc.&amp;nbsp; But what if inside the foreach we also want to output the OrderDetails children of Orders and their corresponding Products? All the child records will be requested when we want, in the below case when we output them...&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;TestMethod&lt;/span&gt;]
&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;Lazy_Load_Them()
{
    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;context = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();
    context.Log = &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.Out;
    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;mydata = &lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;order &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;context.Orders
                 &lt;span style=&quot;color: blue&quot;&gt;select &lt;/span&gt;order;

    &lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;Order &lt;/span&gt;o &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;mydata)
    {
         &lt;span style=&quot;color: #2b91af&quot;&gt;Order_Detail &lt;/span&gt;od = o.Order_Details.First();&lt;span style=&quot;color: green&quot;&gt;//Just the first record for brevity
         &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine(od.ProductID + &quot;, &quot; + od.Product.ProductName);
     }
}&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;Well, the results are gory, as we request details and the Product for each Order, a separate query will be created and this will mean a new round trip to the database for every Order. The queries are below and trust me, the are like that for every row of the Order table.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]&lt;br&gt;FROM [dbo].[Orders] AS [t0]&lt;br&gt;-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8 
&lt;p&gt;SELECT [t0].[OrderID], [t0].[ProductID], [t0].[UnitPrice], [t0].[Quantity], [t0].[Discount]&lt;br&gt;FROM [dbo].[Order Details] AS [t0]&lt;br&gt;WHERE [t0].[OrderID] = @p0&lt;br&gt;-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [10248]&lt;br&gt;-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8 
&lt;p&gt;SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]&lt;br&gt;FROM [dbo].[Products] AS [t0]&lt;br&gt;WHERE [t0].[ProductID] = @p0&lt;br&gt;-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [11]&lt;br&gt;-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8 
&lt;p&gt;&lt;strong&gt;11, Queso Cabrales&lt;/strong&gt;&lt;br&gt;SELECT [t0].[OrderID], [t0].[ProductID], [t0].[UnitPrice], [t0].[Quantity], [t0].[Discount]&lt;br&gt;FROM [dbo].[Order Details] AS [t0]&lt;br&gt;WHERE [t0].[OrderID] = @p0&lt;br&gt;-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [10249]&lt;br&gt;-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8 
&lt;p&gt;SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]&lt;br&gt;FROM [dbo].[Products] AS [t0]&lt;br&gt;WHERE [t0].[ProductID] = @p0&lt;br&gt;-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [14]&lt;br&gt;-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8 
&lt;p&gt;&lt;strong&gt;14, Tofu&lt;/strong&gt;&lt;br&gt;SELECT [t0].[OrderID], [t0].[ProductID], [t0].[UnitPrice], [t0].[Quantity], [t0].[Discount]&lt;br&gt;FROM [dbo].[Order Details] AS [t0]&lt;br&gt;WHERE [t0].[OrderID] = @p0&lt;br&gt;-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [10250]&lt;br&gt;-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;As you can see we have queries for the OrderDetails and another query for the Product data..Three queries for for that! For evey single Order? Hrmm
&lt;p&gt;I have made a total stuff-up, a good thing &#39;lazy loading&#39; turned into a monster. I have been careless in this case, but how can we improve it? How can minimize the round trips?
&lt;h3&gt;DataLoadOptions: LoadWith(LambdaExpression)&lt;/h3&gt;
&lt;p&gt;LinqToSql provides a method for immediate loading of data related to the main Entity we are working through the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.dataloadoptions.aspx&quot; target=&quot;_blank&quot;&gt;DataLoadOptions&lt;/a&gt; class. This class provides two methods to get immediate loading of related data, The LoadWith method and AssociateWith method that allow for filtering of related objects.&lt;/p&gt;
&lt;p&gt;All you have to do is specify which data that is related to your main target and should be retrieved at the same time with a Lambda expression, for example in our case above our main Entity is the Order, and we would like to get the Order_Details for each Order. This will result in only one trip to the database for all that information, we eliminate all the subqueries that we needed because of the lazy loading. This will improve things significantly for our applications and our users. &lt;/p&gt;
&lt;p&gt;In the exaple below I want to get all the Order_Details related to my main interest, Order.&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;Get_Deferred_Data_With_Loading_Options()&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;dataContext = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: #2b91af&quot;&gt;DataLoadOptions&lt;/span&gt;dataLoadOptions = &lt;span style=&quot;color: blue&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;DataLoadOptions&lt;/span&gt;();&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataLoadOptions.LoadWith&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Order&lt;/span&gt;&amp;gt;(order =&amp;gt; order.Order_Details);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataContext.LoadOptions = dataLoadOptions;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;orders = &lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;order &lt;span style=&quot;color: blue&quot;&gt;in&lt;/span&gt;dataContext.Orders&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue&quot;&gt;select &lt;/span&gt;order;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;Order &lt;/span&gt;order &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;orders)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Order_Detail&lt;/span&gt;&amp;gt; ods = order.Order_Details.ToList();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ods.ForEach(od =&amp;gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine( order.CustomerID+ &lt;span style=&quot;color: #a31515&quot;&gt;&quot; &quot;&lt;/span&gt;+ od.OrderID + &lt;span style=&quot;color: #a31515&quot;&gt;&quot; , ProdID: &quot;&lt;/span&gt;+ od.ProductID + &lt;span style=&quot;color: #a31515&quot;&gt;&quot; : &quot;&lt;/span&gt;+ od.UnitPrice));&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;}&lt;/p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;blockquote&gt;
&lt;p&gt;SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry], [t1].[OrderID] AS [OrderID2], [t1].[ProductID], [t1].[UnitPrice], [t1].[Quantity], [t1].[Discount], (&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT COUNT(*)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM [dbo].[Order Details] AS [t2]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE [t2].[OrderID] = [t0].[OrderID]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ) AS [value]&lt;br&gt;FROM [dbo].[Orders] AS [t0]&lt;br&gt;LEFT OUTER JOIN [dbo].[Order Details] AS [t1] ON [t1].[OrderID] = [t0].[OrderID]&lt;br&gt;ORDER BY [t0].[OrderID], [t1].[ProductID]&lt;br&gt;-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8 &lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;VINET 10248 , ProdID: 11 : 14.0000&lt;br&gt;VINET 10248 , ProdID: 42 : 9.8000&lt;br&gt;VINET 10248 , ProdID: 72 : 34.8000&lt;br&gt;TOMSP 10249 , ProdID: 14 : 18.6000&lt;br&gt;TOMSP 10249 , ProdID: 51 : 42.4000&lt;br&gt;HANAR 10250 , ProdID: 41 : 7.7000&lt;br&gt;HANAR 10250 , ProdID: 51 : 42.4000&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;And we can also further load more child records as below.. by adding the LoadWith to get Product associated records from the Order Details entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;Get_Deferred_Data_With_Loading_Options()
{
      &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;dataContext = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();

      &lt;span style=&quot;color: #2b91af&quot;&gt;DataLoadOptions &lt;/span&gt;dataLoadOptions = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;DataLoadOptions&lt;/span&gt;();
      dataLoadOptions.LoadWith&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Order&lt;/span&gt;&amp;gt;(order =&amp;gt; order.Order_Details);
      dataLoadOptions.LoadWith&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Order_Detail&lt;/span&gt;&amp;gt;(od =&amp;gt; od.Product);
        
      dataContext.LoadOptions = dataLoadOptions;
      dataContext.Log = &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.Out;
      
       &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;orders = &lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;order &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;dataContext.Orders
                    &lt;span style=&quot;color: blue&quot;&gt;select &lt;/span&gt;order;

       &lt;span style=&quot;color: blue&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;Order &lt;/span&gt;order &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;orders)
       {
           &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Order_Detail&lt;/span&gt;&amp;gt; ods = order.Order_Details.ToList();
           ods.ForEach(od =&amp;gt; &lt;span style=&quot;color: #2b91af&quot;&gt;Console&lt;/span&gt;.WriteLine( od.Product.ProductName + &lt;span style=&quot;color: #a31515&quot;&gt;&quot;, &quot; &lt;/span&gt;+ order.CustomerID+ &lt;span style=&quot;color: #a31515&quot;&gt;&quot; &quot; &lt;/span&gt;+ od.OrderID + &lt;span style=&quot;color: #a31515&quot;&gt;&quot; , ProdID: &quot; &lt;/span&gt;+ od.ProductID + &lt;span style=&quot;color: #a31515&quot;&gt;&quot; : &quot; &lt;/span&gt;+ od.UnitPrice ));
       }           
}&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&amp;nbsp;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry], [t1].[OrderID] AS [OrderID2], [t1].[ProductID], [t1].[UnitPrice], [t1].[Quantity], [t1].[Discount], [t2].[ProductID] AS [ProductID2], [t2].[ProductName], [t2].[SupplierID], [t2].[CategoryID], [t2].[QuantityPerUnit], [t2].[UnitPrice] AS [UnitPrice2], [t2].[UnitsInStock], [t2].[UnitsOnOrder], [t2].[ReorderLevel], [t2].[Discontinued], (&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT COUNT(*)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM [dbo].[Order Details] AS [t3]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; INNER JOIN [dbo].[Products] AS [t4] ON [t4].[ProductID] = [t3].[ProductID]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE [t3].[OrderID] = [t0].[OrderID]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ) AS [value]&lt;br&gt;FROM [dbo].[Orders] AS [t0]&lt;br&gt;LEFT OUTER JOIN ([dbo].[Order Details] AS [t1]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; INNER JOIN [dbo].[Products] AS [t2] ON [t2].[ProductID] = [t1].[ProductID]) ON [t1].[OrderID] = [t0].[OrderID]&lt;br&gt;ORDER BY [t0].[OrderID], [t1].[ProductID]&lt;br&gt;-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8 &lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Queso Cabrales, VINET 10248 , ProdID: 11 : 14.0000&lt;br&gt;Singaporean Hokkien Fried Mee, VINET 10248 , ProdID: 42 : 9.8000&lt;br&gt;Mozzarella di Giovanni, VINET 10248 , ProdID: 72 : 34.8000&lt;/p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;Query is a bit more complex, but thats all! No more round trips to our database. Cool eh?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;AssociateWith: Filtering at the DataContext Level&lt;/h3&gt;
&lt;p&gt;This is used to specify in advance, sub-queries to limit the amount of data retrieved for any query against that particular context. Your DataContext instance will filter the data given by the Lambda you specified with AssociateWith all the time.&lt;/p&gt;
&lt;p&gt;For example, I want to only process orders where the QuantiyInOrder is less than 10 because of shipping difficulties or special offers.&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;Get_Deferred_Data_With_Loading_Options()
{
      &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;dataContext = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();
&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;      &lt;span style=&quot;color: #2b91af&quot;&gt;DataLoadOptions &lt;/span&gt;dataLoadOptions = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;DataLoadOptions&lt;/span&gt;();
      dataLoadOptions.LoadWith&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Order&lt;/span&gt;&amp;gt;(order =&amp;gt; order.Order_Details);
      dataLoadOptions.AssociateWith&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;Order&lt;/span&gt;&amp;gt;(order =&amp;gt; order.Order_Details.Where(p =&amp;gt; p.Quantity &amp;lt; 10));            &lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry], [t1].[OrderID] AS [OrderID2], [t1].[ProductID], [t1].[UnitPrice], [t1].[Quantity], [t1].[Discount], (&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT COUNT(*)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM [dbo].[Order Details] AS [t2]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ([t2].[Quantity] &amp;lt; @p0) AND ([t2].[OrderID] = [t0].[OrderID])&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ) AS [value]&lt;br&gt;FROM [dbo].[Orders] AS [t0]&lt;br&gt;LEFT OUTER JOIN [dbo].[Order Details] AS [t1] ON ([t1].[Quantity] &amp;lt; @p0) AND ([t1].[OrderID] = [t0].[OrderID])&lt;br&gt;ORDER BY [t0].[OrderID], [t1].[ProductID]&lt;br&gt;-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [10]&lt;br&gt;-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8 &lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;VINET 10248 , ProdID: 72 : 5&lt;br&gt;TOMSP 10249 , ProdID: 14 : 9&lt;br&gt;VICTE 10251 , ProdID: 22 : 6&lt;br&gt;HILAA 10257 , ProdID: 39 : 6&lt;br&gt;ERNSH 10258 , ProdID: 32 : 6&lt;br&gt;CENTC 10259 , ProdID: 37 : 1&lt;br&gt;RATTC 10262 , ProdID: 56 : 2&lt;/p&gt;
&lt;p&gt;All our queries within the scope of this DataContext will be filtered in advance rather than you having to specify a Where clause.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;DataLoadOptions in MSDN can be found below &lt;/p&gt;
&lt;p&gt;&lt;a title=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.dataloadoptions.aspx&quot; href=&quot;http://msdn2.microsoft.com/en-us/library/system.data.linq.dataloadoptions.aspx&quot;&gt;http://msdn2.microsoft.com/en-us/library/system.data.linq.dataloadoptions.aspx&lt;/a&gt;&lt;/p&gt;  </description><link>http://elsharpo.blogspot.com/2008/04/playing-around-with-linq-to-sql-using.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-8245898024547414780</guid><pubDate>Sun, 30 Mar 2008 15:35:00 +0000</pubDate><atom:updated>2008-03-31T02:36:59.186+11:00</atom:updated><title>Localisation and Profiles: Programmatically changing the user&amp;#39;s preferred culture.</title><description>&lt;p&gt;A while ago I wrote a post on how to use Localisation in ASP.NET using resource files etc. We&#39;ll since then I been asked about how to change the user&#39;s culture programmatically by clicking a button or similar.&lt;/p&gt;  &lt;p&gt;In that previous article I described how culture selection is based on browser preferences and HTTP language headers. While this is pretty good, there are many scenarios where this could be improved. Say you are travelling overseas and are accessing your appl remotely from someone&#39;s elses machine, if the culture of that browser is set to Japanese, then your application might not be readable to you. You might not be able to change your language preferences either because that might not be available in a controlled environment. &lt;/p&gt;  &lt;h3&gt;What options do we have?&lt;/h3&gt;  &lt;p&gt;In general, to get manage the user&#39;s preferences we have:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Use the browser settings ( not that great as described above but good enough for 80% of the time) &lt;/li&gt;    &lt;li&gt;When a user registers of first uses your application, they can choose language of choice and then save this in the database for later retrieval &lt;/li&gt;    &lt;li&gt;Persist the above choice in Session state or Cookies. &lt;/li&gt;    &lt;li&gt;Use ASP.NET &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/2y3fs9xs(VS.80).aspx&quot; target=&quot;_blank&quot;&gt;Profiles&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;Let the users choose this in the application. ( buttons. ddls etc )&amp;#160; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In ASP.NET is very straightforward to select the preferred culture based on the user&#39;s browser settings, you can do it at page level like I did in in my prev post but you can also set it up for all your pages in the web.config file saving you the pain of adding the attribute to all your pages throughout your application.&lt;/p&gt;  &lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;system.web&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;globalization &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;culture&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;auto&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;uiCulture&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;auto&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;fileEncoding&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;utf-8&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;requestEncoding&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;utf-8&lt;/span&gt;&amp;quot; 
            &lt;span style=&quot;color: red&quot;&gt;responseEncoding&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;utf-8&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt; ....&lt;/span&gt;&lt;/pre&gt;


&lt;pre class=&quot;code&quot;&gt;&lt;strong&gt;What if I want to save the user settings after they register/access the app for the first time?&lt;/strong&gt;&lt;/pre&gt;

&lt;p&gt;Think about the options you have to persist the preferred culture: Session: Session is not the best for many reasons, most importantly Session is not there forever, for example by default ASP.NET assumes that the user left the site after no requests have been made for 20 mins ,&amp;#160; your user might get up to get a cup of tea and his Session might expire, therefore when she gets back she might not have the application in her culture of choice. Also, you might not be able to access the Session object when you want to access the user&#39;s preferences. So I wouldn&#39;t use Session myself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about Cookies?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cookies are more favourable if all you want is to persist a simple user&#39; preference like the culture or their favourite colour I think, it will get hard pretty quickly if you where storing more than just their colour and horoscope preferences. &lt;/p&gt;

&lt;p&gt;For example in Global.asax you could implement the Application_BeginRequest event and read the cookie there and setup your CurrentThread to the value stored in the cookie that you created after the user made the choice etc.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;void &lt;/span&gt;Application_BeginRequest(&lt;span style=&quot;color: #2b91af&quot;&gt;Object &lt;/span&gt;sender, &lt;span style=&quot;color: #2b91af&quot;&gt;EventArgs &lt;/span&gt;args)
{
    &lt;span style=&quot;color: #2b91af&quot;&gt;HttpCookie &lt;/span&gt;myPreferencesCookie = (&lt;span style=&quot;color: #2b91af&quot;&gt;HttpCookie&lt;/span&gt;)Request.Cookies[&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Culture&amp;quot;&lt;/span&gt;];
    &lt;span style=&quot;color: green&quot;&gt;// check for null etc etc
    &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;culture = myPreferencesCookie.Value;
    System.Threading.&lt;span style=&quot;color: #2b91af&quot;&gt;Thread&lt;/span&gt;.CurrentThread.CurrentUICulture = 
         &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;System.Globalization.&lt;span style=&quot;color: #2b91af&quot;&gt;CultureInfo&lt;/span&gt;(culture);
    System.Threading.&lt;span style=&quot;color: #2b91af&quot;&gt;Thread&lt;/span&gt;.CurrentThread.CurrentCulture = 
         System.Globalization.&lt;span style=&quot;color: #2b91af&quot;&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(culture);
}&lt;br /&gt;&lt;p&gt; But I think it will get complicated very quickly and I don&#39;t think is the best way. Much better I think is ASP.NET&#39;s Profile&lt;/p&gt;&lt;/pre&gt;

&lt;p&gt;The Profile objects is strongly typed and persisted, and you can even implement your own ProfileProvider!&lt;/p&gt;

&lt;p&gt;The easiest way to create a Profile is by creating some properties in the your root web.config. Just like the Resource files, ASP.NET compiles the Profile&#39;s propertied dynamically and then you have this strongly typed profile assets.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;profile&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;properties&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;add &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;MyFavouriteNumber&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;allowAnonymous&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;group &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;Preferences&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;add &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;Culture&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;allowAnonymous&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;add &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;Color&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;allowAnonymous&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;&amp;quot;  &lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;
       &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;group&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;properties&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
 &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;profile&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;&lt;font size=&quot;+0&quot;&gt;Note the attributes, name is very simple, but note alllowAnonymous: This allows anonymous users to read/write properties, you have to set it explicitly because by default, this is set to false. ASP.NET cannot know which user has which profile unless the user is authenticated. So to use this anonymous feauture you have to enable anonymous indentification in your web.config too. More details in MSDN &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/91ka2e6a.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;All I did what this...&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;anonymousIdentification  &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;enabled&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;&lt;font size=&quot;+0&quot;&gt;You can also set defaultvalues, type, readonly attributes to your profile entries too.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;Also I defined what is called &#39;Profile Groups&#39;, that lets you organise the properties better and into more logical groups. Then in your app the intellisense will pick it up beautifully! ( Profile.Preferences.Culture )&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;&lt;font color=&quot;#000000&quot;&gt;So far so good, we can store it in the Profile Object but using Profile is a bit of a problem in the same league as the Sesssion Object: Can we access Profile in Global.asax BeginRequest()? Nop, we can&#39;t, the only way is to write some code to access the data store where you are persisting the user&#39;s preferences. The reason this is the case is that just like Session, Profile is not initialised until Session is ready to roll.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting started with a solution to the problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So far we know we don&#39;t want to use Session, we don&#39;t want to use Cookies and that Profile is good but we can&#39;t really use it straight out of the box. &lt;/p&gt;

&lt;p&gt;Question I had was, how can I localise pages based on the choices the user made? and what if the user wants to change back and forth this setting for say language preferences. Anyway, I started easy, lets give them a UI so they can change languages for example.&lt;/p&gt;

&lt;p&gt;I created a MasterPage and added some big, impossible to miss flags that can be associated to languages and cultures as shown below. The plan is that by clicking on the flag, the culture associated with the flags will be the Thread.Culture that the page will be running under.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R--zDD9p0-I/AAAAAAAAAQU/n1LW5glpSwE/Flags%5B4%5D.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;113&quot; alt=&quot;Flags&quot; src=&quot;http://lh3.google.com/elsharpo/R--zDj9p0_I/AAAAAAAAAQc/FFfHoPtkYx4/Flags_thumb%5B2%5D.jpg&quot; width=&quot;499&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Simple code too..just a few asp:Images..&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;ImageButton &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;CommandName&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;es-UY&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;OnCommand&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Flag_ClickedCommand&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;AlternateText&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Spanish&amp;quot;
       &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;CssClass&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Flag&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ImageUrl&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;~/Profile/FlagsImages/uy.gif&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;imgUruguay&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot;
       &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;
&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;lt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;ImageButton &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;CommandName&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;zh-CN&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;OnCommand&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Flag_ClickedCommand&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;AlternateText&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Chinese&amp;quot;
       &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;CssClass&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Flag&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ImageUrl&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;~/Profile/FlagsImages/cn.gif&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;ImageButton2&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot;
       &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;ImageButton &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;CommandName&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;en-AU&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;OnCommand&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Flag_ClickedCommand&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;AlternateText&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;English&amp;quot;
       &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;CssClass&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Flag&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ImageUrl&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;~/Profile/FlagsImages/au.gif&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;ImageButton1&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot;
       &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt; /&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;OK, note that I have setup an EventHandler for the OnCommand event, when this fires, Flag_ClickedCommand will be called and then culture that that particular flag represents will be passed on as CommandName. &lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;protected void &lt;/span&gt;Flag_ClickedCommand(&lt;span style=&quot;color: #2b91af&quot;&gt;Object &lt;/span&gt;sender, &lt;span style=&quot;color: #2b91af&quot;&gt;CommandEventArgs &lt;/span&gt;args)
{
      &lt;span style=&quot;color: blue&quot;&gt;if &lt;/span&gt;(!args.CommandName.IsValidCulture()) &lt;span style=&quot;color: blue&quot;&gt;return&lt;/span&gt;;        
       Profile.Preferences.Culture = args.CommandName;
       Response.Redirect(Request.Path);        
}&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;Note what I set the Profile.Preferences.Culture to the command name passed on from the ImageButton, but this could well be a simple button or a dropdownlist value etc. I redirect the page to itself since if the user clicks on a flag, they&#39;ll expect the changes to take change immendiately! ( I would!)&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I created an extension method for strings to check if the culture passed along was valid too. Not relevant to this post but since they are very neat I&#39;ll copy it here too. :-)&lt;/p&gt;

  &lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public static bool &lt;/span&gt;IsValidCulture(&lt;span style=&quot;color: blue&quot;&gt;this string &lt;/span&gt;cult)
{
     &lt;span style=&quot;color: blue&quot;&gt;if &lt;/span&gt;(&lt;span style=&quot;color: #2b91af&quot;&gt;Constants&lt;/span&gt;.ChineseCulture == cult
              || &lt;span style=&quot;color: #2b91af&quot;&gt;Constants&lt;/span&gt;.EnglishCulture == cult
              || &lt;span style=&quot;color: #2b91af&quot;&gt;Constants&lt;/span&gt;.SpanishCulture == cult)
     {
         &lt;span style=&quot;color: blue&quot;&gt;return true&lt;/span&gt;;
     }
     &lt;span style=&quot;color: blue&quot;&gt;return false&lt;/span&gt;;    
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Setting the Thread.CurrentThread.CurrentCulture to your preferred culture&lt;/h3&gt;

&lt;p&gt;What we now want to do is for a page to load and pick the user&#39;s preferred culture.&lt;/p&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;I noted that you can override the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.web.ui.page.initializeculture.aspx&quot; target=&quot;_blank&quot;&gt;InitializeCulture&lt;/a&gt;() method solely to intialise the page&#39;s culture. This function gets called very early in the page life cycle, well before and controls are created, meaning that if you want to get some values from controls, you must get them directly from the request using Form! &lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;protected override void &lt;/span&gt;InitializeCulture()
{       
    &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;culture = Profile.Preferences.Culture;
    &lt;span style=&quot;color: blue&quot;&gt;if &lt;/span&gt;(!culture.IsEmpty())
    {
        &lt;span style=&quot;color: #2b91af&quot;&gt;Thread&lt;/span&gt;.CurrentThread.CurrentUICulture = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;CultureInfo&lt;/span&gt;(culture);
        &lt;span style=&quot;color: #2b91af&quot;&gt;Thread&lt;/span&gt;.CurrentThread.CurrentCulture = &lt;span style=&quot;color: #2b91af&quot;&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(culture);
    }
}&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;Unfortunately you have to override this function for every page, not very practical if you have a zillion pages. Next best is to provide a base common class and then inherit from this class so all your webforms can benefit and you don&#39;t have to implement the above in every form. This works fine! &lt;/p&gt;

&lt;p&gt;Note that the Thread culture is only set for the current page and will not affect your &amp;quot;other&amp;quot; pages, initially I thought that the CurrentThread&#39;s culture will be set to the new values and we&#39;ll all happy. But then I also thought what If a user has a different entry point to the application, so the need for all your pages to be able to read and set the user&#39;s fav culture.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public partial class &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;LocalisationBase &lt;/span&gt;: System.Web.UI.&lt;span style=&quot;color: #2b91af&quot;&gt;Page
&lt;/span&gt;{
    &lt;span style=&quot;color: blue&quot;&gt;protected override void &lt;/span&gt;InitializeCulture()
    {
        &lt;span style=&quot;color: #2b91af&quot;&gt;ProfileCommon &lt;/span&gt;common = &lt;span style=&quot;color: #2b91af&quot;&gt;HttpContext&lt;/span&gt;.Current.Profile &lt;span style=&quot;color: blue&quot;&gt;as &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ProfileCommon&lt;/span&gt;;
        &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;culture = common.Preferences.Culture;

        &lt;span style=&quot;color: blue&quot;&gt;if &lt;/span&gt;(!culture.IsEmpty())
        {
            &lt;span style=&quot;color: #2b91af&quot;&gt;Thread&lt;/span&gt;.CurrentThread.CurrentUICulture = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;CultureInfo&lt;/span&gt;(culture);
            &lt;span style=&quot;color: #2b91af&quot;&gt;Thread&lt;/span&gt;.CurrentThread.CurrentCulture = &lt;span style=&quot;color: #2b91af&quot;&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(culture);
        }
    }    
}&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;So base class it was and all my other pages inherited from this.. 

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public partial class &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Profile_Default &lt;/span&gt;: &lt;span style=&quot;color: #2b91af&quot;&gt;LocalisationBase
&lt;/span&gt;{
    &lt;span style=&quot;color: blue&quot;&gt;protected void &lt;/span&gt;Page_Load(&lt;span style=&quot;color: blue&quot;&gt;object &lt;/span&gt;sender, &lt;span style=&quot;color: #2b91af&quot;&gt;EventArgs &lt;/span&gt;e)
    {
    }&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;...........&lt;/pre&gt;


&lt;p&gt;Now lets have a look at my initial UI that I wanted to localise and see if this works.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;a href=&quot;http://lh6.google.com/elsharpo/R--zET9p1AI/AAAAAAAAAQk/ad_2vs4JhMI/englishUI%5B6%5D.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;484&quot; alt=&quot;englishUI&quot; src=&quot;http://lh5.google.com/elsharpo/R--zFD9p1BI/AAAAAAAAAQs/JSnH6DlWZLQ/englishUI_thumb%5B4%5D.jpg&quot; width=&quot;553&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Just a simple form with some dates, numbers and a calendar what we hope we can localise and so the right names and formatting appear for the right culture setting. For the form labels I used resource files just like in my previous post. I have three resource files, one for english, one for spanish and one for mandarin. &lt;/p&gt;

&lt;p&gt;The currency is interesting. All I have is a number where I call ToString(&amp;quot;c&amp;quot;)&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;private void &lt;/span&gt;FillPersonalisedMoney()
{
    lblMoney.Text = (500.23D).ToString(&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;c&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;Now when the user click on the Chinese flag...then the Chinese version of the page will render, dates will be rendered accordingly and the calendar will be in Chinese too..&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R--zFz9p1CI/AAAAAAAAAQ0/DVeDnDabUCI/chinaUI%5B9%5D.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;484&quot; alt=&quot;chinaUI&quot; src=&quot;http://lh3.google.com/elsharpo/R--zGj9p1DI/AAAAAAAAAQ8/EkS2tfa3QUw/chinaUI_thumb%5B7%5D.jpg&quot; width=&quot;550&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;And in Spanish too...&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R--zHT9p1EI/AAAAAAAAARE/kHGmnx1MQpQ/spanishUI%5B3%5D.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;484&quot; alt=&quot;spanishUI&quot; src=&quot;http://lh4.google.com/elsharpo/R--zHz9p1FI/AAAAAAAAARM/Hnp3J1JqqT8/spanishUI_thumb%5B1%5D.jpg&quot; width=&quot;553&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Clicking on Employee Details will take you to a page that also inherits from LocalisationBase and where Employees from ubiquitous Northwind will be displayed with to their likely annoyance, their date of birth, nicely formatted according to the culture of choice!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R--zIz9p1GI/AAAAAAAAARU/X-Ogxzlh65w/EmployeesDetails%5B4%5D.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;268&quot; alt=&quot;EmployeesDetails&quot; src=&quot;http://lh3.google.com/elsharpo/R--zJj9p1HI/AAAAAAAAARc/D4V9rpjB0F0/EmployeesDetails_thumb%5B2%5D.jpg&quot; width=&quot;537&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R--zKj9p1II/AAAAAAAAARk/Wh1YZ_w0eZ4/EmployeesChineseDetails%5B4%5D.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;324&quot; alt=&quot;EmployeesChineseDetails&quot; src=&quot;http://lh6.google.com/elsharpo/R--zLT9p1JI/AAAAAAAAARs/PA96NwE782U/EmployeesChineseDetails_thumb%5B2%5D.jpg&quot; width=&quot;539&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;And there you have it. &lt;/p&gt;

&lt;p&gt;In conclusion I used the native Profile ASP.NET objects, you can implement your own Provider or you can use SQLProfileProvider that ASP.NET offers, either way I honestly think that these guys offer you much more flexibility and power when you want to customize your apps according to user&#39;s preferences.&lt;/p&gt;

&lt;p&gt;Au revoir!&lt;/p&gt;

&lt;p&gt;Resources:&lt;/p&gt;

&lt;p&gt;#Must read for any localisation entrepreneurs&lt;/p&gt;

&lt;p&gt;&lt;a title=&quot;http://quickstarts.asp.net/QuickStartv20/aspnet/doc/localization/default.aspx&quot; href=&quot;http://quickstarts.asp.net/QuickStartv20/aspnet/doc/localization/default.aspx&quot;&gt;http://quickstarts.asp.net/QuickStartv20/aspnet/doc/localization/default.aspx&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;# ASP.NET 2.0 Localization (MSDN Article)&lt;/p&gt;

&lt;p&gt;&lt;a title=&quot;http://msdn2.microsoft.com/en-us/library/ms379546.aspx&quot; href=&quot;http://msdn2.microsoft.com/en-us/library/ms379546.aspx&quot;&gt;http://msdn2.microsoft.com/en-us/library/ms379546.aspx&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;#Profiles&lt;/p&gt;

&lt;p&gt;&lt;a title=&quot;http://msdn2.microsoft.com/en-us/library/at64shx3(VS.80).aspx&quot; href=&quot;http://msdn2.microsoft.com/en-us/library/at64shx3(VS.80).aspx&quot;&gt;http://msdn2.microsoft.com/en-us/library/at64shx3(VS.80).aspx&lt;/a&gt;&lt;/p&gt;  </description><link>http://elsharpo.blogspot.com/2008/03/localisation-and-profiles.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-7608550459170963255</guid><pubDate>Sat, 29 Mar 2008 03:29:00 +0000</pubDate><atom:updated>2008-03-29T15:48:31.255+11:00</atom:updated><title>Caching Awareness Day: Part 1</title><description>&lt;p&gt;There are probably zillions of posts, tutorials etc around on how to do caching in ASP.NET. However a zillion+1 will not hurt. This post is more for sel-reference more than anything. I admit that sometimes I need to go over features I haven&#39;t used in a while (or ever?) to refresh my mind, this is one of those days. &lt;/p&gt;  &lt;p&gt;In my current job we have an application that was suffering from a performance hangover when retrieving some records from WebServices and the db. I thought for a while how caching can improve the user experience of some of guilty pages. &lt;/p&gt;  &lt;p&gt;After a few months in the thinking room waiting for the go ahead from the bosses, we got right into it. The idea was simple: when a particular record was requested from the services it was first stored in the Cache object and any subsequent request for that data will first check in the Application Cache to see if it&#39;s there, if there retrieve it, else go to the WebServices, get it, store it in the Cache etc etc. Needless to say the results where fantastic, the users are happy and they love their &#39;new&#39; app. We are happy, but I kicked myself in the guts why didn&#39;t I do it in the first place!&lt;/p&gt;  &lt;p&gt;Anyway, enough yarns. I plan to post a series on Caching and the different techniques that can be used to put some Caching love into your apps.&lt;/p&gt;  &lt;h3&gt;What is Caching?&lt;/h3&gt;  &lt;p&gt;Caching is all about storing data, objects, pages, part of pages etc in memory immediately after is requested from the original source. These objects are usually stored for example on the Web server, proxy servers or even the client browser. Think of the benefits, much faster to retrieve data from a proxy server &#39;nearby&#39;&amp;#160; for example, where this data has been previously requested and stored,&amp;#160; than retrieving the data from probably a data source thousands of miles away and likely to be some data that was computationally expensive to generate and retrieve.&lt;/p&gt;  &lt;p&gt;ASP.NET provides two types of caching:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Output Caching, where you can store page and controls responses from the original server to the requesting browser. &lt;/li&gt;    &lt;li&gt;Data Caching, where you can programmatically store objects to the server&#39;s memory so that the application can fetch them from there rather than recreating them. &lt;/li&gt; &lt;/ol&gt;  &lt;h3&gt;Caching ASP.NET Pages: Output Caching&lt;/h3&gt;  &lt;p&gt;Page output caching allows for subsequent requests for a given page to be provided from the cache so that the code that initially created the page does not execute. This is good for pages that are static and that you are confident are very frequently accessed pages. However if you have content on your page that is generated dynamically or has some dependency on a parameter or even a random image, then the same item will be displayed for the duration of the caching! beware! :-)&lt;/p&gt;  &lt;p&gt;To enable OutputCaching, you add a &lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;@ &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;OutputCache&lt;/span&gt; directive to a page. For example, in the page below I have cached this page for 10 seconds.&lt;/p&gt;  &lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;@ &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;OutputCache &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;VaryByParam&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;none&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Duration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;10&amp;quot; &lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The contents of your page will not be regenerated each time a user requests the page, also the class behind the page will not be executed during that time. This is the most basic kind of OutputCaching you can set up for a page, although very limited and only useful for very static pages. &lt;/p&gt;

&lt;p&gt;Moving on, let&#39;s see all the properties that can be set for the OutputCache directive. &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R-3JZD9p02I/AAAAAAAAAPU/8jIKmsd0QCM/ouputIntellisense4.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;287&quot; alt=&quot;ouputIntellisense&quot; src=&quot;http://lh6.google.com/elsharpo/R-3JZz9p03I/AAAAAAAAAPc/y0aI0krPb-A/ouputIntellisense_thumb2.jpg&quot; width=&quot;531&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Note that there is a property called &#39;VaryByParam&amp;quot;. This one is very useful. For example, typical Master/Detail scenario, say you have a page where you show all Categories of Products in the Northwind database.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R-3Jaj9p04I/AAAAAAAAAPk/kqvBOkK7k8o/categories7.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;203&quot; alt=&quot;categories&quot; src=&quot;http://lh4.google.com/elsharpo/R-3JbT9p05I/AAAAAAAAAPs/Yp8UnZ03Jsc/categories_thumb5.jpg&quot; width=&quot;492&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;..and then you wanted to display the Products for each category in&amp;#160; a different page, passing the CategoryID in the query string. &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R-3Jbz9p06I/AAAAAAAAAP0/Ir6ylgpzzsI/Capture24.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;37&quot; alt=&quot;Capture2&quot; src=&quot;http://lh4.google.com/elsharpo/R-3JcT9p07I/AAAAAAAAAP8/CiaCiqjtyRM/Capture2_thumb2.jpg&quot; width=&quot;607&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R-3Jcz9p08I/AAAAAAAAAQE/rVeWy6lA-LM/Capture33.jpg&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;136&quot; alt=&quot;Capture3&quot; src=&quot;http://lh4.google.com/elsharpo/R-3JdT9p09I/AAAAAAAAAQM/_u4HTnL7uwE/Capture3_thumb1.jpg&quot; width=&quot;471&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Now if you did the above setup for your details page, then all your users will see whatever was cached first. &lt;/p&gt;

&lt;p&gt;Enter VaryByParam. This helps you by making ASP.NET cache a new instance of the page when a different value for the CategoryID query string parameter is passed to the Products page.&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;@ &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;OutputCache &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Duration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;100&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;VaryByParam&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;CategoryID&amp;quot; &lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;You can also vary the output cache by multiple parameters by separating the list of params by semicolon in the VaryByParam attribute ..&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;@ &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;OutputCache &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Duration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;100&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;VaryByParam&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;CategoryID;ProductID&amp;quot; &lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Also you can generate a new version of the page when any of the params change by assigning a&amp;#160; &lt;strong&gt;* &lt;/strong&gt;to the attribute.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;@ &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;OutputCache &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Duration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;100&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;VaryByParam&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;*&amp;quot; &lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;More information in caching by param can be found in &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/y96218s9(VS.71).aspx&quot; target=&quot;_blank&quot;&gt;MSDN&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;h3&gt;OutputCaching by browser Header&lt;/h3&gt;

&lt;p&gt;It is possible to also use the VaryByHeader attribute to create different versions of a page according to the browser header. For example, my Firefox browser sends the below set of headers with information.&lt;/p&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;&lt;b&gt;Connection&lt;/b&gt;: keep-alive

  &lt;br /&gt;&lt;b&gt;Keep-Alive&lt;/b&gt;: 300

  &lt;br /&gt;&lt;b&gt;Accept&lt;/b&gt;: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5

  &lt;br /&gt;&lt;b&gt;Accept-Charset&lt;/b&gt;: ISO-8859-1,utf-8;q=0.7,*;q=0.7

  &lt;br /&gt;&lt;b&gt;Accept-Encoding&lt;/b&gt;: gzip,deflate

  &lt;br /&gt;&lt;b&gt;Accept-Language&lt;/b&gt;: en-gb,en;q=0.5

  &lt;br /&gt;&lt;b&gt;Host&lt;/b&gt;: localhost

  &lt;br /&gt;&lt;b&gt;User-Agent&lt;/b&gt;: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13&lt;/p&gt;

&lt;p&gt;My IE7 sends:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Connection&lt;/b&gt;: Keep-Alive

  &lt;br /&gt;&lt;b&gt;Accept&lt;/b&gt;: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight, application/x-shockwave-flash, application/x-silverlight-2-b1, */*

  &lt;br /&gt;&lt;b&gt;Accept-Encoding&lt;/b&gt;: gzip, deflate

  &lt;br /&gt;&lt;b&gt;Accept-Language&lt;/b&gt;: en-AU,es-UY;q=0.7,zh-CN;q=0.3

  &lt;br /&gt;&lt;b&gt;Host&lt;/b&gt;: localhost

  &lt;br /&gt;&lt;b&gt;User-Agent&lt;/b&gt;: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SV1; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022)

  &lt;br /&gt;&lt;b&gt;UA-CPU&lt;/b&gt;: x86&lt;/p&gt;

&lt;p&gt; You can check what your browser is sneaking out &lt;a href=&quot;http://pgl.yoyo.org/http/browser-headers.php&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt; or &lt;a href=&quot;http://www.xhaus.com/headers&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt; ( as an aside, I heard that in the in 2001 a browser had some customer headers that were used to sneak information about that particular user...&lt;a href=&quot;http://www.grc.com/su/earthlink.htm&quot; target=&quot;_blank&quot;&gt;read it&lt;/a&gt; and see). In .Net I did the above by calling and then iterating the collection.&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NameValueCollection &lt;/span&gt;headerCollection = Request.Headers;&lt;/p&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;br&gt;
So......&lt;br&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;@ &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;OutputCache &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Duration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;1000&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;VaryByParam&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;none&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;VaryByHeader&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Accept-Language&amp;quot; &lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;That will cache a different page version according to the language used by the user.Right?&lt;/p&gt;

&lt;h3&gt;...Cache by Browser, getting introduced to the very handy VaryByCustom :-)&lt;/h3&gt;

&lt;p&gt;You can also cache by the user&#39;s browser by using VaryByCustom attribute and using the &#39;special&#39; browser attribute. By using this attribute, the page will be cached for each mayor browser, its&amp;#160;&amp;#160; name and version, eg IE7 and IE8 will result in two different cached versions.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;@ &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;OutputCache &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Duration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;1000&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;VaryByParam&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;none&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;VaryByCustom&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;browser&amp;quot; &lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;VaryByCustom is very handy. Basically what you can do is write a function in Global.asax or write an HttpModule to handle caching for pages in any way you want.&lt;/p&gt;

&lt;p&gt;In Global.asax you do it by overriding the GetVaryCustomString() function.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public override string &lt;/span&gt;GetVaryByCustomString(&lt;span style=&quot;color: #2b91af&quot;&gt;HttpContext &lt;/span&gt;context, &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;custom)
{
     &lt;span style=&quot;color: blue&quot;&gt;return base&lt;/span&gt;.GetVaryByCustomString(context, custom);
}&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;font face=&quot;Trebuchet MS&quot;&gt;The function takes two parameters, the HttpContext for the request and a string which is the value you can set in VaryByCustom=&amp;quot;[custom_string]&amp;quot;, this string can be &lt;/font&gt;&lt;font face=&quot;Trebuchet MS&quot;&gt;also be a nunber of string passed along separated by semicolon and you can then split it in GetVaryByCustomString() &lt;/font&gt;&lt;font face=&quot;Trebuchet MS&quot;&gt;The context gives you access to everything you expect from HttpContext, session, Request etc etc, &lt;/font&gt;&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;font face=&quot;Trebuchet MS&quot;&gt;Example: Say you want to cache pages according to the bosses&#39;s mood. &lt;/font&gt;&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;@ &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;OutputCache &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Duration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;1000&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;VaryByParam&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;none&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;VaryByCustom&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;BossHappinessLevel&amp;quot; &lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;font face=&quot;Trebuchet MS&quot;&gt;Then your custom function could look like..&lt;/font&gt;&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;  
&lt;span style=&quot;color: blue&quot;&gt;public override string &lt;/span&gt;GetVaryByCustomString(&lt;span style=&quot;color: #2b91af&quot;&gt;HttpContext &lt;/span&gt;context, &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;custom)
{
    &lt;span style=&quot;color: blue&quot;&gt;if &lt;/span&gt;(custom == &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;BossHappinessLevel&amp;quot;&lt;/span&gt;)
    {
        &lt;span style=&quot;color: #2b91af&quot;&gt;MyAjaxEnabledService &lt;/span&gt;a = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;MyAjaxEnabledService&lt;/span&gt;();
        &lt;span style=&quot;color: blue&quot;&gt;return &lt;/span&gt;a.BossHappinessLevel().ToString();       
    }
    &lt;span style=&quot;color: blue&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;default&amp;quot;&lt;/span&gt;;        
}&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;Based on the string passed, the logic will just return a string that can uniquely identify a request and thus cache a different version of the page according to the current mood! ( The above example doesn&#39;t make any sense if your boss is agro all the time, meaning that the &amp;quot;angry&amp;quot; page will be cached most of the time. Of course for this to make more sense, your page will return different content according to the mood too..if he/she is angry or happy or just indifferent. Please drop me an email if you don&#39;t get it, I need a drink now! LOL&lt;/p&gt;

&lt;h3&gt;Caching profiles in Web.Config&lt;/h3&gt;

&lt;p&gt;Instead of setting the cache policy for each page individually, you can configure page caching in the web.config and then apply your killer settings to lots of pages...&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;system.web&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;caching&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;outputCacheSettings&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
         &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;outputCacheProfiles&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
               &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;add &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;duration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;1000&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;ByBossHappiness&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;varyByParam&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;none&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;varyByCustom&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;BossHappinessLevel&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;
               &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;add &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;duration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;1000&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;ByBrowser&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;varyByCustom&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;browser&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;varyByParam&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;none&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;
         &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;outputCacheProfiles&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
       &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;outputCacheSettings&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;caching&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;system.web&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;Then your pages can recall any of the cacheprofiles by using the CacheProfile attribute of OutputCache&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;@ &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;OutputCache &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;CacheProfile&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;ByBossHappiness&amp;quot; &lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;h4&gt;Next post:&lt;/h4&gt;

&lt;p&gt;In my next post I&#39;d like to investigate how to do the above programmatically and also expire pages in code due to some event for example, if someone in the Northwind database adds a new Product by using the Reponse.RemoveOutputCacheItem() function.&lt;/p&gt;

Other things left to do include:&lt;br&gt;

&lt;ul&gt;
  &lt;li&gt;Partial Page caching.&lt;/li&gt;

  &lt;li&gt;UserControl caching.&lt;/li&gt;

  &lt;li&gt;Data caching&lt;/li&gt;

  &lt;li&gt;Caching by dependencies&lt;/li&gt;

  &lt;li&gt;SQL caching policies by using dependencies!&lt;/li&gt;
&lt;/ul&gt;  </description><link>http://elsharpo.blogspot.com/2008/03/caching-awareness-day-part-1.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-6127037308156271730</guid><pubDate>Tue, 18 Mar 2008 14:04:00 +0000</pubDate><atom:updated>2008-03-19T01:30:22.175+11:00</atom:updated><title>Localisation in ASP.NET: How to write multilingual applications</title><description>&lt;p&gt;I been thinking of learning how to do this for a while now, number of reasons for that, my first language is not English, is Spanish, Uruguayan Spanish to be more precise, those that hear me from day to day can witness my struggles with English from time to time. Sometimes you wake up and words just flow, some other times, well.. that&#39;s not quite the case.&lt;/p&gt;  &lt;p&gt;I always wondered how applications be developed that can then be easily extended to support other languages. Especially in this global village we live in today, in our Pacific region for example there are so many languages that is just mind blowing, from Oceania, South-East Asia, to Japan, the Koreas and even to the other side of the Pacific Ocean there is South and Central America. &lt;/p&gt;  &lt;p&gt;Also by pure coincidance a couple of months we were asked to provide an English language version to a&amp;#160; charity website we have been maintaining at home for a number of years now called &lt;a href=&quot;http://www.uruguayosunidos.com/&quot; target=&quot;_blank&quot;&gt;uruguayosunidos&lt;/a&gt; (I&#39;m not responsible for a terrible spelling mistake on the homepage).&amp;#160; Problem is this site is just a plain html site where unfortunately we cannot use localisation. :-(&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Enter ASP.NET 2.0 and resource files&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;One common approach in ancient times was to provide two versions of the same web, one for each language for example, one for Spanish another for English. This can clearly turn into a nightmarish scenario when maintaining the app, imagine!. The good news is that when creating an application with ASP.NET 2.0 or later you can use resource files to contain language specific content, eg one resx file can contain your French content, another can contain your English specific text etc.&lt;/p&gt;  &lt;p&gt;ASP.NET supports two types of resources, local and global resources, local resources are scoped to a particular page while a global resource is scoped to the entire application. The equation is simple, if you want content specifically for a particular page, use local resources, now if you want the text that say what your &#39;Save&#39; button should display through your whole app, then this &lt;strong&gt;should&lt;/strong&gt; be in your global resource file. &lt;/p&gt;  &lt;p&gt;To discover how this all works together I first put some labels and textboxes into an aspx page and got right into it. To get started, I just want to display a simple form where a user can enter her first name, last name and a button to submit the entries.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9_LisXzkAI/AAAAAAAAAL0/xheDvM1RuVQ/localisation_1%5B4%5D&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;91&quot; alt=&quot;localisation_1&quot; src=&quot;http://lh5.google.com/elsharpo/R9_LjsXzkBI/AAAAAAAAAL8/_KlFoaTGImc/localisation_1_thumb%5B2%5D&quot; width=&quot;539&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Label &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;lblFirstName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Name&amp;quot;  /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;TextBox &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;txtFirstName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot;/&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Label &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;lblLastName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Last Name&amp;quot; /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;TextBox &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;txtLastName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;ButtonRow&amp;quot;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;colspan&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;4&amp;quot;&amp;gt;&amp;lt;a&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;sp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Button &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;btnSave&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;CssClass&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Button&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Save&amp;quot; /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;    
&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;How to localise Page using Explicit Localisation Expressions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So far nothing new. What I wanted to do was render those controls text&#39;s either in English, Spanish or Chinese according to the user&#39;s preferred language. &lt;/p&gt;

&lt;p&gt;For that to happen first of all what I had to do was &#39;localise&#39; the page, first I had to modify the controls to use resources&#39;s expressions. This expression is going to get the content from the resource files according to the user&#39;s language of choice. For example:&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Label &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;lblFirstName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&#39;&lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;$ Resources:lblFirstName &lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&#39; /&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;The value of the Text property is now an &amp;quot;Explicit&amp;quot; resource expression, it is called Explicit because the property is set explicitly to a given value in the resource file for the preferred language.&lt;/p&gt;

&lt;p&gt;Also you have to set the Page directive to include UICulture to auto, so when a user requests the page, a file matching her settings will be loaded automatically.&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;@ &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Page &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Language&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;C#&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;UICulture&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;auto&amp;quot;...&lt;/span&gt;&lt;/p&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;Associate a resource file to a Page&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I created three resource files for this page and I then had to adde them to a special folder called &lt;em&gt;App_LocalResources&lt;/em&gt;. You have to create this folder within the same folder as the page you want to localise.&lt;/p&gt;

&lt;p&gt;Right-click where you want to add the folder and then &lt;em&gt;Add ASP.NET folder-&amp;gt;App_LocalResources,&lt;/em&gt;&amp;#160; once this is done, I was ready to add the resource files to the folder. To do this right click on the &lt;em&gt;App_LocalResources --&amp;gt; &lt;/em&gt;Add New Item and selecting Resource File as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R9_LkMXzkCI/AAAAAAAAAME/h0TJuwNCEDg/localisation_3%5B6%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;381&quot; alt=&quot;localisation_3&quot; src=&quot;http://lh6.google.com/elsharpo/R9_Lk8XzkDI/AAAAAAAAAMM/WaI_uXIsW9U/localisation_3_thumb%5B4%5D&quot; width=&quot;506&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;: To associate a resource file to a particular page, the naming convention is &lt;font color=&quot;#ff0000&quot;&gt;&lt;strong&gt;very very important&lt;/strong&gt;&lt;/font&gt;.&lt;/p&gt;

&lt;p&gt;It has to be in the format of &lt;strong&gt;page_Name.[Culture_Name].resx&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;See how above I have my Default.aspx page, I want to associate a resource file to deal with users who&#39;s language of choice is Spanish-Uruguayan (es-UY)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9_LlcXzkEI/AAAAAAAAAMU/pjwcrYJQFJU/localisation_2%5B5%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;168&quot; alt=&quot;localisation_2&quot; src=&quot;http://lh6.google.com/elsharpo/R9_Ll8XzkFI/AAAAAAAAAMc/Kal_GH3lbz8/localisation_2_thumb%5B3%5D&quot; width=&quot;343&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;I added three files, a default resource file that is used in case none of the other resource files match the user&#39;s preference/settings (it is strongly recommended to always have a default resource file) and then the files for spanish and chinese simplified. Also note that in this example I have my Default.aspx page and that at the same level I added the &lt;em&gt;App_LocalResources&lt;/em&gt; folder.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;You can also add a &#39;neutral&#39; culture resource file and name it in our case Default.aspx.en.resx, in this case the language is set to English without specifying the specific culture name as say AU for Australian, this is used if the user&#39;s preferred language is english but not say Australian english. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Resource Files:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Visual Studio has an editor for the resource files which enables you to enter name and value pairs as shown below..&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9_LmcXzkGI/AAAAAAAAAMk/int9qsbgUQs/localisation_4%5B5%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;194&quot; alt=&quot;localisation_4&quot; src=&quot;http://lh3.google.com/elsharpo/R9_LnMXzkHI/AAAAAAAAAMs/QuUXUeX6ziA/localisation_4_thumb%5B3%5D&quot; width=&quot;750&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Note that I have associated for my Uruguayan Spanish page the name of the properties to their corresponding values that I will then retrieve from my aspx page.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Resource files are nothing more than XML files that are then compiled by the framework into assemblies in the background! Open them in Notepad and see!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9_LnsXzkII/AAAAAAAAAM0/kPC0IBtQnfI/localisation_5%5B3%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;203&quot; alt=&quot;localisation_5&quot; src=&quot;http://lh3.google.com/elsharpo/R9_LoMXzkJI/AAAAAAAAAM8/0xoV1pYJGJc/localisation_5_thumb%5B1%5D&quot; width=&quot;272&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once I &#39;localise&#39; all the controls in the page, my aspx looked&amp;#160; like this.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Label &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;lblFirstName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&#39;&lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;$ Resources:lblFirstName &lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&#39; /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;TextBox &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;txtFirstName_Ex&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot;/&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Label &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;lblLastName_Ex&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&#39;&lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;$ Resources:lblLastName &lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&#39; /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;TextBox &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;txtLastName_Ex&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot;/&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;ButtonRow&amp;quot;&amp;gt;
       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;colspan&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;4&amp;quot;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Button &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;btnSave&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;CssClass&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Button&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&#39;&lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;$ Resources:btnSave &lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&#39; /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;    
&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now is up to the user&#39;s settings to see what gets loaded. I&#39;m going to setup my preferred language in IE7 to Spanish and see what happens..&lt;/p&gt;

&lt;p&gt;To do this: Tools-&amp;gt;Internet Options -&amp;gt; Languages button at the bottom. I then added Spanish Uruguay and Moved it to the top to set it as my preferred language as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R9_LpMXzkKI/AAAAAAAAANE/J2-zMfPPoWY/localisation_6%5B4%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;421&quot; alt=&quot;localisation_6&quot; src=&quot;http://lh6.google.com/elsharpo/R9_Lp8XzkLI/AAAAAAAAANM/TtL8ZhTMbtY/localisation_6_thumb%5B2%5D&quot; width=&quot;371&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now is all ready to go and loading the page should display my labels and button in Spanish.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9_LqcXzkMI/AAAAAAAAANU/3ir837El6Uc/localisation_7%5B4%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;87&quot; alt=&quot;localisation_7&quot; src=&quot;http://lh6.google.com/elsharpo/R9_Lq8XzkNI/AAAAAAAAANc/sILE1WenXC0/localisation_7_thumb%5B2%5D&quot; width=&quot;515&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;And yeah! it worked, look how I associated the lblFirstName property in the Resource file:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Label &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;lblFirstName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&#39;&lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;$ Resources:lblFirstName &lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&#39; /&amp;gt; &lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9_LrcXzkOI/AAAAAAAAANk/NgcPHmk7zvw/localisation_8%5B3%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;83&quot; alt=&quot;localisation_8&quot; src=&quot;http://lh6.google.com/elsharpo/R9_Lr8XzkPI/AAAAAAAAANs/oHDmzkTXPhk/localisation_8_thumb%5B1%5D&quot; width=&quot;265&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&amp;#160;&lt;/span&gt;&lt;/p&gt;
&lt;span style=&quot;color: blue&quot;&gt;&lt;/span&gt;

&lt;p&gt;If I then set Chinese to my preferred language ( provided I have the Chinese culture resource file as shown below)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9_LssXzkQI/AAAAAAAAAN0/F-YUP13nIWg/localisation_9%5B3%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;140&quot; alt=&quot;localisation_9&quot; src=&quot;http://lh6.google.com/elsharpo/R9_Lt8XzkRI/AAAAAAAAAN8/Tjzc22BalYs/localisation_9_thumb%5B1%5D&quot; width=&quot;644&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;u&gt;Thanks Jerry for the characters! I hope it means what we intend to ;-)&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Then loading the page shows the correct content.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R9_LvMXzkSI/AAAAAAAAAOE/OBCVmTPUIqY/localisation_10%5B4%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;94&quot; alt=&quot;localisation_10&quot; src=&quot;http://lh5.google.com/elsharpo/R9_LvsXzkTI/AAAAAAAAAOM/BI7HqMxMWcI/localisation_10_thumb%5B2%5D&quot; width=&quot;518&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Implicit Localisation Expressions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An alternative to explicit expressions is that you use implicit localisation which is very handy if you want to localise controls throughout your page with just one resource key.&amp;#160; The resource expression looks as follows:&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
     &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Label &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;lblFirstName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Name&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;meta&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;resourceKey&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;lblFirstName&amp;quot; /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
     &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;TextBox &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;txtFirstName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;TextBox&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
     &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Label &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;lblLastName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Last Name&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;meta&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;resourceKey&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;lblLastName&amp;quot; /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
     &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;TextBox &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;txtLastName&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;TextBox&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;ButtonRow&amp;quot;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;colspan&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;4&amp;quot;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Button &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;btnSave&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;CssClass&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Button&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;Save&amp;quot; &lt;span style=&quot;color: #a31515&quot;&gt;meta&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;resourceKey&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;btnSave&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt; /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
   &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;    
&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;The labels and button include the &lt;span style=&quot;color: #a31515&quot;&gt;meta&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;resourceKey&lt;/span&gt; property that represents a key in the local resource file. The entries are set to the Text property of the labels and button. I noted that you have to define all the meta resourceKey for each localisation resource file you have.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R9_LwMXzkUI/AAAAAAAAAOU/pYE4Nt4j_zg/localisation_11%5B3%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;139&quot; alt=&quot;localisation_11&quot; src=&quot;http://lh5.google.com/elsharpo/R9_LwsXzkVI/AAAAAAAAAOc/HrmfCW0t9DA/localisation_11_thumb%5B1%5D&quot; width=&quot;644&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The advantages of using implicit localisation is that you can use the content for multiple controls, say you have 4+ &#39;Save&#39; buttons through your page, all you need to do is associate a resource key to the controls. Also it is easier to localise existing apps, all that needs to be done is add meta:resourceKey=&amp;quot;Property_Name&amp;quot; attributes&amp;#160; to your existing controls.&lt;/p&gt;

&lt;p&gt;Note that is also possible to load resources programmatically by using GetLocalResourceObject(String name):Object and that you can also used it to format text, you can store values in the resource file such as &amp;quot;Welcome {0}!&amp;quot; and then at run time format the string as necessary.&amp;#160; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Global Resources:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While local resources are cool, their limitation is that they are relevant at a page level scope rather than at an application scope. A global resource can then be used through the app for you to setup the localised text. By convention, any resource that you want to be available to all your pages, controls etc should be in a global resource file.&lt;/p&gt;

&lt;p&gt;To add global resource files you need to first add another special folder to your solution, this time the folder must be at the root of your application. To add it right-click on your application node --&amp;gt; Add ASP.NET folder --&amp;gt; &lt;em&gt;App_GlobalResources&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Then you just add resource files as I did it for local page level, following the same naming convention as shown before for the language and culture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resource_File_Name.[Culture_Name].resx&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9_LxcXzkWI/AAAAAAAAAOk/ofB2_gsZr6s/localisation_12%5B4%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;109&quot; alt=&quot;localisation_12&quot; src=&quot;http://lh6.google.com/elsharpo/R9_Lx8XzkXI/AAAAAAAAAOs/bnoSDXcqmeY/localisation_12_thumb%5B2%5D&quot; width=&quot;509&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;I called this file Resource.es-UY.resx&lt;/p&gt;

&lt;p&gt;Now the interesting thing comes when you want to retrieve the values from a global resource file. The sytnax is slightly different and it is as follows:&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;   &amp;lt;h2&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Literal &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;litWelcome&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&#39;&lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;$ Resources:Resource,Welcome &lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&#39; /&amp;gt;&amp;lt;/h2&amp;gt;
&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;With global resources you have to specify the name of the resource file and then the property you want to get to. &lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9_LycXzkYI/AAAAAAAAAO0/prqZbRMAwrw/localisation_13%5B4%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;145&quot; alt=&quot;localisation_13&quot; src=&quot;http://lh6.google.com/elsharpo/R9_Ly8XzkZI/AAAAAAAAAO8/XEZpv0fDiPs/localisation_13_thumb%5B2%5D&quot; width=&quot;579&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;It is also possible to retrieve values from global resources programmatically using GetGlobalResourceObject(string, string) method that takes two parameters, name of the global resource page and name of the property key you want to retrieve.&lt;/p&gt;

&lt;p&gt;Because the .net framework converts global resources into compiled classes, you can use strongly typed expressions to retrieve values from the resource files. When you create a resource file, a new class is added to the Resource namespace for you to use! How good is that!!? The snippet&amp;#160; shown below with Visual Studio intellisense doing the damage for the above example. Wicked!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9_LzsXzkaI/AAAAAAAAAPE/2UdREQRiBRk/localisation_14%5B7%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;222&quot; alt=&quot;localisation_14&quot; src=&quot;http://lh3.google.com/elsharpo/R9_L0MXzkbI/AAAAAAAAAPM/QKmGyABEBTk/localisation_14_thumb%5B3%5D&quot; width=&quot;644&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Last but not least there is the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/ms228255.aspx&quot; target=&quot;_blank&quot;&gt;Localize&lt;/a&gt; control. It is used when you want to localise bigger pieces of text in your apps/pages. It works the same way as described above using meta:resourceKey at page level or using the global resource as in the last example. I can&#39;t see a super good use for it since it seems very similar to the Literal and Label controls. Jury still out.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Localize &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;elLocalisator&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&#39;&lt;/span&gt;&lt;span style=&quot;background: #ffee62&quot;&gt;&amp;lt;%&lt;/span&gt;$ Resources:Resource,GoodMorning &lt;span style=&quot;background: #ffee62&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&#39; /&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;That all!&lt;/p&gt;

&lt;p&gt;This was a very interesting post to write, learned some tricks, there&#39;s still a lot to cover for example, how to format currency and DateTime according to culture, setting cultures programmatically or in web.config or page level. Maybe is there is interest we can do it later. &lt;/p&gt;

&lt;p&gt;Hope it helps someone out there!&lt;/p&gt;

&lt;p&gt;el#&lt;/p&gt;


&lt;p&gt;Resources:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;MSDN article tree for localisation can be found &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/c6zyy3s9.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;How to generate a resource file for you page automatically. ( &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/c6zyy3s9.aspx&quot; target=&quot;_blank&quot;&gt;Visual Studio will do it for you&lt;/a&gt;!)&lt;/li&gt;

  &lt;li&gt;CultureInfo Class &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;  </description><link>http://elsharpo.blogspot.com/2008/03/localisation-in-aspnet-how-to-write.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-5445680804158647920</guid><pubDate>Mon, 17 Mar 2008 14:04:00 +0000</pubDate><atom:updated>2008-03-18T01:04:03.837+11:00</atom:updated><title>How to change IE7 default &amp;#39;View Page Source&amp;#39; text viewer in Windows XP/Vista</title><description>&lt;p&gt;I really love Notepad&amp;lt;/grin&amp;gt;, I think it does a wonderful job with the really simple things, like a quick and dirty way of opening text files, web.configs (if you are in a rush only of course).&lt;/p&gt;  &lt;p&gt;Now, if you are a web developer and say you want to see the source code of any given page or you might get a JavaScript error in line 1209 and want to have a look at it..then honestly NotePad does not cut it really. &lt;/p&gt;  &lt;p&gt;I use Notepad++ at work &lt;a title=&quot;http://notepad-plus.sourceforge.net/uk/site.htm&quot; href=&quot;http://notepad-plus.sourceforge.net/uk/site.htm&quot;&gt;http://notepad-plus.sourceforge.net/uk/site.htm&lt;/a&gt; and so far I&#39;m a very happy chap.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;So how do you change to say..Vi in Windows XP&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Download TweakUI from &lt;a title=&quot;http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx&quot; href=&quot;http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx&quot;&gt;http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Start Tweak UI and update your view source editor under Internet Explorer | View Source, and then point it to the exe of your editor of choice. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;But mate, what about Vista?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;oh yeah!, TweakUI does not work in Vista so I read this IE7 blog entry in the msdn blogs&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;a title=&quot;http://blogs.msdn.com/ie/archive/2007/03/28/ie7-in-vista-configuring-your-view-source-editor.aspx&quot; href=&quot;http://blogs.msdn.com/ie/archive/2007/03/28/ie7-in-vista-configuring-your-view-source-editor.aspx&quot;&gt;http://blogs.msdn.com/ie/archive/2007/03/28/ie7-in-vista-configuring-your-view-source-editor.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt; You can also download this mini-app to do it for you ( preferred choice, since I could not find the HKey described above on my Vista sys)&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a title=&quot;http://www.iconico.com/viewSourceEditor/&quot; href=&quot;http://www.iconico.com/viewSourceEditor/&quot;&gt;http://www.iconico.com/viewSourceEditor/&lt;/a&gt;&amp;#160; ( skip the registration!)&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Worked very well.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R956UMXzj-I/AAAAAAAAALk/5ZBCpHGaxw0/Capture%5B4%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;303&quot; alt=&quot;Capture&quot; src=&quot;http://lh4.google.com/elsharpo/R956UsXzj_I/AAAAAAAAALs/f6efC0Qc_1k/Capture_thumb%5B2%5D&quot; width=&quot;512&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  </description><link>http://elsharpo.blogspot.com/2008/03/how-to-change-ie7-default-page-source.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-3754464138701658023</guid><pubDate>Mon, 17 Mar 2008 13:24:00 +0000</pubDate><atom:updated>2008-03-18T00:24:12.542+11:00</atom:updated><title>WCF Services - Part 2</title><description>&lt;p&gt;In the last post I tried to show how to use HTTP GET to get data back from my Services. Now I want to put my thoughts in order and show how I created new Employees and updated them using programmatic POST using the ubiquitous Northwind database.&lt;/p&gt; &lt;p&gt;My first attempts where at trying to update a record using values embedded in the querystring... easier said than done, it was quite an effort.. At then end it worked, it just required a shift in the way I was tackling the problem. I ended up having my contract accept a Stream parameter and then had to read the stream and get the values back.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;My contract was:&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: green&quot;&gt;//STREAM EXAMPLE
&lt;/span&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
[&lt;span style=&quot;color: #2b91af&quot;&gt;WebInvoke&lt;/span&gt;(UriTemplate = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;*&quot;&lt;/span&gt;, Method = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;POST&quot;&lt;/span&gt;)]
&lt;span style=&quot;color: blue&quot;&gt;void &lt;/span&gt;UpdateEmployeeName(&lt;span style=&quot;color: #2b91af&quot;&gt;Stream &lt;/span&gt;id);&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;My uri template was &quot;*&quot; since I wanted to have the querystring collection of values straight after the &#39;root&#39; of my Service.svc&lt;/p&gt;
&lt;p&gt;My implementation was very simple...&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;UpdateEmployeeName(&lt;span style=&quot;color: #2b91af&quot;&gt;Stream &lt;/span&gt;id)
 {
     &lt;span style=&quot;color: #2b91af&quot;&gt;StreamReader &lt;/span&gt;reader = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;StreamReader&lt;/span&gt;(id);
     &lt;span style=&quot;color: #2b91af&quot;&gt;String &lt;/span&gt;res = reader.ReadToEnd();
     &lt;span style=&quot;color: #2b91af&quot;&gt;NameValueCollection &lt;/span&gt;coll = &lt;span style=&quot;color: #2b91af&quot;&gt;HttpUtility&lt;/span&gt;.ParseQueryString(res);
     
     &lt;span style=&quot;color: blue&quot;&gt;int &lt;/span&gt;employeeID = &lt;span style=&quot;color: #2b91af&quot;&gt;Int32&lt;/span&gt;.Parse(coll[&lt;span style=&quot;color: #a31515&quot;&gt;&quot;elid&quot;&lt;/span&gt;]);
     &lt;span style=&quot;color: #2b91af&quot;&gt;String &lt;/span&gt;newName = coll[&lt;span style=&quot;color: #a31515&quot;&gt;&quot;newName&quot;&lt;/span&gt;];
&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;     // Bless LinqToSql
     &lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext &lt;/span&gt;cts = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;(ConnectionManager.GetHomeLocalConnectionString);
     &lt;span style=&quot;color: #2b91af&quot;&gt;Employee &lt;/span&gt;e = cts.Employees.Where(a =&amp;gt; a.EmployeeID == employeeID ).Single();
     e.FirstName = newName;
     cts.SubmitChanges();
 }&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;Now, how the &lt;strong&gt;heck&lt;/strong&gt; do I make a programmatic POST to my contract?&lt;/p&gt;
&lt;p&gt;I just created a WebRequest and set the content type to form-urlencoded and set the request length to the length of my queryString collection.&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: green&quot;&gt;//set the data
&lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ASCIIEncoding &lt;/span&gt;enc = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ASCIIEncoding&lt;/span&gt;();
&lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;datatext = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;elid=&quot; &lt;/span&gt;+ elEmployeeID.Text;
datatext += &lt;span style=&quot;color: #a31515&quot;&gt;&quot;&amp;amp;newName=&quot; &lt;/span&gt;+ elEmployeeNewName.Text;

&lt;span style=&quot;color: blue&quot;&gt;byte&lt;/span&gt;[] data = enc.GetBytes(datatext);

&lt;span style=&quot;color: green&quot;&gt;//HTTP POST query
&lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;WebRequest &lt;/span&gt;request = &lt;span style=&quot;color: #2b91af&quot;&gt;HttpWebRequest&lt;/span&gt;.Create(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;http://localhost/DemoWCF/Service.svc&quot;&lt;/span&gt;);
request.Method = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;POST&quot;&lt;/span&gt;;
request.ContentType = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;application/x-www-form-urlencoded&quot;&lt;/span&gt;;
request.ContentLength = data.Length;

&lt;span style=&quot;color: #2b91af&quot;&gt;Stream &lt;/span&gt;datasteam = request.GetRequestStream();
datasteam.Write(data, 0, data.Length);
datasteam.Close();
&lt;span style=&quot;color: #2b91af&quot;&gt;WebResponse &lt;/span&gt;response = request.GetResponse();&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;To test it I threw a few textfields together and magic....&lt;/p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R95w7sXzj4I/AAAAAAAAAK0/mQ0lnwLYR5Q/part2_1%5B7%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;170&quot; alt=&quot;part2_1&quot; src=&quot;http://lh3.google.com/elsharpo/R95w8cXzj5I/AAAAAAAAAK8/-C2HcAsDUe4/part2_1_thumb%5B3%5D&quot; width=&quot;644&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;And checking the db....&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R95w88Xzj6I/AAAAAAAAALE/cp3B__pQysU/part2_2%5B3%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;78&quot; alt=&quot;part2_2&quot; src=&quot;http://lh4.google.com/elsharpo/R95w9sXzj7I/AAAAAAAAALM/U3vuJsQZ2Vo/part2_2_thumb%5B1%5D&quot; width=&quot;644&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I also wanted to create a new Employee using Javascript using a programmatic post as above but this time using Javascript Object Notation (JSON ) &lt;a title=&quot;http://www.json.org/&quot; href=&quot;http://www.json.org/&quot;&gt;http://www.json.org/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;My contract..&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
[&lt;span style=&quot;color: #2b91af&quot;&gt;WebInvoke&lt;/span&gt;(BodyStyle = &lt;span style=&quot;color: #2b91af&quot;&gt;WebMessageBodyStyle&lt;/span&gt;.Wrapped,
RequestFormat = &lt;span style=&quot;color: #2b91af&quot;&gt;WebMessageFormat&lt;/span&gt;.Json,
ResponseFormat = &lt;span style=&quot;color: #2b91af&quot;&gt;WebMessageFormat&lt;/span&gt;.Json,
UriTemplate = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;/CreateEmployee&quot;&lt;/span&gt;)]
&lt;span style=&quot;color: blue&quot;&gt;void &lt;/span&gt;CreateEmployee(&lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;FirstName, &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;LastName);&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;Implementation:&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;CreateEmployee(&lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;FirstName, &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;LastName)
{
&lt;span style=&quot;color: #2b91af&quot;&gt;      NorthwindDataContext &lt;/span&gt;cts = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();
      &lt;span style=&quot;color: #2b91af&quot;&gt;Employee &lt;/span&gt;e = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Employee&lt;/span&gt;();      
      e.LastName = LastName;
      e.FirstName = FirstName;
      cts.Employees.InsertOnSubmit(e);
      cts.SubmitChanges();           
}&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To generate an HTTP POST call with JavaScript, I created a XMLHTTPRequest and then I just constructed the url, set the header content type to json and was careful to format my JSON pair&#39;s payload..&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;asp&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Button  &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;btnJSONCreateEmployee&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;runat&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;server&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;OnClientClick&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;doEmployeeCreate()&quot;&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;function &lt;/span&gt;doEmployeeCreate()
{            
&lt;span style=&quot;color: blue&quot;&gt;       var &lt;/span&gt;newFirstName = document.getElementById(&lt;span style=&quot;color: #a31515&quot;&gt;&#39;TextBox1&#39;&lt;/span&gt;).value;            
       &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;newLastName = document.getElementById(&lt;span style=&quot;color: #a31515&quot;&gt;&#39;TextBox2&#39;&lt;/span&gt;).value;
            
       &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;xmlHttp = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;XMLHttpRequest();            
          
       &lt;span style=&quot;color: green&quot;&gt;// Create result handler 
       &lt;/span&gt;xmlHttp.onreadystatechange=&lt;span style=&quot;color: blue&quot;&gt;function&lt;/span&gt;()
       {
          &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt;(xmlHttp.readyState == 4)
          {
             document.getElementById(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;result&quot;&lt;/span&gt;).value = xmlHttp.responseText;
           }
       }
            
       &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;url = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;Service.svc/CreateEmployee&quot;&lt;/span&gt;;
       &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;body = &lt;span style=&quot;color: #a31515&quot;&gt;&#39;{&quot;FirstName&quot;: &#39;&lt;/span&gt;+ &lt;span style=&quot;color: #a31515&quot;&gt;&#39;&quot;&#39; &lt;/span&gt;+ newFirstName + &lt;span style=&quot;color: #a31515&quot;&gt;&#39;&quot;&#39;&lt;/span&gt;;
       body += &lt;span style=&quot;color: #a31515&quot;&gt;&#39;, &quot;LastName&quot;: &#39; &lt;/span&gt;+ &lt;span style=&quot;color: #a31515&quot;&gt;&#39;&quot;&#39;&lt;/span&gt;+newLastName + &lt;span style=&quot;color: #a31515&quot;&gt;&#39;&quot;&#39; &lt;/span&gt;+ &lt;span style=&quot;color: #a31515&quot;&gt;&#39;}&#39;&lt;/span&gt;;
            
       &lt;span style=&quot;color: green&quot;&gt;//Send the Http Request
       &lt;/span&gt;xmlHttp.open(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;POST&quot;&lt;/span&gt;, url, &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;);
       xmlHttp.setRequestHeader(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;Content-type&quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&quot;application/json&quot;&lt;/span&gt;);
       xmlHttp.send(body); 
}&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To create a JSON payload in C# is very simple, just format a string carefully, escaping the &quot; and you&#39;ll be fine..&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;protected void &lt;/span&gt;btnCallService_Clicked(&lt;span style=&quot;color: blue&quot;&gt;object &lt;/span&gt;sender, &lt;span style=&quot;color: #2b91af&quot;&gt;EventArgs &lt;/span&gt;args)
{
   &lt;span style=&quot;color: #2b91af&quot;&gt;ASCIIEncoding &lt;/span&gt;enc = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;ASCIIEncoding&lt;/span&gt;();
   &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;employeeID = txtJsonEmpID.Text;
   &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;firstName = txtJsonFirstName.Text;
   &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;lastName = txtJsonLastName.Text;           
   &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;datatext = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;{\&quot;EmployeeID\&quot;:{0}, \&quot;FirstName\&quot;:\&quot;{1}\&quot;  , \&quot;LastName\&quot;:\&quot;{2}\&quot; }&quot;&lt;/span&gt;;
        
   datatext = datatext.Replace(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;{0}&quot;&lt;/span&gt;, employeeID)
                      .Replace(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;{1}&quot;&lt;/span&gt;,firstName)
                      .Replace(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;{2}&quot;&lt;/span&gt;, lastName);
               
   &lt;span style=&quot;color: blue&quot;&gt;byte&lt;/span&gt;[] data = enc.GetBytes(datatext);

   W&lt;span style=&quot;color: #2b91af&quot;&gt;ebRequest &lt;/span&gt;request = &lt;span style=&quot;color: #2b91af&quot;&gt;HttpWebRequest&lt;/span&gt;.Create(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;http://localhost/DemoWCF/Service.svc/CallJason&quot;&lt;/span&gt;);
   request.Method = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;POST&quot;&lt;/span&gt;;
   request.ContentType = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;application/json&quot;&lt;/span&gt;;
   request.ContentLength = data.Length;
   &lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;   &lt;span style=&quot;color: #2b91af&quot;&gt;Stream &lt;/span&gt;datasteam = request.GetRequestStream();
   datasteam.Write(data, 0, data.Length);
   datasteam.Close();
   &lt;span style=&quot;color: #2b91af&quot;&gt;WebResponse &lt;/span&gt;response = request.GetResponse();
}&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Ok, now unfortunately one of the new Employees didn&#39;t leave good comments in SVN and el# got a bit upset when he had a look at the logs. So let&#39;s fire the guy, tough choice yes, but then also we need to demo a programmatic HTTP DELETE.&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R95w-MXzj8I/AAAAAAAAALU/vJZXJiYhgA4/part2_3%5B4%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;128&quot; alt=&quot;part2_3&quot; src=&quot;http://lh4.google.com/elsharpo/R95w-sXzj9I/AAAAAAAAALc/RX1VIHGndTw/part2_3_thumb%5B2%5D&quot; width=&quot;428&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/pre&gt;
&lt;p&gt;Simple contract...I just specify the request format, the response format and the method that my client will be using..&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;[WebInvoke&lt;/span&gt;(BodyStyle = &lt;span style=&quot;color: #2b91af&quot;&gt;WebMessageBodyStyle&lt;/span&gt;.Wrapped,
    RequestFormat = &lt;span style=&quot;color: #2b91af&quot;&gt;WebMessageFormat&lt;/span&gt;.Json,
    ResponseFormat = &lt;span style=&quot;color: #2b91af&quot;&gt;WebMessageFormat&lt;/span&gt;.Json,
    UriTemplate = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;/FireEmployee&quot;&lt;/span&gt;,
    Method=&lt;span style=&quot;color: #a31515&quot;&gt;&quot;DELETE&quot; &lt;/span&gt;)]
&lt;span style=&quot;color: blue&quot;&gt;void &lt;/span&gt;FireEmployee(&lt;span style=&quot;color: blue&quot;&gt;int &lt;/span&gt;EmployeeID);&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;Implementation is dead simple...this has got nothing to do with WCF, but I just post it here for completeness.&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;FireEmployee(&lt;span style=&quot;color: blue&quot;&gt;int &lt;/span&gt;EmployeeID)
{
    &lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext &lt;/span&gt;ctx = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();
    &lt;span style=&quot;color: #2b91af&quot;&gt;Employee &lt;/span&gt;emp = ctx.Employees.Where(e =&amp;gt; e.EmployeeID == EmployeeID).Single();
    ctx.Employees.DeleteOnSubmit(emp);
    ctx.SubmitChanges();  
}  &lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;Calling using JavaScript is v. simple too..&lt;/p&gt;
&lt;div align=&quot;left&quot;&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;function &lt;/span&gt;fireTheGuy()
{
    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;employeeID  =  document.getElementById(&lt;span style=&quot;color: #a31515&quot;&gt;&#39;txtToDeleteEmployeeID&#39;&lt;/span&gt;).value;
    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;xmlHttp = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;XMLHttpRequest();            
          
    &lt;span style=&quot;color: green&quot;&gt;// Create result handler 
    &lt;/span&gt;xmlHttp.onreadystatechange=&lt;span style=&quot;color: blue&quot;&gt;function&lt;/span&gt;()
    {
        &lt;span style=&quot;color: blue&quot;&gt;if&lt;/span&gt;(xmlHttp.readyState == 4)
        {
          document.getElementById(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;result&quot;&lt;/span&gt;).value = xmlHttp.responseText;
        }
    }
            
    &lt;span style=&quot;color: green&quot;&gt;// void CreateEmployee(string FirstName, string LastName);
    &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;url = &lt;span style=&quot;color: #a31515&quot;&gt;&quot;Service.svc/FireEmployee&quot;&lt;/span&gt;;
    &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;body = &lt;span style=&quot;color: #a31515&quot;&gt;&#39; { &quot;EmployeeID&quot;: &#39; &lt;/span&gt;+  employeeID + &lt;span style=&quot;color: #a31515&quot;&gt;&#39; }&#39;&lt;/span&gt;;
            
    &lt;span style=&quot;color: green&quot;&gt;//Send the Http Request
    &lt;/span&gt;xmlHttp.open(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;DELETE&quot;&lt;/span&gt;, url, &lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;);
    xmlHttp.setRequestHeader(&lt;span style=&quot;color: #a31515&quot;&gt;&quot;Content-type&quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515&quot;&gt;&quot;application/json&quot;&lt;/span&gt;);
    xmlHttp.send(body);       
}&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;And trust me, it works like a charm.&lt;/p&gt;
&lt;p&gt;Next post I would like to return complex types back to the client, as in Employee&#39;s objects etc..&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;/div&gt;  </description><link>http://elsharpo.blogspot.com/2008/03/wcf-services-part-2.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-2594980437101924015</guid><pubDate>Tue, 11 Mar 2008 13:06:00 +0000</pubDate><atom:updated>2008-03-12T00:30:01.832+11:00</atom:updated><title>REST with WCF</title><description>&lt;p&gt;At the MS Heroes day, a few thursdays ago, our first session was on WCF and is fair to say a few of us were very impressed by what I saw and I just couldn&#39;t wait to recreate those presentations on my desk.. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What is WCF?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;From MSDN,&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Is a set of .net Technologies for building and running connected systems. It is a new breed of communications infrastructure built around the Web services architecture. Advanced Web services support in Windows Communication Foundation provides secure, reliable, and transacted messaging along with interoperability. The service-oriented programming model of Windows Communication Foundation is built on the Microsoft .NET Framework and simplifies development of connected systems&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What is REST ? &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;REST is a architectural model ( it is not a standard ) for distributed computing that defines how resources are distributed and addressed. &lt;/p&gt;  &lt;p&gt;The acronym means &lt;strong&gt;Representational State Transfer&lt;/strong&gt;, translated to mere mortals language it means that each unique URL is a representation of some data. It was term invented by Roy Fielding one of the principal authors of the HTTP protocol.&lt;/p&gt;  &lt;p&gt;I will post resources and links later in the post. However Google for REST and you&#39;ll get heaps of results if you are curious. Definitions are boring! I like doing things. But you need to know a bit of the theory.&lt;/p&gt;  &lt;p&gt;eg:&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://localhost/myWebService/Employee/2&quot;&gt;http://localhost/myWebService/Employee/2&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://localhost/myWebService/Employee&quot;&gt;http://localhost/myWebService/Employee&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://localhost/myWebService/Product/89&quot;&gt;http://localhost/myWebService/Product/89&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;REST WebServices: An alternative to SOAP&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;All the above urls will return you different data ( a &lt;strong&gt;representation&lt;/strong&gt; of the resource requested ) according to (of course) on how you map this URL to your contracts.&amp;#160; First link might be mapped to return you the Employee with ID =2 and so on, or the Product with ID = 89&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Basic principles&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Identify the entities you want to expose a representation eg Employee, Product &lt;/li&gt;    &lt;li&gt;Create URL for each resource/entity. Resources should be nouns. &lt;/li&gt;    &lt;li&gt;Categorise resources according to whether clients can just get a representation of your data or whether they can modify the data. For resources that you just want to make available to clients, make them accessible using HTTP GET, for resources that you want to give the client the possibility to modify data, make them accessible using HTTP POST, PUT or DELETE &lt;/li&gt;    &lt;li&gt;Resources accessible via GET should be &#39;side effect&#39; free. This means the web service call should not modify the data in the back end. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Yahoo and &lt;a href=&quot;http://www.flickr.com/services/api/request.rest.html&quot;&gt;Flickr&lt;/a&gt; amongst others use REST web services. Example of Flickr REST web service can be seen &lt;a href=&quot;http://www.flickr.com/services/api/response.rest.html&quot;&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Main advantages of using REST&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Easy to work with, and easy to read for &#39;humans&#39;. &lt;/li&gt;    &lt;li&gt;Easy to consume ( not always, I hear Amazon services can be a pain to parse ) &lt;/li&gt;    &lt;li&gt;Improved server response times. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I started working with this on the weekend after Microsoft Heroes in Sydney. At the time I challenged myself not to Google for answers and get the examples working only with the help of MSDN for WCF. &lt;/p&gt;  &lt;p&gt;It was not easy at first, MSDN is huge and it throws so much information at you, if you are not careful you start losing track of what link you open where and soon you lose track of what you are reading, at least I do!&lt;/p&gt;  &lt;p&gt;Needless to say it wasn&#39;t that easy. I find it interesting to see how much developers including myself sometimes google problems searching for tailor-made answers. Imagine a world without Google? Is mindblowing to think how older programmers looked for answers 20 years ago, maybe things where simpler those days. Hrmm..&lt;/p&gt;  &lt;p&gt;Ok, so lets get started, first of all I went NewSite -&amp;gt; WCF Service. Visual Studio will create a simple solution with an IService and a Service class and also a Service.svc file.&lt;/p&gt;  &lt;p&gt;First of all lets look at the IService interface is provided by default.&lt;/p&gt;  &lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;ServiceContract&lt;/span&gt;]
&lt;span style=&quot;color: blue&quot;&gt;public interface &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;IService
&lt;/span&gt;{

    [&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
    &lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;GetData(&lt;span style=&quot;color: blue&quot;&gt;int &lt;/span&gt;value);

    [&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
    &lt;span style=&quot;color: #2b91af&quot;&gt;CompositeType &lt;/span&gt;GetDataUsingDataContract(C&lt;span style=&quot;color: #2b91af&quot;&gt;ompositeType &lt;/span&gt;composite);

    &lt;span style=&quot;color: green&quot;&gt;// TODO: Add your service operations here
&lt;/span&gt;}&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;FIrst thing you do is define a contract. You do this by adding the [&lt;span style=&quot;color: #2b91af&quot;&gt;ServiceContract&lt;/span&gt;]&amp;#160; attribute to your interface.&amp;#160; This contract will define what operations your webservice will ultimately support. Think of an operation as a method call that your service will expose. Each method in the interface that you want to expose, must then have the [&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]&amp;#160; attribute defined / applied to it. If a method does not have this attribute, it won&#39;t be exposed therefore it wont be accessible by the service&#39;s clients.&lt;/p&gt;

&lt;p&gt;We then have to implement this interface and implement each method that we defined in our ServiceContract. &lt;/p&gt;

&lt;p&gt;I want to expose a method by a REST WebService to return an Employee Name representation when the URL is &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;GetEmployeeName/{id}&amp;quot;.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: #a31515&quot;&gt;&lt;/span&gt;Remember that if we just want a representation of our data without any side effect by the REST principles we have to use the HTTP GET verb. WCF Web model allows you to do this by using the WebGet method attribute for Get operations. All you have to do is adorn your method with WebGet.&lt;/p&gt;

&lt;p&gt;For that to happen I have to define&amp;#160; my operation contract in the interface as follows:&lt;/p&gt;

&lt;p&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;] 

  &lt;br /&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;WebGet&lt;/span&gt;(UriTemplate= &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;GetEmployeeName/{id}&amp;quot;&lt;/span&gt;, ResponseFormat = &lt;span style=&quot;color: #2b91af&quot;&gt;WebMessageFormat&lt;/span&gt;.Xml)] 

  &lt;br /&gt;&lt;span style=&quot;color: blue&quot;&gt;string&lt;/span&gt;GetEmployeeName(&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;id); 

  &lt;br /&gt;&lt;/p&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;In the WebGet attribute we can also describe the layout of the URL that our WebService will respond to. For example I want the method above to be called upon to answer the GetEmployeeFirstName call when the URL is MyService.svc/GetEmployeeName/2&lt;/p&gt;

&lt;p&gt;We do this by adding a &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/bb675245.aspx&quot; target=&quot;_blank&quot;&gt;UriTemplate&lt;/a&gt; property to our WebGet attribute. This templates allow you to match an incoming URL to an operation in the service. The ResponseFormat attribute is self explanatory, you can choose XML or JSON.&lt;/p&gt;

&lt;p&gt;Now we have to implement our GetEmployeeeFirstName method in our class.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Service &lt;/span&gt;: &lt;span style=&quot;color: #2b91af&quot;&gt;IService
&lt;/span&gt;{

    &lt;span style=&quot;color: blue&quot;&gt;public string &lt;/span&gt;GetEmployeeName(&lt;span style=&quot;color: blue&quot;&gt;string &lt;/span&gt;id)
    {
        &lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext &lt;/span&gt;ct = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();
        &lt;span style=&quot;color: blue&quot;&gt;return &lt;/span&gt;ct.Employees.Where(a =&amp;gt; a.EmployeeID == &lt;span style=&quot;color: #2b91af&quot;&gt;Int32&lt;/span&gt;.Parse(id)).Select(a =&amp;gt; a.FirstName + &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot; &amp;quot; &lt;/span&gt;+ a.LastName).Single();
    }
}&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;I&#39;m using LinqToSql again to retrieve employees from the Northwind database.&lt;/p&gt;

&lt;p&gt;We are almost ready to go now. I had my biggest struggle with the web.config and configuring behaviours and endpoints. I wasn&#39;t the only one that struggled, for what I can gather around blogs etc and the poor MSDN documentation this wasn&#39;t that straightforward. &lt;/p&gt;

&lt;p&gt;I settled for the config settings below...&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;system.serviceModel&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;bindings&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;webHttpBinding&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;binding &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;ServiceBehavior&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;binding&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;                
            &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;webHttpBinding&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;        
        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;bindings&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;services&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;service &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;Service&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;behaviorConfiguration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;ServiceBehavior&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
                &amp;lt;!-- &lt;/span&gt;&lt;span style=&quot;color: green&quot;&gt;Service Endpoints &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;--&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;endpoint &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;address&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&amp;quot;  &lt;span style=&quot;color: red&quot;&gt;binding&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;webHttpBinding&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;behaviorConfiguration&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;ServiceBehavior&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;contract&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;IService&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;endpoint&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;endpoint &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;contract&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;IService&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;address&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;mex&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: red&quot;&gt;binding&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;mexHttpBinding&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;endpoint&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;                
            &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;service&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;            
        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;services&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;behaviors&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;endpointBehaviors&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;behavior  &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;ServiceBehavior&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
                    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;webHttp &lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;                     
                &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;behavior&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;                
            &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;endpointBehaviors&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;serviceBehaviors&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;behavior &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;ServiceBehavior&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
                     &lt;/span&gt;To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment 
                    &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;serviceMetadata &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;httpGetEnabled&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;&amp;quot; &lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;
                                     &lt;/span&gt;To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information 
                    &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;serviceDebug &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;includeExceptionDetailInFaults&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;true&lt;/span&gt;&amp;quot;&lt;span style=&quot;color: blue&quot;&gt;/&amp;gt;
                &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;behavior&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;  
            &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;serviceBehaviors&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
       &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;behaviors&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;system.serviceModel&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;Since I&#39;m using the WCF web model and all my requests are through HTTP and not SOAP I have to use &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.servicemodel.webhttpbinding.aspx&quot; target=&quot;_blank&quot;&gt;webHttpBinding&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;From MSDN..&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The WCF Web Programming Model allows developers to expose WCF Web services through HTTP requests that use &amp;quot;plain old XML&amp;quot; (POX) style messaging instead of SOAP-based messaging. For clients to communicate with a service using HTTP requests, an endpoint of the service must be configured with the WebHttpBinding that has the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/bb536047.aspx&quot;&gt;WebHttpBehavior&lt;/a&gt; attached to it. The WCF Web Programming Model also requires that the individual service operations are annotated with the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/bb536021.aspx&quot;&gt;WebGetAttribute&lt;/a&gt; or &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/bb515593.aspx&quot;&gt;WebInvokeAttribute&lt;/a&gt; attributes. This defines a mapping from a URI and HTTP method to the service operation, as well as the format of the messages used to call the operation and return the results. Support in WCF for syndication and ASP.AJAX integration are both built on top of the WCF Web Programming Model.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;SO far so good, Now if we run our WCF app and we invoke the URL with GetEmployeeName/2 the employee name should be returned...&lt;/p&gt;

&lt;p&gt;&lt;a title=&quot;http://localhost/CrapService/Service.svc/GetEmployeeName/2&quot; href=&quot;http://localhost/DemoWCF/Service.svc/GetEmployeeName/2&quot;&gt;http://localhost/DemoWCF/Service.svc/GetEmployeeName/2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9aDm8XzjtI/AAAAAAAAAJc/X4bNvheNvS8/Capture%5B6%5D&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;182&quot; alt=&quot;Capture&quot; src=&quot;http://lh4.google.com/elsharpo/R9aDnsXzjuI/AAAAAAAAAJk/ItdblOF16u4/Capture_thumb%5B4%5D&quot; width=&quot;814&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://localhost/DemoWCF/Service.svc/GetEmployeeName/3&quot;&gt;http://localhost/DemoWCF/Service.svc/GetEmployeeName/3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R9aDoMXzjvI/AAAAAAAAAJs/uOzbtr9SWus/Capture2%5B7%5D&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;183&quot; alt=&quot;Capture2&quot; src=&quot;http://lh5.google.com/elsharpo/R9aDo8XzjwI/AAAAAAAAAJ0/dbF4a1ahNzM/Capture2_thumb%5B5%5D&quot; width=&quot;811&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;font size=&quot;1&quot;&gt;(BTW Vista &lt;a href=&quot;http://www.vista4beginners.com/Snipping-Tool&quot; target=&quot;_blank&quot;&gt;Snipping Tool&lt;/a&gt; kicks bum big time!!!) ( David, I got to talk you into Vista for blogging Windows Live Writer Full, is awesome)&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Lets say I know want to get all the Employees Names by going to an url like this&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://localhost/DemoWCF/Service.svc/Employees&quot;&gt;http://localhost/DemoWCF/Service.svc/Employees&lt;/a&gt;&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
[&lt;span style=&quot;color: #2b91af&quot;&gt;WebGet&lt;/span&gt;(UriTemplate = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Employees&amp;quot;&lt;/span&gt;, ResponseFormat = &lt;span style=&quot;color: #2b91af&quot;&gt;WebMessageFormat&lt;/span&gt;.Xml)]
&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;&amp;gt; GetEmployeesNames();&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;My implementation of the operation would be something along the lines of..&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;&amp;gt; GetEmployeesNames()
{
&lt;span style=&quot;color: #2b91af&quot;&gt;    NorthwindDataContext &lt;/span&gt;ct = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();
    &lt;span style=&quot;color: #2b91af&quot;&gt;Enumerable&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;&amp;gt; ab = &lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;emps &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;ct.Employees
                            &lt;span style=&quot;color: blue&quot;&gt;select &lt;/span&gt;emps.FirstName + &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot; &amp;quot; &lt;/span&gt;+ emps.LastName;
    &lt;span style=&quot;color: blue&quot;&gt;return &lt;/span&gt;ab.ToList();
}&lt;/pre&gt;

&lt;p&gt;And the result would be..&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R9aDqMXzjxI/AAAAAAAAAJ8/TjOs4L2STRg/Capture3%5B6%5D&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;501&quot; alt=&quot;Capture3&quot; src=&quot;http://lh6.google.com/elsharpo/R9aDrMXzjyI/AAAAAAAAAKE/s1b5YkUnIRU/Capture3_thumb%5B4%5D&quot; width=&quot;556&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;Now if I wanted the Response to be of &lt;/p&gt;
&lt;span style=&quot;color: #2b91af&quot;&gt;WebMessageFormat&lt;/span&gt;.Json 

&lt;p&gt;I would then have to change my OperationContract to note my intention as follows &lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
[&lt;span style=&quot;color: #2b91af&quot;&gt;WebGet&lt;/span&gt;(UriTemplate = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;Employees&amp;quot;&lt;/span&gt;, ResponseFormat = &lt;span style=&quot;color: #2b91af&quot;&gt;WebMessageFormat&lt;/span&gt;.Json)]
&lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;&amp;gt; GetEmployeesNames();&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;p&gt;When I then go to the URI, you will then get prompted to save the response as a file...&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R9aDsMXzjzI/AAAAAAAAAKM/z3KK501j83Y/image%5B2%5D&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;151&quot; alt=&quot;image&quot; src=&quot;http://lh4.google.com/elsharpo/R9aDtsXzj0I/AAAAAAAAAKU/HW3xOAEjPrs/image_thumb&quot; width=&quot;244&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/pre&gt;

&lt;p&gt;that upon opening with trusty Notepad you can see the JSON string...&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R9aDvcXzj1I/AAAAAAAAAKc/h4Sgms-Gapk/Capture5%5B4%5D&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;74&quot; alt=&quot;Capture5&quot; src=&quot;http://lh6.google.com/elsharpo/R9aDwMXzj2I/AAAAAAAAAKk/Tl89d2WzlMo/Capture5_thumb%5B2%5D&quot; width=&quot;870&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/pre&gt;

&lt;p&gt;In this post I just wanted to show how I got my REST services to work and I then plan to post about how to use PUT, POST and DELETE , how to return complex types, DataContracts and also how to pass querystring values to your OperationContract as a Stream, that in fact was the first thing that I got working. ( Quick preview below). I would also like to post all the source code of my working examples. &lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
[&lt;span style=&quot;color: #2b91af&quot;&gt;WebInvoke&lt;/span&gt;(UriTemplate = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;*&amp;quot;&lt;/span&gt;, Method = &lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;)]
&lt;span style=&quot;color: blue&quot;&gt;void &lt;/span&gt;UpdateEmployeeName(&lt;span style=&quot;color: #2b91af&quot;&gt;Stream &lt;/span&gt;id);&lt;/pre&gt;

Implementation

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public void &lt;/span&gt;UpdateEmployeeName(&lt;span style=&quot;color: #2b91af&quot;&gt;Stream &lt;/span&gt;id)
{
&lt;span style=&quot;color: #2b91af&quot;&gt;   StreamReader &lt;/span&gt;reader = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;StreamReader&lt;/span&gt;(id);
   &lt;span style=&quot;color: #2b91af&quot;&gt;String &lt;/span&gt;res = reader.ReadToEnd();
   N&lt;span style=&quot;color: #2b91af&quot;&gt;ameValueCollection &lt;/span&gt;coll = &lt;span style=&quot;color: #2b91af&quot;&gt;HttpUtility&lt;/span&gt;.ParseQueryString(res);
   &lt;span style=&quot;color: blue&quot;&gt;int &lt;/span&gt;employeeID = &lt;span style=&quot;color: #2b91af&quot;&gt;Int32&lt;/span&gt;.Parse(coll[&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;elid&amp;quot;&lt;/span&gt;]);
   &lt;span style=&quot;color: #2b91af&quot;&gt;String &lt;/span&gt;newName = coll[&lt;span style=&quot;color: #a31515&quot;&gt;&amp;quot;newName&amp;quot;&lt;/span&gt;];
   &lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext &lt;/span&gt;cts = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();
   &lt;span style=&quot;color: #2b91af&quot;&gt;Employee &lt;/span&gt;e = cts.Employees.Where(a =&amp;gt; a.EmployeeID == employeeID ).Single();
   e.LastName = newName;
   cts.SubmitChanges();
}&lt;/pre&gt;
&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;

&lt;pre class=&quot;code&quot;&gt;See you then.........&lt;/pre&gt;


&lt;font face=&quot;Courier New&quot;&gt;&lt;/font&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;  </description><link>http://elsharpo.blogspot.com/2008/03/rest-with-wcf.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-199693791513323363</guid><pubDate>Mon, 10 Mar 2008 14:01:00 +0000</pubDate><atom:updated>2008-03-11T01:01:44.985+11:00</atom:updated><title>Playing around with Silverlight 2.0 Beta 1</title><description>&lt;p&gt;Since Silverlight Beta was released a few days, I wanted to have a look around and see what can you do with it.&lt;/p&gt; &lt;p&gt;After I did all the admins stuff like downloaded the beta, installed etc I started following ScottGu&#39;s tutorials on his blog to get an idea.&lt;/p&gt; &lt;p&gt;I thought, hell what I&#39;m going to do! I&#39;ve got no idea and certainly no skills as a designer, so I thought ok, lets simulate the AjaxAutoComplete extender and see where it takes us.&lt;/p&gt; &lt;p&gt;First of all I created a Silverlight application...&lt;/p&gt; &lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9U-48XzjNI/AAAAAAAAAFY/etUxmyQUe3E/image%5B12%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;392&quot; alt=&quot;image&quot; src=&quot;http://lh4.google.com/elsharpo/R9U-58XzjOI/AAAAAAAAAFg/ipsQqQ4RJzQ/image_thumb%5B6%5D&quot; width=&quot;474&quot; border=&quot;0&quot;&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Then Visual Studio told me that in order to run a SL app, I need to host in a web page.&lt;/p&gt; &lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R9U-6sXzjPI/AAAAAAAAAFo/9lw9XrIDiiw/image%5B14%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;392&quot; alt=&quot;image&quot; src=&quot;http://lh3.google.com/elsharpo/R9U-7sXzjQI/AAAAAAAAAFw/fTzzAymPfDQ/image_thumb%5B8%5D&quot; width=&quot;476&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;In project type above you can either choose, WebSite or WebAppliucation type..hrmm.&lt;/p&gt; &lt;p&gt;Upon clicking ok you are presented with an empty xaml page called Page.xaml&lt;/p&gt; &lt;p&gt;Now, the Silverlight control, because that is what we are creating, &quot;lives&quot; inside an html/aspx page. Note that the ctrl Source property is pointing to SuperApp.xap...&lt;/p&gt; &lt;p&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R9U-8cXzjRI/AAAAAAAAAF4/_T7T4LyjOfo/image%5B22%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;600&quot; alt=&quot;image&quot; src=&quot;http://lh4.google.com/elsharpo/R9U-98XzjSI/AAAAAAAAAGA/cC683SlKMrI/image_thumb%5B12%5D&quot; width=&quot;1028&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;&lt;strong&gt;What the heck is this .xap file? I hear you ask..&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Nothing too exciting, it is the compiled Silverlight application in zip format. This file contains the dlls it needs to run like System.Windows.Controls.dll and System.Windows.Controls.Extended.dll and the dll of your app in our case SuperApp.dll.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Dealing with the Xaml..&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;First of all I need to build the GUI for the TextBox and ultimately my dodgy AutoCompleteExtender.&lt;/p&gt; &lt;p&gt;What do we need?&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Need a TexBox&lt;/li&gt; &lt;li&gt;Need some sort of DropDownList that is displayed as the user types in..&lt;/li&gt; &lt;li&gt;Need a button to submit the selected name eventually.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;I choose &lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Canvas&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;gt;&lt;/span&gt; as my layout, there is Grid and StackPanel. I used Grid too and it looked very easy to use, it was like creating an html table and then referencing to the x,y cells..very cool.&lt;/p&gt; &lt;p&gt;This is the Xaml for my text box. Note the Canvas.Top and Canvas.Left, no prize for guessing that it references the parent container of my textbox and it tells it to position it 100 pixels from the left and 20 from the top&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;TextBox &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;TextBox_EmployeeName&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Canvas.Left&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;100&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Canvas.Top&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;20&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Width&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;200&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;BorderBrush&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;CornflowerBlue&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;BorderThickness&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;1&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Height&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;30&quot; /&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;
&lt;/span&gt;&lt;/p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;The button is very simple:&lt;/p&gt;
&lt;p&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;Button &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Click&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;Save_Click&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Content&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;Save&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Canvas.Left&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;310&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Canvas.Top&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;20&quot; x&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Name&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;Save&quot;&lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Width&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;60&quot; &amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;I also added an event listener for the Click event.&lt;/p&gt;
&lt;p&gt;Now for the ComboBox-look a like. There is no ComboBox in Silverlight Beta, so the closest thing is a ListBox.&lt;/p&gt;
&lt;p&gt;Lets aligned right under the bottom of the TextBox and see what happens. At this stage of the game I was very hopeful that this will be able to be done.&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;ListBox  &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;KeyDown&lt;/span&gt;&lt;/font&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;EmployeeNames_KeyDown&quot;  
        &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;MouseLeftButtonDown&lt;/span&gt;&lt;/font&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;EmployeeNames_MouseLeftButtonDown&quot;  
        &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Visibility&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;Collapsed&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Name&lt;/span&gt;&lt;/font&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;EmployeeNames&quot; 
        &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Width&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;200&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Canvas.Left&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;100&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Canvas.Top&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;46&quot; /&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style=&quot;color: blue&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;Ok, so far so good, note I added two events to my ListBox, KeyDown and MouseLeftButtonDown. Basically these two events will &#39;listen&#39; for when the user presses a key while this control has focus and also while the Mouse left button is pressed also while focus is on. ( I was thinking hrmm, this reminds me of SWING development back at uni in Java..)&lt;/p&gt;
&lt;p&gt;Resulting page is: ( beautiful )&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R9U--cXzjTI/AAAAAAAAAGM/gtFzVALcsYo/basic_Silver_Gui%5B6%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;126&quot; alt=&quot;basic_Silver_Gui&quot; src=&quot;http://lh4.google.com/elsharpo/R9U--8XzjUI/AAAAAAAAAGU/dFn-KIlPMh0/basic_Silver_Gui_thumb%5B4%5D&quot; width=&quot;257&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Next question I had was. Ok I have to get some data from my Northwind database. I want text to resolve back to an EmployeeName. How am I going to do this? WebServices with WCF..&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This WebService &lt;em&gt;has&lt;/em&gt; to be configured as BasicHttpBinding.&lt;/p&gt;
&lt;p&gt;You cannot add a WebService to the Silverlight application, you have to do it somewhere else. In this case I added it you my WebSite. AddNewItem --&amp;gt; WCF Service.&lt;/p&gt;
&lt;p&gt;I defined my [&lt;span style=&quot;color: #2b91af&quot;&gt;ServiceContract&lt;/span&gt;] Interface and my &lt;span style=&quot;color: #2b91af&quot;&gt;OperationContracts&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;and then my DataContract, because I wanted to return a MyEmployee object from my WCF webService.&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;[&lt;span style=&quot;color: #2b91af&quot;&gt;ServiceContract&lt;/span&gt;]
&lt;span style=&quot;color: blue&quot;&gt;public interface &lt;/span&gt;&lt;/font&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;IMyService
&lt;/span&gt;{
    [&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
    &lt;span style=&quot;color: blue&quot;&gt;int &lt;/span&gt;CountEmployees();&lt;/font&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;/font&gt;&lt;font size=&quot;1&quot;&gt;
    [&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
    &lt;span style=&quot;color: #2b91af&quot;&gt;MyEmployee &lt;/span&gt;GetEmployee(&lt;span style=&quot;color: blue&quot;&gt;int &lt;/span&gt;id);

    [&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
    &lt;span style=&quot;color: #2b91af&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;MyEmployee&lt;/span&gt;&amp;gt; GetAllEmployees();

    [&lt;span style=&quot;color: #2b91af&quot;&gt;OperationContract&lt;/span&gt;]
    &lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;[] GetAjaxyEmployeeNames(&lt;span style=&quot;color: #2b91af&quot;&gt;String &lt;/span&gt;prefixText);
}

[&lt;span style=&quot;color: #2b91af&quot;&gt;DataContract&lt;/span&gt;]
&lt;span style=&quot;color: blue&quot;&gt;public class &lt;/span&gt;&lt;/font&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;MyEmployee
&lt;/span&gt;{   
    [&lt;span style=&quot;color: #2b91af&quot;&gt;DataMember&lt;/span&gt;]
    &lt;span style=&quot;color: blue&quot;&gt;public string &lt;/span&gt;Name { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }

    [&lt;span style=&quot;color: #2b91af&quot;&gt;DataMember&lt;/span&gt;]
    &lt;span style=&quot;color: blue&quot;&gt;public int &lt;/span&gt;GetEmployeeID { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;;}

    [&lt;span style=&quot;color: #2b91af&quot;&gt;DataMember&lt;/span&gt;]
    &lt;span style=&quot;color: blue&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;String &lt;/span&gt;Address { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }

    [&lt;span style=&quot;color: #2b91af&quot;&gt;DataMember&lt;/span&gt;]
    &lt;span style=&quot;color: blue&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;String &lt;/span&gt;Title { &lt;span style=&quot;color: blue&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue&quot;&gt;set&lt;/span&gt;; }  
}&lt;/font&gt;&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;Then I had my MyService class that implemented my IService interface.&lt;/p&gt;
&lt;p&gt;The function of interest is the GetAjaxyEmployeeNames, since it returns a String[] with the names that start with the prefixText passed on. Also note that you cannot add LinqToSql to Silverlight 2.0 Beta app, it is not supported at this stage, it does support LinqToXml though and one of ScottGu&#39;s tuts shows this. Since I added the Service to the WebSite, I just added a dbml file and drag-and-dropped the Employee table. Piece of cake.&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;public string&lt;/span&gt;[] GetAjaxyEmployeeNames(&lt;span style=&quot;color: #2b91af&quot;&gt;String &lt;/span&gt;prefixText)
{
       &lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext &lt;/span&gt;ctx = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;NorthwindDataContext&lt;/span&gt;();
       &lt;span style=&quot;color: blue&quot;&gt;var &lt;/span&gt;emps = &lt;span style=&quot;color: blue&quot;&gt;from &lt;/span&gt;e &lt;span style=&quot;color: blue&quot;&gt;in &lt;/span&gt;ctx.Employees
                  &lt;span style=&quot;color: blue&quot;&gt;where &lt;/span&gt;e.LastName.StartsWith(prefixText)
                  &lt;span style=&quot;color: blue&quot;&gt;orderby &lt;/span&gt;e.LastName &lt;/font&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;descending
                  select &lt;/span&gt;e.LastName + &lt;span style=&quot;color: #a31515&quot;&gt;&quot;, &quot; &lt;/span&gt;+ e.FirstName;
       &lt;span style=&quot;color: blue&quot;&gt;return &lt;/span&gt;emps.ToArray&amp;lt;&lt;span style=&quot;color: #2b91af&quot;&gt;String&lt;/span&gt;&amp;gt;();        
}&lt;/font&gt;&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;Now we have the webService sorted out. We need to add a reference to our Silverlight app about this. The way to do it is very easy. Just right click on the project file Add Service Reference and you will be shown the dialog below..then discover and hopefully it will find the web service we just created.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9U_AMXzjVI/AAAAAAAAAGc/2D1Pdy6Q6XE/image%5B26%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;484&quot; alt=&quot;image&quot; src=&quot;http://lh6.google.com/elsharpo/R9U_BcXzjWI/AAAAAAAAAGk/6tyz6huOE9c/image_thumb%5B14%5D&quot; width=&quot;519&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Back to the Xaml now.&lt;/p&gt;
&lt;p&gt;We have to add an Even listener to our TextBox, so when the text changes in the txtBox, the webservice will be called. This is easy, we just add a TextChanged event handler.&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515&quot;&gt;TextBox &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;TextChanged&lt;/span&gt;&lt;/font&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;TextBox_TextChanged&quot;
        &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Canvas.Left&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;100&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Canvas.Top&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;20&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Width&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;200&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Text&lt;/span&gt;&lt;/font&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;&quot;
        &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Name&lt;/span&gt;&lt;/font&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;TextBox_EmployeeName&quot;
        &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;BorderBrush&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;CornflowerBlue&quot; &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;BorderThickness&lt;/span&gt;&lt;/font&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;1&quot;
        &lt;/span&gt;&lt;span style=&quot;color: red&quot;&gt;Height&lt;/span&gt;&lt;span style=&quot;color: blue&quot;&gt;=&quot;30&quot;&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;I love the way VS does this for xaml pages, as soon as you type KeyDown the intellisense will ask you if you want to create a new listener, and if you press enter, VS will create a function in the codefile with some real cool naming by default.. wish we had this for normal asp.net! ( ScottGu said this is coming soon!)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font size=&quot;1&quot;&gt;I also added a KeyDown handler, so when the textbox has focus and the user presses the down arrow key, the behaviour of the real AutoCompleteExtender is simulated and the focus and control moves on to the ListBox.&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9U_B8XzjXI/AAAAAAAAAGs/q2hOFN3wcvM/event_intellegince%5B6%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;167&quot; alt=&quot;event_intellegince&quot; src=&quot;http://lh6.google.com/elsharpo/R9U_CcXzjYI/AAAAAAAAAG0/WCOnROMYK9w/event_intellegince_thumb%5B4%5D&quot; width=&quot;363&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;We have the handlers now, next we have to create a proxy to our WebService so they methods it exposes can be called.&amp;nbsp; &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Add two using statements to System.ServiceModel and System.ServiceModel.Channels.&lt;/li&gt;
&lt;li&gt;Create a binding element in the scope of the class ( so we can use it everywhere!). &lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;Binding &lt;/span&gt;binding = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;BasicHttpBinding&lt;/span&gt;(); &lt;/font&gt;&lt;font size=&quot;2&quot;&gt;Bindings specify the details for a client to communicate with the Service. The only binding supported at this stage is BasicHttpBinding.&lt;/font&gt;&lt;/li&gt;
&lt;li&gt;Next we have to create an EndPointAddress. This gives the address of the Service to be called upon. So we just use the address where this service is being hosted. &lt;/li&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;EndpointAddress &lt;/span&gt;address = &lt;span style=&quot;color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af&quot;&gt;EndpointAddress&lt;/span&gt;(&quot;&lt;span style=&quot;color: #a31515&quot;&gt;&lt;a href=&quot;http://localhost:50740/SilverlightApplicationFirstEver_Web/Services/MyService.svc&quot;&gt;&lt;font color=&quot;#ff0000&quot;&gt;http://localhost:50740/SilverlightApplicationFirstEver_Web/Services/MyService.svc&lt;/font&gt;&lt;/a&gt;&quot;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;/font&gt;&amp;nbsp;&lt;/pre&gt;&lt;/ol&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;We now have everything we need to create our proxy and call the Service. Remember we have a listener for when the user enters text in our TextBox and so the TextChanged event fires.&lt;/p&gt;
&lt;p&gt;To create the proxy to our service, the proxy&#39;s constructor takes in the binding and endpointAddress.&lt;/p&gt;
&lt;p&gt;To now call the methods in our service we have to realise that all calls in Silverlight are asynchronous. In this case I&#39;m using the event driven pattern where the proxy contains two members for each potential call to the Service, these are an async method and a completed event that is raised once the async call returns. This results on the following members: &lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;/font&gt;&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;pre class=&quot;code&quot;&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9U_C8XzjZI/AAAAAAAAAG8/SRHRj1Je5uY/resultingmethods%5B5%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;44&quot; alt=&quot;resultingmethods&quot; src=&quot;http://lh6.google.com/elsharpo/R9U_DcXzjaI/AAAAAAAAAHE/rxNMFbmaojQ/resultingmethods_thumb%5B3%5D&quot; width=&quot;730&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/pre&gt;
&lt;p&gt;The complete function looks like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9U_D8XzjbI/AAAAAAAAAHM/Kvn_tEbmcD8/completed_function%5B4%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;147&quot; alt=&quot;completed_function&quot; src=&quot;http://lh3.google.com/elsharpo/R9U_EsXzjcI/AAAAAAAAAHU/hrYgUxDfqlE/completed_function_thumb%5B2%5D&quot; width=&quot;706&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;And the proxy_AjaxyEmployeeCompleted function that handles the returned data from the WebService is:&lt;/p&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9U_FMXzjdI/AAAAAAAAAHc/BP1oOGbkDGo/proxy_completed_call%5B5%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;292&quot; alt=&quot;proxy_completed_call&quot; src=&quot;http://lh3.google.com/elsharpo/R9U_FsXzjeI/AAAAAAAAAHk/yx4s7jYHYBc/proxy_completed_call_thumb%5B3%5D&quot; width=&quot;630&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/font&gt;&lt;/pre&gt;
&lt;p&gt;&lt;font size=&quot;1&quot;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Note that the ListBox is hidden unless there is a result to display.&lt;/p&gt;
&lt;p&gt;Now as soon as you type something in, the service will be displayed and the ListBox ItemsSource property ( think of it as a DataSource in Asp.net ) is bound to args.Result, which is a String[]&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9U_GMXzjfI/AAAAAAAAAHs/2_IVkFMw9jQ/extenderinaction%5B3%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;251&quot; alt=&quot;extenderinaction&quot; src=&quot;http://lh3.google.com/elsharpo/R9U_GsXzjgI/AAAAAAAAAH0/Fl9BMXt9hqo/extenderinaction_thumb%5B1%5D&quot; width=&quot;448&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;/font&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;Thats basically it, I have more functions to simulate the real AutoCompleteExtender, like for example when the user clicks with the mouse on say &quot;King, Robert&quot; that Employee name will be selected and will be copied into the TextBox. &lt;/p&gt;
&lt;p&gt;Also I have a listener on the ListBox that listens for any down arrow key pressed and mouse left button events that copy the SelectedItem text to the TextBox again.&lt;font color=&quot;#0000ff&quot; size=&quot;1&quot;&gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color=&quot;#0000ff&quot; size=&quot;1&quot;&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R9U_HsXzjhI/AAAAAAAAAH8/D6JJjI7f3pU/mousedown%5B6%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;424&quot; alt=&quot;mousedown&quot; src=&quot;http://lh6.google.com/elsharpo/R9U_IcXzjiI/AAAAAAAAAIE/asetE7cPtig/mousedown_thumb%5B4%5D&quot; width=&quot;795&quot; border=&quot;0&quot;&gt;&lt;/a&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color=&quot;#0000ff&quot; size=&quot;1&quot;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;And to finish up, when the TextBox has focus and the user presses the down arrow key, this means that she will like to go on an navigate through the employee names in the ListBox. To to this we added a KeyDown listener on the TextBox.&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R9U_I8XzjjI/AAAAAAAAAIM/aCJTX8WoCtQ/keydowned_textarea%5B3%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;176&quot; alt=&quot;keydowned_textarea&quot; src=&quot;http://lh6.google.com/elsharpo/R9U_JcXzjkI/AAAAAAAAAIU/nuvUfhU0yYk/keydowned_textarea_thumb%5B1%5D&quot; width=&quot;644&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;/font&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;Out of interest I then opened the solution file in Expression Blend 2.5 March edition and got to play with colours etc etc. Very cool but well outside my domain all this designer stuff..&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R9U_KcXzjlI/AAAAAAAAAIc/8Qfdv-eDgsE/image%5B34%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;461&quot; alt=&quot;image&quot; src=&quot;http://lh4.google.com/elsharpo/R9U_L8XzjmI/AAAAAAAAAIk/Vw1LUienChE/image_thumb%5B20%5D&quot; width=&quot;781&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/pre&gt;
&lt;p&gt;Well, is all good, I learned heaps about Xaml etc. VEry powerful, but also learned that things can get very complicated very quickly. There are some sample apps that come with Blend that are plain scary.&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9U_NMXzjnI/AAAAAAAAAIs/A7U4HNL5Vak/image%5B38%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;301&quot; alt=&quot;image&quot; src=&quot;http://lh4.google.com/elsharpo/R9U_N8XzjoI/AAAAAAAAAI0/lKibgcFXxpQ/image_thumb%5B22%5D&quot; width=&quot;394&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/pre&gt;
&lt;p&gt;A photo book where you can &quot;grab&quot; the pages from the lower right corner..&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9U_PMXzjpI/AAAAAAAAAI8/7vVyu_MrBYI/image%5B42%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;296&quot; alt=&quot;image&quot; src=&quot;http://lh5.google.com/elsharpo/R9U_QMXzjqI/AAAAAAAAAJE/y2vk_KGGTm8/image_thumb%5B24%5D&quot; width=&quot;396&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/pre&gt;
&lt;p&gt;impressive this paint look-alike..&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R9U_RMXzjrI/AAAAAAAAAJM/S3ZYwp02EkU/image%5B46%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;307&quot; alt=&quot;image&quot; src=&quot;http://lh4.google.com/elsharpo/R9U_R8XzjsI/AAAAAAAAAJU/C9juCrVxzjg/image_thumb%5B26%5D&quot; width=&quot;401&quot; border=&quot;0&quot;&gt;&lt;/a&gt; &lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;/font&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;/font&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;font size=&quot;1&quot;&gt;&lt;/font&gt;&amp;nbsp;&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;
&lt;p&gt;&lt;font size=&quot;1&quot;&gt;&lt;/font&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;pre class=&quot;code&quot;&gt;&amp;nbsp;&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;/ol&gt;  </description><link>http://elsharpo.blogspot.com/2008/03/playing-around-with-silverlight-20-beta.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-9143836813528301614</guid><pubDate>Fri, 07 Mar 2008 09:54:00 +0000</pubDate><atom:updated>2008-03-08T19:58:11.191+11:00</atom:updated><title>Silverlight Beta 1 and Silverlight Tools 1 for VS2008 Released!</title><description>&lt;div class=&quot;wlWriterSmartContent&quot; id=&quot;scid:0767317B-992E-4b12-91E0-4F059A8CECA8:27c1c011-68cf-4812-9251-d9a5f6bee741&quot; style=&quot;padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px&quot;&gt;Technorati Tags: &lt;a href=&quot;http://technorati.com/tags/Silverlight&quot; rel=&quot;tag&quot;&gt;Silverlight&lt;/a&gt;&lt;/div&gt;  &lt;p&gt;At MIX08 Microsoft announced the releases of Silverlight 2 Beta 1 and the tools suite for VS2008.&lt;/p&gt;  &lt;p&gt;The download for the plug in is around 4 MB for SL 2, and over 54MB for the tools.&lt;/p&gt;  &lt;p&gt;Tools can be found &lt;a href=&quot;http://www.microsoft.com/downloads/thankyou.aspx?familyId=e0bae58e-9c0b-4090-a1db-f134d9f095fd&amp;amp;displayLang=en#&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt; and the Silverlight plug-in can be downloaded from &lt;a href=&quot;http://www.microsoft.com/silverlight/resources/installationFiles.aspx?v=2.0&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;If you want to get into it I reckon a good place to start will be in &lt;a href=&quot;http://silverlight.net/Learn/&quot;&gt;http://silverlight.net/Learn/&lt;/a&gt; where you can find &lt;a href=&quot;http://silverlight.net/learn/tutorials.aspx&quot; target=&quot;_blank&quot;&gt;tutorials&lt;/a&gt; and &lt;a href=&quot;http://silverlight.net/learn/labs.aspx&quot; target=&quot;_blank&quot;&gt;labs&lt;/a&gt;!? &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/bb404701.aspx&quot; target=&quot;_blank&quot;&gt;MSDN articles&lt;/a&gt; are a bit rough but useful.&lt;/p&gt;  &lt;p&gt;Source code for the controls and Unit tests were also released and can be found &lt;a href=&quot;http://www.microsoft.com/downloads/thankyou.aspx?familyId=ea93dd89-3af2-4acb-9cf4-bfe01b3f02d4&amp;amp;displayLang=en&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Also it was announced the release of &lt;a href=&quot;http://www.microsoft.com/expression/products/download.aspx?key=blend2dot5&quot; target=&quot;_blank&quot;&gt;Expression Blend 2.5 March Preview&lt;/a&gt;, which you will need if you want to play around with the labs in the learn section.&lt;/p&gt;  &lt;p&gt;Heard in dot.rocks about the Telerik RadControls for Silverlight controls, you can have a look at their work in progress &lt;a href=&quot;http://www.telerik.com/products/silverlight/overview.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I haven&#39;t played with Silverlight at all just yet but I been following what&#39;s coming out of Redmond through ScottGu and his blog. He published a number of &lt;a href=&quot;http://weblogs.asp.net/scottgu/archive/2008/02/22/first-look-at-silverlight-2.aspx&quot; target=&quot;_blank&quot;&gt;tutorials&lt;/a&gt; that I&#39;m eager to have a closer look now that I installed the tools. &lt;/p&gt;  &lt;p&gt;I wonder what impact this is going to have on the web?&lt;/p&gt;  &lt;p&gt;Lots of controls out of the box..&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R9EQ4cXzjHI/AAAAAAAAAEg/3-kNxzP4Fjg/Silver_toolbox%5B5%5D&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;771&quot; alt=&quot;Silver_toolbox&quot; src=&quot;http://lh4.google.com/elsharpo/R9EQ48XzjII/AAAAAAAAAEo/vrAVGl1C_6E/Silver_toolbox_thumb%5B3%5D&quot; width=&quot;327&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I also had to create a simple HelloWorld type page. And I have to admit it looks cool.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R9EQ5cXzjJI/AAAAAAAAAEw/6PsYd0G2f20/Silver_hello_world%5B3%5D&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;294&quot; alt=&quot;Silver_hello_world&quot; src=&quot;http://lh4.google.com/elsharpo/R9EQ58XzjKI/AAAAAAAAAE4/-eGVLoUETyU/Silver_hello_world_thumb%5B1%5D&quot; width=&quot;386&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The XAML...&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh6.google.com/elsharpo/R9EQ6cXzjLI/AAAAAAAAAFA/pC3Jz03vJ5w/Silver_HelloWorld_Code%5B3%5D&quot;&gt;&lt;img style=&quot;border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px&quot; height=&quot;292&quot; alt=&quot;Silver_HelloWorld_Code&quot; src=&quot;http://lh5.google.com/elsharpo/R9EQ7MXzjMI/AAAAAAAAAFI/42PXOQe58_M/Silver_HelloWorld_Code_thumb%5B1%5D&quot; width=&quot;644&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;  </description><link>http://elsharpo.blogspot.com/2008/03/silverlight-beta-1-and-silverlight.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-5359645917547034380</guid><pubDate>Thu, 28 Feb 2008 13:56:00 +0000</pubDate><atom:updated>2008-02-29T02:09:41.559+11:00</atom:updated><title>AjaxControl Toolkit AutoCompleteExtender with WCF</title><description>&lt;div class=&quot;wlWriterSmartContent&quot; id=&quot;scid:0767317B-992E-4b12-91E0-4F059A8CECA8:154976f3-383f-4ade-b8ac-d2bc11895106&quot; style=&quot;padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px&quot;&gt;Technorati Tags: &lt;a href=&quot;http://technorati.com/tags/WCF&quot; rel=&quot;tag&quot;&gt;WCF&lt;/a&gt;,&lt;a href=&quot;http://technorati.com/tags/LINQ&quot; rel=&quot;tag&quot;&gt;LINQ&lt;/a&gt;,&lt;a href=&quot;http://technorati.com/tags/ASP.NET&quot; rel=&quot;tag&quot;&gt;ASP.NET&lt;/a&gt;,&lt;a href=&quot;http://technorati.com/tags/C#&quot; rel=&quot;tag&quot;&gt;C#&lt;/a&gt;,&lt;a href=&quot;http://technorati.com/tags/.Net%203.5&quot; rel=&quot;tag&quot;&gt;.Net 3.5&lt;/a&gt;,&lt;a href=&quot;http://technorati.com/tags/WebServices&quot; rel=&quot;tag&quot;&gt;WebServices&lt;/a&gt;&lt;/div&gt;  &lt;p&gt;Went to Heroes yesterday and one of the things that really impressed was the session on &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/ms735119.aspx&quot;&gt;Windows Communication Foundation&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;From MSDN, &amp;quot;Windows Communication Foundation (WCF) is Microsoft&#39;s unified programming model for building service oriented applications&amp;quot;.&lt;/p&gt;  &lt;p&gt;First thing to do of course, is add a new Ajax enabled WCF Service to your solution.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R8a9jt-7cmI/AAAAAAAAABQ/yt5PkHsZ3zY/AddAjaxEnabledWCFService%5B22%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;492&quot; alt=&quot;AddAjaxEnabledWCFService&quot; src=&quot;http://lh6.google.com/elsharpo/R8a9lN-7cnI/AAAAAAAAABc/Do2QmvRsqFc/AddAjaxEnabledWCFService_thumb%5B18%5D&quot; width=&quot;647&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;What I wanted to do is have the ubiquitous textbox with an autocompleteextender. The code is very simple. &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R8bEUd-7coI/AAAAAAAAABk/p6OhA3QQhHI/autocompleteExtenderTag%5B3%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;129&quot; alt=&quot;autocompleteExtenderTag&quot; src=&quot;http://lh5.google.com/elsharpo/R8bEU9-7cpI/AAAAAAAAABs/UaQkWlFX-mw/autocompleteExtenderTag_thumb%5B1%5D&quot; width=&quot;651&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Then I went into MyAjaxEnabledService class and added to get my books names once the user entered the book names.&lt;/p&gt;  &lt;p&gt;When creating a WCF service, you have to define the contract for the services that are going to be called by the consumers and describe how to interact with your service.&lt;/p&gt;  &lt;p&gt;You do this by creating an interface that defines the input and output types which apply to the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute(VS.85).aspx&quot;&gt;ServiceContract&lt;/a&gt; Attribute to the interface and the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.servicemodel.operationcontractattribute(VS.85).aspx&quot;&gt;OperationContract&lt;/a&gt; Attribute to the methods that you want to expose. &lt;/p&gt;  &lt;p&gt;In this case I have set the &lt;strong&gt;ServiceAttribute&lt;/strong&gt; with a namespace value of elJuan.org :-)&lt;/p&gt;  &lt;p&gt;And then created the methods that I want to reach from my aspx pages and&amp;#160; expose as part of the public contract and adorned them with the &lt;strong&gt;OperationContract&lt;/strong&gt; Attribute.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R8bEVt-7cqI/AAAAAAAAAB0/jP7rFs6EnNY/servicecodefile%5B7%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;435&quot; alt=&quot;servicecodefile&quot; src=&quot;http://lh6.google.com/elsharpo/R8bEWN-7crI/AAAAAAAAAB8/d8t42sKeEP8/servicecodefile_thumb%5B3%5D&quot; width=&quot;735&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Last thing to do, on our ScriptManager we add a reference to our new service and the path to it.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R8bNFd-7csI/AAAAAAAAACE/yPnCnYeCtAE/servicecodefile%5B49%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;122&quot; alt=&quot;servicecodefile&quot; src=&quot;http://lh5.google.com/elsharpo/R8bNF9-7ctI/AAAAAAAAACM/hN-dy_2kXpg/servicecodefile_thumb%5B31%5D&quot; width=&quot;734&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Ok, now we are all set, note that I have for the purposes of this simple post an array with &amp;quot;books&amp;quot; and by pure coincidence, three of them start with &#39;Coo&#39;.&lt;/p&gt;  &lt;p&gt;Run the project and entering Coo.. our service will be called and the books which name start with Coo will be returned to us.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R8bNGd-7cuI/AAAAAAAAACU/tAcICJB3vw8/servicecodefile%5B13%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;222&quot; alt=&quot;servicecodefile&quot; src=&quot;http://lh5.google.com/elsharpo/R8bNG9-7cvI/AAAAAAAAACc/ewY2pkGurRI/servicecodefile_thumb%5B7%5D&quot; width=&quot;296&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Also not that I have another public contract called GetBookByID(int):String&lt;/p&gt;  &lt;p&gt;What I want to do is get a book by it&#39;s ID,when we click the &amp;quot;show me&amp;quot; button,&amp;#160; for this simple example I&#39;m just indexing the array, but it can be easily expanded to get data from a database etc. &lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh3.google.com/elsharpo/R8bNHd-7cwI/AAAAAAAAACk/OCzxXpD4PQo/servicecodefile%5B24%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;181&quot; alt=&quot;servicecodefile&quot; src=&quot;http://lh5.google.com/elsharpo/R8bNH9-7cxI/AAAAAAAAACs/39mduAUZpVM/servicecodefile_thumb%5B16%5D&quot; width=&quot;305&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;First we created a number of JavaScript functions that I&#39;m going to use for this calls.&lt;/p&gt;  &lt;p&gt;I have wired up the showMe button onclick event to my InvokeWCF() Javascript function.&lt;/p&gt;  &lt;p&gt;Note that as I type the name of my WCF service, VS&#39;s intellisense will show me the available public contracts, in this case GetBookByID()&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R8bNIt-7cyI/AAAAAAAAAC0/EHwZOyn7oic/servicecodefile%5B28%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;421&quot; alt=&quot;servicecodefile&quot; src=&quot;http://lh6.google.com/elsharpo/R8bNJN-7czI/AAAAAAAAAC8/0iUxrn4fp-M/servicecodefile_thumb%5B18%5D&quot; width=&quot;644&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now note how the intellisense again picked up that GetBookByID takes an integer as parameter too, very cool.&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R8bNJt-7c0I/AAAAAAAAADE/NeVAyrAW3qQ/servicecodefile%5B33%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;166&quot; alt=&quot;servicecodefile&quot; src=&quot;http://lh6.google.com/elsharpo/R8bNKN-7c1I/AAAAAAAAADM/1MyEqIHoI2w/servicecodefile_thumb%5B21%5D&quot; width=&quot;649&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You have to define an onSuccess function, and optionally an onFailed and an userContext.&lt;/p&gt;  &lt;p&gt;I defined an Success and OnWCFError function for this first time ever WCF service as show below...&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh5.google.com/elsharpo/R8bN_9-7c6I/AAAAAAAAAD0/bFKjNDTGokI/servicecodefile%5B57%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;359&quot; alt=&quot;servicecodefile&quot; src=&quot;http://lh3.google.com/elsharpo/R8bOAd-7c7I/AAAAAAAAAD8/GdCNuyxur0Y/servicecodefile_thumb%5B35%5D&quot; width=&quot;771&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;And the end result...voila!&lt;/p&gt;  &lt;p&gt;&lt;a href=&quot;http://lh4.google.com/elsharpo/R8bNLt-7c4I/AAAAAAAAADk/Ajp4HimGMDM/servicecodefile%5B44%5D&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;132&quot; alt=&quot;servicecodefile&quot; src=&quot;http://lh6.google.com/elsharpo/R8bNMN-7c5I/AAAAAAAAADs/BU3Y--PrTx8/servicecodefile_thumb%5B28%5D&quot; width=&quot;309&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This is the first ever time I played with WCF and it&#39;s pretty simple. In my next post I&#39;ll generate REST and SOAP protocol enabled services.&lt;/p&gt;  &lt;p&gt;Thanks,&lt;/p&gt;  </description><link>http://elsharpo.blogspot.com/2008/02/ajaxcontrol-toolkit-autocompleteextende.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>8</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-519334379594158959.post-598971757102928789</guid><pubDate>Fri, 15 Feb 2008 05:11:00 +0000</pubDate><atom:updated>2008-02-15T16:12:22.485+11:00</atom:updated><title>First Ever</title><description>thanks mate.

:-)</description><link>http://elsharpo.blogspot.com/2008/02/first-ever.html</link><author>noreply@blogger.com (elsharpo)</author><thr:total>1</thr:total></item></channel></rss>