<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.runatserver.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Patrice Paré</title><link>http://blogs.runatserver.com/ppare/default.aspx</link><description>RunAtServer Consulting - Specialized in Microsoft Web Technologies</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>A Nice and Easy Security Feature For A Web Site</title><link>http://blogs.runatserver.com/ppare/archive/2009/04/21/a-nice-and-easy-security-feature-for-a-web-site.aspx</link><pubDate>Tue, 21 Apr 2009 04:25:56 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:1224</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2009/04/21/a-nice-and-easy-security-feature-for-a-web-site.aspx#comments</comments><description>&lt;p&gt;In the world wide web, a good security policy starts with preventing people to access sensitive information. Someone looking for information will definitely look at the web site structure through the navigation elements, the menu and the sitemap. To prevent users to get this information, you can hide elements of your navigation controls by adding roles to the sitemap file and by adding a single attribute to your web.config.&lt;/p&gt;  &lt;p&gt;First, implement in your web site a form authetication with roles and memberships. For more information on how to do that, I highly recommend &lt;a title="Scott Guthrie&amp;#39;s blog on Roles, Membership and Authentication for ASP.NET" href="http://weblogs.asp.net/scottgu/archive/2006/02/24/ASP.NET-2.0-Membership_2C00_-Roles_2C00_-Forms-Authentication_2C00_-and-Security-Resources-.aspx" target="_blank"&gt;Scott Guthrie&amp;#39;s blog on Roles, Membership and Authentication for ASP.NET&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Second, implement the site map provider in your web.config file.&lt;/p&gt;  &lt;p&gt;Third, add a sitemap file to your web project. In the file, add nodes that maps to your pages.&lt;/p&gt;  &lt;p&gt;Fourth, add a MasterPage, and to it, add a menu control with a sitemapdatasource object. Bind the sitemapdatasource to the site map provider in your web.config file.&lt;/p&gt;  &lt;p&gt;Fifth, add a treeview control in one page and a sitemapdatasource object. Bind the sitemapdatasource to the site map provider in your web.config file.&lt;/p&gt;  &lt;p&gt;Check if everything compiles and that each control are shown appropriately. &lt;/p&gt;  &lt;p&gt;Now, in the SiteMap provider in the web.config file, add the securityTrimmingEnabled attribute and set it to true. &lt;/p&gt;  &lt;p&gt;In your sitemap file, add the role attribute and set its value to one of your roles you have defined. &lt;/p&gt;  &lt;p&gt;Refresh your browser. If you have authenticated with a role not specified in the sitemap, or your are an anonymous user, the nodes with role values will not show for the SiteMap control and the Menu control as well. &lt;/p&gt;  &lt;p&gt;Code Samples :&lt;/p&gt;  &lt;p&gt;SiteMap Providers in the Web.Config file:&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;siteMap&lt;/span&gt; &lt;span class="attr"&gt;defaultProvider&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;TopMenuSiteMapProvider&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;enabled&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;     &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;providers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;         &lt;span class="rem"&gt;&amp;lt;!-- GENERAL ACCESS TO WEB SITE --&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;add&lt;/span&gt; &lt;span class="attr"&gt;siteMapFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;~/TopMenu.sitemap&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;TopMenuSiteMapProvider&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;System.Web.XmlSiteMapProvider&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;/pre&gt;

  &lt;pre&gt;         &lt;span class="rem"&gt;&amp;lt;!-- LEFT MENU WITH SECURITY --&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;         &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;add&lt;/span&gt; &lt;span class="attr"&gt;siteMapFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;~/LeftMenu.sitemap&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LeftMenuSiteMapProvider&amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="attr"&gt;securityTrimmingEnabled&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;System.Web.XmlSiteMapProvider&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;     &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;providers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;siteMap&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Site Map File with role attribute:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;siteMapNode&lt;/span&gt; &lt;span class="attr"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;$Resources:Menu, Administration.Title&amp;quot;&lt;/span&gt;  &lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="attr"&gt;description&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;$Resources:Menu, Administration.Description&amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;     &lt;span class="attr"&gt;roles&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Administrateur&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;A Menu Control bound to a SiteMapDataSource object in the masterpage file:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;asp:SiteMapDataSource&lt;/span&gt; &lt;span class="attr"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;SiteMapDataSource1&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;          &lt;span class="attr"&gt;ShowStartingNode&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;          &lt;span class="attr"&gt;SiteMapProvider&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LeftMenuSiteMapProvider&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;asp:Menu&lt;/span&gt; &lt;span class="attr"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Menu1&amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;           &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;           &lt;span class="attr"&gt;Orientation&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Vertical&amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;           &lt;span class="attr"&gt;DataSourceID&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;SiteMapDataSource1&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;p&gt;&lt;span class="kwrd"&gt;Treeview control bound to a SiteMapDataSource object in a page &lt;/span&gt;&lt;/p&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;asp:SiteMapDataSource&lt;/span&gt; &lt;span class="attr"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;SiteMapDataSource1&amp;quot;&lt;/span&gt; &lt;/pre&gt;

    &lt;pre&gt;     &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;     &lt;span class="attr"&gt;ShowStartingNode&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;/pre&gt;

    &lt;pre&gt;     &lt;span class="attr"&gt;SiteMapProvider&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LeftMenuSiteMapProvider&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;asp:TreeView&lt;/span&gt; &lt;span class="attr"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;TreeView1&amp;quot;&lt;/span&gt; &lt;/pre&gt;

    &lt;pre&gt;      &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;      &lt;span class="attr"&gt;DataSourceID&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;SiteMapDataSource1&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;asp:TreeView&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;p&gt;&lt;span class="kwrd"&gt;&lt;/span&gt;&lt;/p&gt;

  &lt;p&gt;&lt;span class="kwrd"&gt;Let&amp;#39;s have a look at what you are going to see of your menu control in your browser:&lt;/span&gt;&lt;/p&gt;

  &lt;table cellspacing="0" cellpadding="2"&gt;
      &lt;tr&gt;
        &lt;td&gt;Before signing in&lt;/td&gt;

        &lt;td&gt;After signing in&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ANiceandEasySecurityFeatureForAMenu_147E2/logged%20out.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;border-top:0px;border-right:0px;" border="0" alt="logged out" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ANiceandEasySecurityFeatureForAMenu_147E2/logged%20out_thumb.png" width="244" height="128" /&gt;&lt;/a&gt; &lt;/td&gt;

        &lt;td&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ANiceandEasySecurityFeatureForAMenu_147E2/logged%20in.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;border-top:0px;border-right:0px;" border="0" alt="logged in" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ANiceandEasySecurityFeatureForAMenu_147E2/logged%20in_thumb.png" width="244" height="110" /&gt;&lt;/a&gt; &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;And what about the sitemap control:&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="2"&gt;
    &lt;tr&gt;
      &lt;td&gt;Before signing in&lt;/td&gt;

      &lt;td&gt;After signing in&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ANiceandEasySecurityFeatureForAMenu_147E2/sitemaploggedout.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;border-top:0px;border-right:0px;" border="0" alt="sitemaploggedout" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ANiceandEasySecurityFeatureForAMenu_147E2/sitemaploggedout_thumb.png" width="149" height="142" /&gt;&lt;/a&gt; &lt;/td&gt;

      &lt;td&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ANiceandEasySecurityFeatureForAMenu_147E2/sitemaploggedin.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;border-top:0px;border-right:0px;" border="0" alt="sitemaploggedin" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ANiceandEasySecurityFeatureForAMenu_147E2/sitemaploggedin_thumb.png" width="173" height="150" /&gt;&lt;/a&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Preventing people from seeing your web site archithecture is something you want to. And as we have seen it, it implies not code behind coding, only a few configuration here and there. When the Membership and roles are define, configuring the sitemap provider is a matter of a few seconds.&lt;/p&gt;

&lt;p&gt;Best Regards!&lt;/p&gt;

&lt;p&gt;Patrice&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e496082a-f9fa-440c-b840-c35d3ec97042" class="wlWriterSmartContent"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ASP.NET%202.0" rel="tag"&gt;ASP.NET 2.0&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Menu" rel="tag"&gt;Menu&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Security" rel="tag"&gt;Security&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SiteMap" rel="tag"&gt;SiteMap&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SiteMAp%20Providers" rel="tag"&gt;SiteMAp Providers&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TreeView" rel="tag"&gt;TreeView&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=1224" width="1" height="1"&gt;</description><category domain="http://blogs.runatserver.com/ppare/archive/tags/SiteMap+Providers/default.aspx">SiteMap Providers</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/ASP.NET+2.0/default.aspx">ASP.NET 2.0</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/TreeView/default.aspx">TreeView</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/SiteMap/default.aspx">SiteMap</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Security/default.aspx">Security</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Menu/default.aspx">Menu</category></item><item><title>Defining a Trust Level for your web application</title><link>http://blogs.runatserver.com/ppare/archive/2009/04/15/defining-a-trust-level-for-your-web-application.aspx</link><pubDate>Thu, 16 Apr 2009 03:24:34 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:1218</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2009/04/15/defining-a-trust-level-for-your-web-application.aspx#comments</comments><description>&lt;p&gt;Last weekend I worked on an assignment which consisted on determining how to read, write and delete files in a specific folder and to read, insert, update and delete records in an MS Access 2003 database in a Medium Trust Level on a shared hosting context. The client mislead the assignment by confusing the Trust Level set at Medium with his capabilities to accomplish the tasks.&lt;/p&gt;  &lt;p&gt;First, the Trust is useful to isolate different applications in a shared hosting environment and to enhance the security by prohibiting web application to access the host system resources. A web server administrator would want to prevent a malicious web applications to access files or directories of other web applications such like the web.config file to read, say, the connection string to get access to confidential data. On the other hand, the web server administrator would want to prevent a good intended or not web application to access the hosting machine resources like the registry, the file system or event logs. &lt;/p&gt;  &lt;p&gt;There are 6 different levels of trust you may use for different purposes:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Full : the default configuration in a development environment &lt;/li&gt;    &lt;li&gt;High &lt;/li&gt;    &lt;li&gt;Medium : the usual setting for a production environment &lt;/li&gt;    &lt;li&gt;Low &lt;/li&gt;    &lt;li&gt;Minimal &lt;/li&gt;    &lt;li&gt;Custom* : you may define your own security policy &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;* This trust does not exist but may be defined and customized accordingly to your needs. &lt;/p&gt;  &lt;p&gt;The trust is define in the web.config file of the Framework 2.0.50727.&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;location&lt;/span&gt; &lt;span class="attr"&gt;allowOverride&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;system.web&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;securityPolicy&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;              &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trustLevel&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Full&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;policyFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;internal&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;              &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trustLevel&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;High&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;policyFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;web_hightrust.config&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;              &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trustLevel&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Medium&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;policyFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;web_mediumtrust.config&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;              &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trustLevel&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Low&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;policyFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;web_lowtrust.config&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;              &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trustLevel&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Minimal&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;policyFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;web_minimaltrust.config&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;         &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;securityPolicy&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;         &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trust&lt;/span&gt; &lt;span class="attr"&gt;level&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Full&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;originUrl&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;     &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;system.web&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt; &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;location&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;There are several remarks to be done here. First, the security policy may be overridden because the property allowOverride is set to true. Also, the trust is set to Full. To override the security policy, the web.config file of the web application must be modified as follow:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;system.web&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trust&lt;/span&gt; &lt;span class="attr"&gt;level&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Medium&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;originUrl&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;span class="kwrd"&gt;Inside the &amp;lt;system.web&amp;gt; node, add a trust element. Set its level to one of the level available. &lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The trust levels are defined in a config file in the %WINDIR%\Microsoft.NET\Framework\v2.0.50727\CONFIG directory. The full trust level is a special case where the ASP.NET host does not apply any security and is therefore considered as having full trust on the local machine zone. It is mapped to an internal handler. The other trust levels are defined in files. The file has two main sections. The first define the security classes and the second the permission related to it. &lt;/p&gt;

&lt;p&gt;To understand what is a Medium Trust Level, it is good to compare the web_mediumtrust.config file with the web_hightrust.config file. When comparing the two files, we remark the medium one has much less IPermission elements define. Also, some IPermission are much more restrictive. For instance, the medium FileIOPermission has read, write, append and PathDirectory only while the high FileIOPermission has unrestrictive permission. &lt;/p&gt;

&lt;p&gt;You can write your own custom security file. &lt;/p&gt;

&lt;p&gt;First, copy the %WINDIR%\Microsoft.NET\Framework\v2.0.50727\CONFIG\web_mediumtrust.config and rename it to something like web_customtrust.config.&lt;/p&gt;

&lt;p&gt;Second, open the new file and add, remove, modify the file to meet your needs. To add a new security, add a SecurityClass and add a IPermission. &lt;/p&gt;

&lt;p&gt;Third, open the web.config file of the %WINDIR%\Microsoft.NET\Framework\v2.0.50727\CONFIG\ directory. Add your custom policy. &lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trustLevel&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Custom&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;policyFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;web_CustomTrust.config&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Set the allowOverride to true.&lt;/p&gt;

&lt;p&gt;Fourth, open the web.config file of your web application. &lt;/p&gt;

&lt;p&gt;Fifth, inside the system.web node, add a trust node and set its trust level to Custom.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;system.web&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trust&lt;/span&gt; &lt;span class="attr"&gt;level&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Custom&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;originUrl&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Another way to reference the custom trust file, is to add the web_CustomTrust.config file at the root of the web application. Then, in the web.config file, add the following nodes :&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;location&lt;/span&gt; &lt;span class="attr"&gt;allowOverride&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;system.web&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;securityPolicy&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trustLevel&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Custom&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;policyFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;web_CustomTrust.config&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;securityPolicy&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;trust&lt;/span&gt; &lt;span class="attr"&gt;level&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Custom&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;originUrl&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;system.web&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;location&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;For more information on trust levels, I suggest you to read these articles:&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="2"&gt;
    &lt;tr&gt;
      &lt;td&gt;Source:&lt;/td&gt;

      &lt;td&gt;URL:&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;MSDN&lt;/td&gt;

      &lt;td&gt;
        &lt;p&gt;&lt;a title="How To: Use Medium Trust in ASP.NET 2.0" href="http://msdn.microsoft.com/en-us/library/ms998341.aspx#paght000020_developingformediumtrust" target="_blank"&gt;How To: Use Medium Trust in ASP.NET 2.0&lt;/a&gt;&lt;/p&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;&amp;#160;&lt;/td&gt;

      &lt;td&gt;
        &lt;p&gt;&lt;a title="How To: Use Code Access Security in ASP.NET 2.0" href="http://msdn.microsoft.com/en-us/library/ms998326.aspx" target="_blank"&gt;How To: Use Code Access Security in ASP.NET 2.0&lt;/a&gt;&lt;/p&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;&amp;#160;&lt;/td&gt;

      &lt;td&gt;
        &lt;p&gt;&lt;a title="Chapter 9 &amp;#8211; Using Code Access Security with ASP.NET" href="http://msdn.microsoft.com/en-us/library/aa302425.aspx" target="_blank"&gt;Chapter 9 &amp;#8211; Using Code Access Security with ASP.NET&lt;/a&gt;&lt;/p&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;&amp;#160;&lt;/td&gt;

      &lt;td&gt;&lt;a title="ASP.NET Trust Levels and Policy Files" href="http://msdn.microsoft.com/en-us/library/wyts434y(VS.71).aspx" target="_blank"&gt;ASP.NET Trust Levels and Policy Files&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;&amp;#160;&lt;/td&gt;

      &lt;td&gt;&lt;a title="Trust Levels and Policy Files for ASP.NET (IIS 6.0)" href="http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/1f7959af-2eac-4418-91a3-56006ea6eb07.mspx?mfr=true" target="_blank"&gt;Trust Levels and Policy Files for ASP.NET (IIS 6.0)&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;

&lt;table cellspacing="0" cellpadding="2"&gt;
    &lt;tr&gt;
      &lt;td&gt;Rick Strahl&lt;/td&gt;

      &lt;td&gt;&lt;a title="Running ASP.NET in Medium Trust" href="http://west-wind.com/weblog/posts/6344.aspx" target="_blank"&gt;Running ASP.NET in Medium Trust&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;From the MSDN Site (&lt;a title="How To: Use Code Access Security in ASP.NET 2.0" href="http://msdn.microsoft.com/en-us/library/ms998326.aspx" target="_blank"&gt;How To: Use Code Access Security in ASP.NET 2.0&lt;/a&gt;) this table is quite informative:&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="2"&gt;
    &lt;tr&gt;
      &lt;td align="center"&gt;&lt;font color="#004080"&gt;&lt;strong&gt;Trust Level&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;

      &lt;td align="center"&gt;&lt;font color="#004080"&gt;&lt;strong&gt;Key Capabilities and Restrictions&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;Full&lt;/td&gt;

      &lt;td&gt;No restrictions imposed by code access security.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;High&lt;/td&gt;

      &lt;td&gt;No unmanaged code.
        &lt;br /&gt;No enterprise services.

        &lt;br /&gt;Can access Microsoft SQL Server and other OLE DB data sources.

        &lt;br /&gt;Can send e-mail by using SMTP servers.

        &lt;br /&gt;Very limited reflection permissions. No ability to invoke code by using reflection.

        &lt;br /&gt;A broad set of other framework features are available. Applications have full access to the file system and to sockets.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;Medium&lt;/td&gt;

      &lt;td&gt;Permissions are limited to what the application can access within the directory structure of the application.
        &lt;br /&gt;No file access is permitted outside of the application&amp;#39;s virtual directory hierarchy.

        &lt;br /&gt;Can access SQL Server.

        &lt;br /&gt;Can send e-mail by using SMTP servers.

        &lt;br /&gt;Limited rights to certain common environment variables.

        &lt;br /&gt;No reflection permissions whatsoever.

        &lt;br /&gt;No sockets permission.

        &lt;br /&gt;To access Web resources, you must explicitly add endpoint URLs&amp;#8212;either in the &lt;strong&gt;originUrl&lt;/strong&gt; attribute of the &amp;lt;&lt;strong&gt;trust&lt;/strong&gt;&amp;gt; element or inside the policy file.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;Low&lt;/td&gt;

      &lt;td&gt;Intended to model the concept of a read-only application with no network connectivity.
        &lt;br /&gt;Read only access for file I/O within the application&amp;#39;s virtual directory structure.&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;Minimal&lt;/td&gt;

      &lt;td&gt;Execute only.
        &lt;br /&gt;No ability to change the &lt;strong&gt;IPrincipal&lt;/strong&gt; on a thread or on the &lt;strong&gt;HttpContext&lt;/strong&gt;.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;

&lt;p&gt;
  &lt;br /&gt;Also, have a look at &lt;strong&gt;Table 4: Default ASP.NET Policy Permissions and Trust Levels &lt;/strong&gt;on the same web page which will explain you what permissions are available at what level.&lt;/p&gt;

&lt;p&gt;An exception may be raised if the defined trust level does not allow your action. It may look something like this:&lt;/p&gt;

&lt;p&gt;Request for the permission of type &amp;#39;System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&amp;#39; failed. mscorlib&lt;/p&gt;

&lt;p&gt;This exception was raised because the FileIOPermission did not authorised the performed action.&lt;/p&gt;

&lt;p&gt; Finally, the assignment I spoke of earlier was more about configuring a folder to give Modify permission (read, write, delete) and giving away a few piece of code on how to connect to a MS Access database to read, update, insert and delete records and on how to read, write and delete files a folder. &lt;/p&gt;

&lt;p&gt;Best regards,&lt;/p&gt;

&lt;p&gt;Patrice&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:09925131-769a-4726-a327-17ff91cb7aef" class="wlWriterSmartContent"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/Trust" rel="tag"&gt;Trust&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SecurityClass" rel="tag"&gt;SecurityClass&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Security" rel="tag"&gt;Security&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Policy" rel="tag"&gt;Policy&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Medium%20Trust%20Level" rel="tag"&gt;Medium Trust Level&lt;/a&gt;,&lt;a href="http://technorati.com/tags/shared%20hosting" rel="tag"&gt;shared hosting&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ASP.NET%202.0" rel="tag"&gt;ASP.NET 2.0&lt;/a&gt;,&lt;a href="http://technorati.com/tags/FileIOPermission" rel="tag"&gt;FileIOPermission&lt;/a&gt;,&lt;a href="http://technorati.com/tags/OleDbPermission" rel="tag"&gt;OleDbPermission&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=1218" width="1" height="1"&gt;</description><category domain="http://blogs.runatserver.com/ppare/archive/tags/ASP.NET+2.0/default.aspx">ASP.NET 2.0</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/.NET+Framework/default.aspx">.NET Framework</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/FileIOPermission/default.aspx">FileIOPermission</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Shared+Hosting/default.aspx">Shared Hosting</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/IPermission/default.aspx">IPermission</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Trust/default.aspx">Trust</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Medium+Trust+Level/default.aspx">Medium Trust Level</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/SecurityClass/default.aspx">SecurityClass</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/OleDbPermission/default.aspx">OleDbPermission</category></item><item><title>Resizing GIF Images</title><link>http://blogs.runatserver.com/ppare/archive/2009/03/22/resizing-gif-images.aspx</link><pubDate>Sun, 22 Mar 2009 04:24:02 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:713</guid><dc:creator>ppare</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2009/03/22/resizing-gif-images.aspx#comments</comments><description>&lt;p&gt;I worked this week on a Console application that would allow me to resize all images in a folder to thumbnail size of 128 by 128 pixels. This small project was meant to correct a problem on a web site. My friend can upload any kind of images (bmp, jpg, gif, png) of any size to her web site. The images are then displayed in a catalogue. There are up to 20 images per page. For consistency, every image are set to be displayed in a 128x128 pixels. When the user click on the image, it is displayed in it original size in another browser:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;#39;http://www.website.com/Uploads/Catalogue/myImage001.jpg&amp;#39;&lt;/span&gt; 
          &lt;span class="attr"&gt;target&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;_blank&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;lnkImage&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;img&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;imgItem&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;My Image&amp;quot;&lt;/span&gt; 
          &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;../Uploads/Catalogue/myImage001.jpg&amp;quot;&lt;/span&gt;
          &lt;span class="attr"&gt;style&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;height:128px;width:128px;border-width:0px;&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The problem with this html is if the image is large, it will take time to download. On the other hand, if the image is small, the user does not get anything better when clicking on the image to look at it in another browser because it is of the same size.&lt;/p&gt;

&lt;p&gt;To correct this, I suggested her to always upload image files in their original size, the larger the better. And I would take care of displaying images on the right size. &lt;/p&gt;

&lt;p&gt;So I develop a class that I would add to the business logic layer of her web site and also to a console application which would run once to resize all of her images already uploaded on her web server.&lt;/p&gt;

&lt;p&gt;Here is the class I creates;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; Resize images to 128x128px&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;param name=&amp;quot;sourceFilename&amp;quot;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39;              Complete input file name&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;param name=&amp;quot;targetFilename&amp;quot;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39;                 Complete output file name&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt; &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; ResizeImageBad( _ &lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;              &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; sourceFilename &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;, _&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;              &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; targetFilename &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;)&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;     &lt;span class="kwrd"&gt;Dim&lt;/span&gt; img &lt;span class="kwrd"&gt;As&lt;/span&gt; System.Drawing.Image = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;     &lt;span class="kwrd"&gt;Dim&lt;/span&gt; graph &lt;span class="kwrd"&gt;As&lt;/span&gt; Graphics = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;     &lt;span class="kwrd"&gt;Dim&lt;/span&gt; smallImg &lt;span class="kwrd"&gt;As&lt;/span&gt; System.Drawing.Image&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;     &lt;span class="kwrd"&gt;Dim&lt;/span&gt; rectangle &lt;span class="kwrd"&gt;As&lt;/span&gt; Rectangle&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;     &lt;span class="kwrd"&gt;Dim&lt;/span&gt; smlSize &lt;span class="kwrd"&gt;As&lt;/span&gt; Size = &lt;span class="kwrd"&gt;New&lt;/span&gt; Size(128, 128)&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;     &lt;span class="kwrd"&gt;Try&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;         img = System.Drawing.Image.FromFile(_ &lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;                            sourceFilename)&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;         smallImg = &lt;span class="kwrd"&gt;New&lt;/span&gt; Bitmap(_ &lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;                            smlSize.Width, _&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;                            smlSize.Height, _&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;                            img.PixelFormat)&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;         rectangle = &lt;span class="kwrd"&gt;New&lt;/span&gt; Rectangle(_&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;                            0, _&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;                            0, _&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;                            smlSize.Width, _&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;                            smlSize.Height)&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;  &lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;         graph = Graphics.FromImage(smallImg)&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;         graph.CompositingQuality = CompositingQuality.HighQuality&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;         graph.SmoothingMode = SmoothingMode.HighQuality&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;         graph.InterpolationMode = InterpolationMode.Bicubic&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;         graph.DrawImage(img, rectangle)&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;         smallImg.Save(targetFilename)&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;    &lt;span class="kwrd"&gt;Catch&lt;/span&gt; ex &lt;span class="kwrd"&gt;As&lt;/span&gt; Exception&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;         cLogEventViewer.LogError( _&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;                System.Reflection.MethodBase.
GetCurrentMethod.DeclaringType.Name &amp;amp; _&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  44:  &lt;/span&gt;                 &lt;span class="str"&gt;&amp;quot; - Exception Resize Image - &amp;quot;&lt;/span&gt; &amp;amp; _&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  45:  &lt;/span&gt;                ex.Message &amp;amp; &lt;span class="str"&gt;&amp;quot; &amp;quot;&lt;/span&gt; &amp;amp; _&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  46:  &lt;/span&gt;                ex.StackTrace)&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  47:  &lt;/span&gt;     &lt;span class="kwrd"&gt;Finally&lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;Clean up!&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  48:  &lt;/span&gt;         &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; img &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  49:  &lt;/span&gt;             img.Dispose()&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  50:  &lt;/span&gt;             img = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  51:  &lt;/span&gt;         &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  52:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  53:  &lt;/span&gt;         &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; graph &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  54:  &lt;/span&gt;             graph.Dispose()&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  55:  &lt;/span&gt;             graph = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  56:  &lt;/span&gt;         &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  57:  &lt;/span&gt;         smlSize = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  58:  &lt;/span&gt;         rectangle = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  59:  &lt;/span&gt;     &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Try&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&lt;span class="lnum"&gt;  60:  &lt;/span&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre&gt;&amp;#160;&lt;/pre&gt;

    &lt;p&gt;When I ran the console application targeting a folder containing only jpg, gif or jpg files, the console application throw exceptions for gif files. The png files were not resized and fortunately the jpg were ok.&lt;/p&gt;

    &lt;p&gt;Here is the log error from the event viewer:&lt;/p&gt;

    &lt;blockquote&gt;
      &lt;p&gt;&amp;quot;A Graphics object cannot be created from an image that has an indexed pixel format. &amp;quot;&lt;/p&gt;
    &lt;/blockquote&gt;

    &lt;pre&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ResizingImagesinBatch_10EA0/event%20viewer.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="304" alt="Error log from Event Viewer" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ResizingImagesinBatch_10EA0/event%20viewer_thumb.png" width="445" border="0" /&gt;&lt;/a&gt;&lt;/pre&gt;

    &lt;p&gt;&amp;#160; After a few readings, I found out that the problem was at the pixel format level. In the code sample I provided earlier, I used the pixel format provided by the System.Drawing.Image object instance loaded from the file name as you can see on line 26.&lt;/p&gt;

    &lt;p&gt;I changed the pixel format to System.Drawing.Image.PixelFormat.Format24bppRgb which provides 24 bits per pixel, 8 bits for Red, 8 for Green and and 8 for Blue. This change stopped the errors. The GIF files were resized and their weight were lighter. Unfortunately, when I tried to view the image applications I used sent an error on opening. &lt;/p&gt;

    &lt;p&gt;I search a little further and I added another filter on the file,&lt;/p&gt;

    &lt;pre class="csharpcode"&gt;graph.PixelOffsetMode = PixelOffsetMode.HighQuality&lt;/pre&gt;

    &lt;p&gt;that I inserted between line 34 and 35. Also, I looked for the &amp;quot;.gif&amp;quot; extension at the end of the file name. If the file name ended with &amp;quot;.gif&amp;quot;, I then specified that specific format:&lt;/p&gt;

    &lt;pre class="csharpcode"&gt;smallImg.Save(targetFilename, Imaging.ImageFormat.Gif)&lt;/pre&gt;

    &lt;p&gt;Also, I changed the InterpolationMode to HighQualityByCubic which provides a higher quality after resizing the GIF file.&lt;/p&gt;

    &lt;p&gt;Let&amp;#39;s have a look at the images. On the next image, on the left, is the original GIF image and on the right is the resized GIF image.&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ResizingImagesinBatch_10EA0/ImageQualityDefect.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="311" alt="GIF Image Quality Issu" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/ResizingImagesinBatch_10EA0/ImageQualityDefect_thumb.png" width="417" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;As you can see, the image on the right, after resizing has lost some quality. &lt;/p&gt;

    &lt;p&gt;Well... I have looked for quite some time now on how I could solve the quality issue and I must admit it, I am now clueless. To complete the application, however, I decided to reformat the GIF to JPEG. doing this solves completely the quality issue but does not answer how I could resize GIF images.&lt;/p&gt;

    &lt;p&gt;If someone has an answer, I am curious to know how you did it.&lt;/p&gt;

    &lt;p&gt;But for now, here is the method I use to resize images:&lt;/p&gt;

    &lt;div class="csharpcode" style="overflow:scroll;"&gt;
      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; Resize images to 128x128px&lt;/span&gt;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;param name=&amp;quot;sourceFilename&amp;quot;&amp;gt;Complete input file name&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;param name=&amp;quot;targetFilename&amp;quot;&amp;gt;Complete output file name&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="rem"&gt;&amp;#39;&amp;#39;&amp;#39; &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; ResizeImage( _&lt;/pre&gt;

      &lt;pre class="alt"&gt;          &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; sourceFilename &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;, _&lt;/pre&gt;

      &lt;pre class="alt"&gt;          &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; targetFilename &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;)&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;   &lt;span class="kwrd"&gt;Dim&lt;/span&gt; img &lt;span class="kwrd"&gt;As&lt;/span&gt; System.Drawing.Image = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;   &lt;span class="kwrd"&gt;Dim&lt;/span&gt; graph &lt;span class="kwrd"&gt;As&lt;/span&gt; Graphics = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;   &lt;span class="kwrd"&gt;Dim&lt;/span&gt; smallImg &lt;span class="kwrd"&gt;As&lt;/span&gt; System.Drawing.Image&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;   &lt;span class="kwrd"&gt;Dim&lt;/span&gt; rectangle &lt;span class="kwrd"&gt;As&lt;/span&gt; Rectangle&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;   &lt;span class="kwrd"&gt;Dim&lt;/span&gt; smlSize &lt;span class="kwrd"&gt;As&lt;/span&gt; Size = &lt;span class="kwrd"&gt;New&lt;/span&gt; Size(128, 128)&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    &lt;span class="kwrd"&gt;Try&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;      img = System.Drawing.Image.FromFile(sourceFilename)&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;      smallImg = &lt;span class="kwrd"&gt;New&lt;/span&gt; Bitmap(smlSize.Width, smlSize.Height, PixelFormat.Format24bppRgb)&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;      rectangle = &lt;span class="kwrd"&gt;New&lt;/span&gt; Rectangle(0, 0, smlSize.Width, smlSize.Height)&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;       graph = Graphics.FromImage(smallImg)&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;       graph.CompositingQuality = CompositingQuality.HighQuality&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;       graph.SmoothingMode = SmoothingMode.HighQuality&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;       graph.PixelOffsetMode = PixelOffsetMode.HighQuality&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;       graph.InterpolationMode = InterpolationMode.HighQualityBicubic&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;       graph.DrawImage(img, rectangle)&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;       &lt;span class="kwrd"&gt;If&lt;/span&gt; targetFilename.LastIndexOf(&lt;span class="str"&gt;&amp;quot;.gif&amp;quot;&lt;/span&gt;) &amp;gt; 0 &lt;span class="kwrd"&gt;AndAlso&lt;/span&gt; targetFilename.EndsWith(&lt;span class="str"&gt;&amp;quot;.gif&amp;quot;&lt;/span&gt;) &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;           targetFilename = targetFilename.Replace(&lt;span class="str"&gt;&amp;quot;.gif&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;.jpg&amp;quot;&lt;/span&gt;)&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;                smallImg.Save(targetFilename)&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;       &lt;span class="kwrd"&gt;Else&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;           smallImg.Save(targetFilename)&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;       &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;   &lt;span class="kwrd"&gt;Catch&lt;/span&gt; ex &lt;span class="kwrd"&gt;As&lt;/span&gt; Exception&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;       cLogEventViewer.LogError( _&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;              System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name &amp;amp; _&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;              &lt;span class="str"&gt;&amp;quot; - Exception Resize Image - &amp;quot;&lt;/span&gt; &amp;amp; _&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;              ex.Message &amp;amp; &lt;span class="str"&gt;&amp;quot; &amp;quot;&lt;/span&gt; &amp;amp; _&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;              ex.StackTrace)&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;   &lt;span class="kwrd"&gt;Finally&lt;/span&gt; &lt;span class="rem"&gt;&amp;#39;Clean up!&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;       &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; img &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;            img.Dispose()&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  44:  &lt;/span&gt;            img = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  45:  &lt;/span&gt;       &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  46:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  47:  &lt;/span&gt;       &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; graph &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  48:  &lt;/span&gt;            graph.Dispose()&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  49:  &lt;/span&gt;            graph = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  50:  &lt;/span&gt;        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  51:  &lt;/span&gt;        smlSize = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  52:  &lt;/span&gt;        rectangle = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;&lt;/pre&gt;

      &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  53:  &lt;/span&gt;    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Try&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="lnum"&gt;  54:  &lt;/span&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
    &lt;/div&gt;

    &lt;p&gt;Have a good one!&lt;/p&gt;

    &lt;p&gt;Cheers!&lt;/p&gt;

    &lt;p&gt;Patrice&lt;/p&gt;
  &lt;/div&gt;

  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:c95aa7d7-ab94-41c4-8168-59f719f3af8a" style="padding-right:0px;display:inline;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/.Net" rel="tag"&gt;.Net&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET%20Framework%202.0" rel="tag"&gt;.NET Framework 2.0&lt;/a&gt;,&lt;a href="http://technorati.com/tags/GDI+" rel="tag"&gt;GDI+&lt;/a&gt;,&lt;a href="http://technorati.com/tags/GIF%20Images" rel="tag"&gt;GIF Images&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=713" width="1" height="1"&gt;</description><category domain="http://blogs.runatserver.com/ppare/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/.NET+Framework/default.aspx">.NET Framework</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/VB.NET/default.aspx">VB.NET</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/GID_2B00_/default.aspx">GID+</category></item><item><title>Adding Nullables To Your Code</title><link>http://blogs.runatserver.com/ppare/archive/2009/03/19/adding-nullables-to-your-code.aspx</link><pubDate>Fri, 20 Mar 2009 03:32:56 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:699</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2009/03/19/adding-nullables-to-your-code.aspx#comments</comments><description>&lt;p&gt;Why should I use Nullables? What are they for? Why have they added the Nullables to the .Net Framework 2.0?&lt;/p&gt;  &lt;p&gt;These are questions I asked myself. And because you are good people, I will share my thoughts with you. Isn&amp;#39;t it wonderful? Nah! I&amp;#39;m just kidding. In fact, the following post is the product of some readings on the topic and I started using it in my day-to-day coding because it answered a real issue.&lt;/p&gt;  &lt;p&gt;First thing first. Let&amp;#39;s consider the following scenario: a form on a web page somehow bound to a database. On the form, there are several kind of inputs: textboxes, dropdownlists, radiobuttons, checkboxes, textboxes for date/time and so on. What if some fields are not mandatory? It means these fields may be left empty. How should you treat an empty field considering that the database accepts null values?&lt;/p&gt;  &lt;p&gt;How do you manage the situation? Because, philosophically speaking, an absence of value is a value. It means the user did not want to add his input, for any reason, on the form. &lt;/p&gt;  &lt;p&gt;With value types however, how could you move that piece of information from the form to the database knowing value types cannot be null (C#). &lt;/p&gt;  &lt;p&gt;A Value type cannot be null because its value is not a pointer to an address in memory but the value itself. Whereas the reference type is a pointer to an address in memory. A null value represents an address in memory, x00000000. So setting a reference type to null is in fact pointing to an address in memory. When times come to instantiate the object, the pointer is redirected to the address in memory where the object is (somewhere on the heap).&lt;/p&gt;  &lt;p&gt;There have been some thoughts spent over this to overcome the impossibility of assigning a null value to value types. Some have thought about adding a bite to all value types where this extra bit would serve as a flag to indicate whether or not the value is null. Although this possibility seems sexy, it seems it would have been a nightmare to manage and moreover to validate the flag whenever accessing its value. For instance, what if a Byte could have an extra bite to flag its has a null value. By definition, a Byte is defined with 256 possibilities ranging from 0 to 255 (00 to FF). An extra bite would mean 257 possibilities (there is the nightmare). &lt;/p&gt;  &lt;p&gt;Another way to represent the null value is to use a default values throughout the application. The problem with this approach is to select a value that will be never use. &lt;/p&gt;  &lt;p&gt;A third way is to wrap the value type in a reference type, such like the Object class or a custom class. Although it seems practical, it is however costly on memory and performance. &lt;/p&gt;  &lt;p&gt;With the coming of the .NET Framework 2.0 and the Generics, the Framework addresses this issue with the Nullable&amp;lt;T&amp;gt; type. This type is a Generic class with a type value constraint. Since only type values can not be null, it makes sense. &lt;/p&gt;  &lt;p&gt;Let&amp;#39;s see how we can use the Nullable&amp;lt;t&amp;gt; Type:&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ShowNullable()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:&lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="rem"&gt;//Nullable&amp;lt;T&amp;gt; aInt = null;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;Nullable&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; aInt = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;try&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;  &lt;span class="kwrd"&gt;int&lt;/span&gt; assignerInt = aInt.Value;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&lt;span class="kwrd"&gt;catch&lt;/span&gt; (InvalidOperationException ex)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;  Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Exception of type &lt;/span&gt;&lt;/pre&gt;

  &lt;blockquote&gt;
    &lt;pre&gt;&lt;span class="str"&gt;       InvalidOperationException: {0}&amp;quot;&lt;/span&gt;, ex.Message);&lt;/pre&gt;
  &lt;/blockquote&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;&lt;span class="kwrd"&gt;catch&lt;/span&gt; (Exception ex)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;  Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Exception de type &lt;/span&gt;&lt;/pre&gt;

  &lt;blockquote&gt;
    &lt;pre&gt;&lt;span class="str"&gt;             System.Exception: {0}&amp;quot;&lt;/span&gt;, ex.Message);&lt;/pre&gt;
  &lt;/blockquote&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;            &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;&lt;span class="rem"&gt;//Using the HasValue property&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (aInt.HasValue)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;  Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Value: {0}&amp;quot;&lt;/span&gt;, aInt);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;&lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;  Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Value: null&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;&lt;span class="rem"&gt;//Using GetValueOrDefault&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Default value (with a parameter): &lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="str"&gt;              {0}&amp;quot;&lt;/span&gt;, aInt.GetValueOrDefault(-5));&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Default value (without a parameter): &lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="str"&gt;              {0}&amp;quot;&lt;/span&gt;, aInt.GetValueOrDefault());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  28:&lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;On line 4), we see how a Nullable can be declared. Like Generics, the Nullable is fully qualified with a type parameter specified between angle brackets. The initial value of aInt is null. When you try to retrieve a Nullable value having a null value, it throws an InvalidOperatorException. To prevent the exception from being thrown, it is best to test the Nullable with the HasValue property. The property returns false when it has null as a value. You can process the value according to it. Another feature of the Nullable is the GetValueOrDefault where it returns a default value when the Nullable is null. You have the choice to assign yourself the default value to be returned in case the Nullable is null, or you let the Framework return the default value (0, false, date.MinVale, and so on).&lt;/p&gt;

&lt;p&gt;For C# users, there are two things worth mentioning. First is the shortcut for declaring a Nullable. The other is the shorcut for the GetValueOrDefault method. Let&amp;#39;s have a look at the following code sample:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ShowNullableOperators()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  Nullable&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; a = 1;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;  &lt;span class="kwrd"&gt;int&lt;/span&gt;? b = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;  &lt;span class="kwrd"&gt;int&lt;/span&gt;? c = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;  c = a ?? 3;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;  Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Value of a: {0}; &lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="str"&gt;               Value of b: {1}, &lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="str"&gt;               Value of c with operator ??: {2}&amp;quot;&lt;/span&gt;, a, b, c);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;  a = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;  c = a ?? 3;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;  Console.WriteLine(&lt;span class="str"&gt;&amp;quot;Value of a: {0}; &lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="str"&gt;               Value of b: {1}, &lt;/span&gt;&lt;/pre&gt;

  &lt;blockquote&gt;
    &lt;pre&gt;&lt;span class="str"&gt;             Value of c with operator ??: {2}&amp;quot;&lt;/span&gt;, a, b, c);&lt;/pre&gt;
  &lt;/blockquote&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;  }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;On line 3), it is the usual Nullable declaration. On the next line, it is the shortcut for declaring a nullable. In this example, I have declare a Nullable of type int. I also assigned a null value to it. On the sixth line, it is the default value comparator. This operator first evaluate the left argument, in this case a, to determine if it has a null value. If it is not a null value, it returns the value. Otherwise, it returns the right value. &lt;/p&gt;

&lt;h4&gt;&lt;font color="#004080"&gt;Conclusion&lt;/font&gt;&lt;/h4&gt;

&lt;p&gt;The Nullable type is a convenient type to use when a real type may be assigned to a null value. The few properties available are significant and useful. The HasValue property gives the opportunity to the programmer to decide the logic to be applied in the case of a null value. If no logic is required, the GetValueOrDefault method is a good alternative. &lt;/p&gt;

&lt;p&gt;This post concludes the topics I covered four weeks ago in a lunch meeting. As you can see, there was a lot of stuff but it was a good study that covered some of the most important new features in .NET Framework 2.0.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

&lt;p&gt;Patrice&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:4e791747-59b4-4bea-8d90-a4a11d9b7c7f" style="padding-right:0px;display:inline;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Framework%202.0" rel="tag"&gt;Framework 2.0&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Generics" rel="tag"&gt;Generics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Nullables" rel="tag"&gt;Nullables&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160; &lt;/p&gt;

&lt;pre class="csharpcode"&gt;&amp;#160;&lt;/pre&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=699" width="1" height="1"&gt;</description><category domain="http://blogs.runatserver.com/ppare/archive/tags/ASP.NET+2.0/default.aspx">ASP.NET 2.0</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Generics/default.aspx">Generics</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Nullables/default.aspx">Nullables</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/.NET+Framework/default.aspx">.NET Framework</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Understanding And Working With Generics - Part III</title><link>http://blogs.runatserver.com/ppare/archive/2009/03/17/understanding-and-working-with-generics-part-iii.aspx</link><pubDate>Tue, 17 Mar 2009 04:00:46 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:682</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2009/03/17/understanding-and-working-with-generics-part-iii.aspx#comments</comments><description>&lt;p&gt;Sorry for taking so long to write the last part. I got a busy week last week with a few parties, a girlfriend birthday, an interview with a potential client &lt;a title="bewise" href="http://www.bewise.fr" target="_blank"&gt;bewise&lt;/a&gt; - which I will come over in a near future - and so on. &lt;/p&gt;  &lt;p&gt;The last post was on three fundamental considerations of the Generics: Generic constraints, generics types and methods and generic type inference. When I review the second part, I believe I went over the type constraints a little too quickly and I consider I should have spent more time discussing and implementing examples to demonstrate how to efficiently use these. I took notice of it and I should come back to it when time will allow me to do so. &lt;/p&gt;  &lt;p&gt;Today, we shall discuss of two things. How the JIT compiler manages the Generics and the Generic classes available in the .NET Framework.&lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;JIT Compiler&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;The main task of the Just In Time - JIT - Compiler is to translate the IL, the Intermediate Language, to the native language in order to be executed by the computer. &lt;/p&gt;  &lt;p&gt;The JIT Compiler produce different code for each type value but shares some native code regarding the generics for the reference types. It can do this because the compiler knows the length in memory of each reference, which is 32 or 64 bits depending on the architecture of the machine. An array of references will always be of the same size in the memory, whatever it is in the array, because the reference of the array is a pointer placed on the stack. Items in the array are however place on the heap and thus, dynamically created whenever needed and using the required memory they have to. &lt;/p&gt;  &lt;p&gt;There are performance issues when using arraylist that are annihilated when using Generics. For instance, when someone used an arraylist of Bytes in .NET 1.1, each item had to be boxed and then to get a hold on the reference of the boxed value. When using a Generic List&amp;lt;T&amp;gt; there is no boxing/unboxing effect. Also, each item of the list is, for instance, a Byte instead of an Object. The array is of the right type and in memory is the right size. &lt;/p&gt;  &lt;p&gt;This gives a better efficiency, saves memory, and is faster on runtime because there is no time lost on boxing or unboxing, there is no validation when unboxing and the Garbage Collector does not have to get involved when unboxed values are no more referenced.&lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Generic Classes&lt;/font&gt;&lt;/h4&gt;  &lt;h5&gt;&lt;font color="#0080c0"&gt;1. List&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/h5&gt;  &lt;p&gt;The Generic List&amp;lt;T&amp;gt; is the counterpart of the arraylist in the .NET 1.1. There are a few functionalities and caracteristics that existed in arraylist that were not implemented in the Generic List&amp;lt;T&amp;gt;. Among them, the Missing in Action methods are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Adaper&lt;/li&gt;    &lt;li&gt;Clone&lt;/li&gt;    &lt;li&gt;FixedSized&lt;/li&gt;    &lt;li&gt;Repeat&lt;/li&gt;    &lt;li&gt;SetRange&lt;/li&gt;    &lt;li&gt;Synchronized&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We can make an old API into a Generic List by implementing the IList and IList&amp;lt;T&amp;gt; interfaces.&lt;/p&gt;  &lt;p&gt;The new functionalities are:&lt;/p&gt;  &lt;p&gt;1. ConverAll: Converts each item of one type to another type and returns a list of the later. &lt;/p&gt;  &lt;p&gt;2. Exists: Checks if at least one element in the list meets the predicate.&lt;/p&gt;  &lt;p&gt;3. TrueForAll: Check if every element in the list meets the predicate&lt;/p&gt;  &lt;p&gt;4. FindFirts, FindLast, FindAll: Find an element in the list.&lt;/p&gt;  &lt;p&gt;5. RemoveAll: Remove all elements in the list that meets the predicate&lt;/p&gt;  &lt;p&gt;6. ForEach: It is not the ForEach iterative structure but this method allowes to perform an action on each item of the list.&lt;/p&gt;  &lt;h5&gt;&lt;font color="#0080c0"&gt;2. Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/font&gt;&lt;/h5&gt;  &lt;p&gt;The Generic Dictionary&amp;lt;,&amp;gt; is the counterpart of the HashTable in the .NET 1.1. Because in the .NET 2.0 the HashTable implements the IEqualityComparer interface, some functionalities available with the Dictionary&amp;lt;,&amp;gt; are also available with the HashTable. However, the most meaningful difference between the two is their behaviour when retrieving a value associated to a key. &lt;/p&gt;  &lt;p&gt;If we ask for a value with a key not present in the dictionary, the HashTable will return a null value. The dictionary&amp;lt;,&amp;gt; will throw a KeyNotFoundException. &lt;/p&gt;  &lt;p&gt;Both supports ContainKeys which retrieve the value associated to the key, if present, place it in the output parameter and returns true. Otherwise, it returns false and place the default value in the output parameter.&lt;/p&gt;  &lt;h5&gt;&lt;font color="#0080c0"&gt;3. Queue&amp;lt;T&amp;gt;, Stack&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/h5&gt;  &lt;p&gt;Both are lists that do not allow random access to their elements. Accessing items at random offers poor performances. However, when retrieving elements in the proper order, these list are quite effective. &lt;/p&gt;  &lt;p&gt;Queue&amp;lt;T&amp;gt; is a FIFO, First In First Out list. To add an item, we have to use the Enqueue method. When adding an object to a queue, the object is added at the end of the list. To retrieve an item, we have to use the Dequeue method and the first object in the list is removed from the list.&lt;/p&gt;  &lt;p&gt;To get the next object in the queue or the stack without removing it from the list, we can use the Peek method. &lt;/p&gt;  &lt;p&gt;Speaking of the stack, it is a FILO, First In Last Out (or a LIFO, Last In First Out depending on how you see your half filled (or is it empty) glass of water)) list. To add an item, we use the Push method which adds an item at the beginning of the list. To get (and remove) an item from the list, we use the Pop method. It retrieves the last object put in the list.&lt;/p&gt;  &lt;h5&gt;&lt;font color="#0080c0"&gt;4. SortedList&amp;lt;TKey, TValue&amp;gt;, SortedDictionary&amp;lt;TKey, TValue&amp;gt;&lt;/font&gt;&lt;/h5&gt;  &lt;p&gt;The sortedList allows access to its elements through an index. The sortedDictionary, however, is through a key. The most significant carateristic of both generic classes is that when enumerating items, they are sorted by their key. &lt;/p&gt;  &lt;p&gt;Another thing worth mentioning is when using the sortedlist, that list is more memory efficient in contrast to the sortedDictionary. The sortedDictionary is more faster when adding unsorted items. However, when items are sorted, the sortedlist is more faster than the sortedDictionary. &lt;/p&gt;  &lt;h5&gt;&lt;font color="#0080c0"&gt;5. LinkedList&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/h5&gt;  &lt;p&gt;This is a new list in .NET 2.0. What is it exactly? It is a chained list where each item is a node linked to previous node and linked to the next node. This list is quite efficient when you need to iterate from the beginning to the end or from the end to the beginning. However, random access is not efficient and is rather slow. &lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Conclusion&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;This was the third post out of three on Generics. In this third part of the sequel, we examined how the JIT Compiler worked when faced with Generic and where were the advantages of using the Generics. We also summarized the Generic classes available in the .NET Framework. We saw that each class had its own advantages and taking advantage of it is merely a question of which context is the most favourable.&amp;#160; &lt;/p&gt;  &lt;p&gt;We could write until tomorrow morning about the Generics. However, there is only one thing you must have in mind though. And it is the fact that they are quite more effective and convenient to use than their counterpart (arraylist or hashtable). &lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:fe1f82a6-8ec0-4044-b682-6f45dd761162" style="padding-right:0px;display:inline;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET%202.0" rel="tag"&gt;.NET 2.0&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Generics" rel="tag"&gt;Generics&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=682" width="1" height="1"&gt;</description><category domain="http://blogs.runatserver.com/ppare/archive/tags/ASP.NET+2.0/default.aspx">ASP.NET 2.0</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Generics/default.aspx">Generics</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Understanding And Working With Generics - Part II</title><link>http://blogs.runatserver.com/ppare/archive/2009/02/26/understanding-and-working-with-generics-part-ii.aspx</link><pubDate>Fri, 27 Feb 2009 04:38:38 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:590</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2009/02/26/understanding-and-working-with-generics-part-ii.aspx#comments</comments><description>&lt;p&gt;In the previous post, I covered different aspects of the .NET Framework, namely, the situation at the time of the .NET 1.1 and the challenges it offered to both the programmers and the compiler. I also named a few needs the .NET Framework 2.0 had to meet in order to correct the grievances expressed when using .NET 1.1. The solution offered, as we saw, was the Generic namespace. We quickly check the benefits someone gets from using the Generics and I finally reviewed a few vocabulary so we could discuss the Generics on the same basis.&lt;/p&gt;  &lt;p&gt;In this post, I will talk about the different Generics a programmer can use, the type constraints and type inference.&lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Generics&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;There are two kinds of Generics. There is the Generic type and the Generic Method.&lt;/p&gt;  &lt;h5&gt;&lt;font color="#8080c0"&gt;4 Generic Types&lt;/font&gt;&lt;/h5&gt;  &lt;p&gt;The Generic type is composed of 4 elements: classes, interfaces, delegates and structures.&lt;/p&gt;  &lt;p&gt;A Generic class is declared as followed:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/class.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="73" alt="Generic Class" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/class_thumb.png" width="208" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;Let&amp;#39;s consider the following class:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/class_1.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="152" alt="Implentation of a Generic class" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/class_1_thumb.png" width="391" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;When instantiating the class and calling the method ShowMyRealSelf,&amp;#160; we get the following results:&lt;/p&gt;  &lt;p&gt;Using a generic class:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/class_2.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="35" alt="Instantiating a generic class" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/class_2_thumb.png" width="307" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Results:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/class_3.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="27" alt="Results from a generic class" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/class_3_thumb.png" width="355" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;How does it work when you try using interface? Consider the following interface and class implementations :&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/interface.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="164" alt="Interface implementation" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/interface_thumb.png" width="386" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;and the instantiation of the class is as follow:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/interface_1.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="25" alt="Using a class implementing a generic inteface" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/interface_1_thumb.png" width="372" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;will produce this result:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/interface_2.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="18" alt="Results from using a class implementing a genericiInterface" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/interface_2_thumb.png" width="417" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The last sample is quite simple (save for the French class names...). The class MyGenericClass implements a custom interface ITransformation. These two expect a type as an input and will produce another type as an output. This is shown by how the method Transform uses the type parameters. In this example, I transform some juice to wine. With the same class, I could transform milk to butter or yogurt or cheese (or with divine help, water to wine - no offence meant).&lt;/p&gt;  &lt;h5&gt;&lt;font color="#8080c0"&gt;Generic Method&lt;/font&gt;&lt;/h5&gt;  &lt;p&gt;A generic class usually has generic methods. A generic method may return a generic type. it can also have generic type arguments. It must however define at least one type parameter. Here is the signature of the ConvertAll method from the generic class List.&lt;/p&gt;  &lt;p&gt;List&amp;lt;TOutput&amp;gt; ConvertAll&amp;lt;TOutput&amp;gt;(Converter&amp;lt;TInput, TOutput&amp;gt; conv).&lt;/p&gt;  &lt;p&gt;The ConvertAll method is quite useful for converting a list of one type to a list of another type. For example, someone could use this method to convert a list of candidates to a list of employees. Of course, this conversion implies somehow some logic to be applied to each item of the list to be converted. But first, let&amp;#39;s have a look at the method ConvertAll.&lt;/p&gt;  &lt;p&gt;Its return type is a List of TOutput. The TOuput type will be defined by the parameter type &amp;lt;TOutput&amp;gt;.&lt;/p&gt;  &lt;p&gt;It expects to get as an argument a Converter type variable, which is a generic delegate with two type parameters. The first type&lt;strong&gt; &lt;/strong&gt;parameter is the input type. The second, defined by the type parameter of the ConvertAll method, is the output type.&lt;/p&gt;  &lt;p&gt;Using the previous example of candidates and employees, lets suggests we do have two classes: a Candidate class and an Employee Class. Thus, the generic delegate should be declared as follow:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/delegate.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="16" alt="Generic delegate" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/delegate_thumb.png" width="421" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The logic to convert a Candidate to an Employee should be a usual method like this one:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/convert.png"&gt;&lt;/a&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/convert_1.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="62" alt="Method to convert" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/convert_thumb_1.png" width="410" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;Finally, the overall class of hiring a bunch of candidates:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/method_delegate.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="166" alt="Hiring a new employe" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/method_delegate_thumb.png" width="401" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Type Constraints&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;Type constraints on generics are useful when someone wants to control how his generic will be used. &lt;/p&gt;  &lt;p&gt;There are 4 type constraints:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Reference type constraint: a generic method or a generic type with a reference constraints means the type parameter must be a reference type (an instance of Employee for example). &lt;/li&gt;    &lt;li&gt;Value type constraint: the parameter type can only accept value types (int, bool, enum, ect). &lt;/li&gt;    &lt;li&gt;Constructor constraint: the type parameter must have a parameterless constructor. &lt;/li&gt;    &lt;li&gt;Derivation constraint: it means the type parameter must be or derived from the specified class. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The way a constraint must be declared is as follow:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/constraints.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="68" alt="Constructor constraint" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/constraints_thumb.png" width="360" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In the previous example, the generic method has a constructor constraint. It means an object of type T may be instantiated somewhere in the code. Since the type value may not be instantiated, a value type such like int, or bool is not allowed. &lt;/p&gt;  &lt;p&gt;Constraints can be combined for a more secure use of the generic. As you might expect it, the type value can only be used alone.&lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Type inference&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;Type inference demonstrates the power of the compiler. It shows how much the compiler is &amp;quot;intelligent&amp;quot; enough to infer the type parameter based on the type argument.&lt;/p&gt;  &lt;p&gt;Type parameter inference:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/inference.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="227" alt="Inference" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/UnderstandingAndWorkingWithGenericsPartI_10B6E/inference_thumb.png" width="363" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Usually, when using a generic method, specifying the type parameter is the standard. However, type inference allows the programmer to be a bit lazy and to omit the type parameter. &lt;/p&gt;  &lt;p&gt;Both ways are good programming practice. However, specifying type parameter is, in my own opinion, a better practice for the sake of code readability. &lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Conclusion&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;With this second part of three blogs on Generics, we have looked at the two different kind of generics someone may encounter, generic type and generic method. In the section relative to generic method, we reviewed how to use the ConvertAll method from the generic List class. Also, we reviewed the 4 constraints which allows the programmer to limit the way his generics may be used. Finally we examined the type inference and how it could simplify code writing and how it takes its token on readability.&lt;/p&gt;  &lt;p&gt;The last post will look at how the JIT manages memory with generics. Also, we will look at the 4 generic classes available.&lt;/p&gt;  &lt;p&gt;Don&amp;#39;t miss the last one!&lt;/p&gt;  &lt;p&gt;Patrice&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f1e59da6-c909-4b96-ada7-b251ec3621dc" style="padding-right:0px;display:inline;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Framework%202.0" rel="tag"&gt;Framework 2.0&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Generics" rel="tag"&gt;Generics&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=590" width="1" height="1"&gt;</description></item><item><title>Understanding And Working With Generics - Part I</title><link>http://blogs.runatserver.com/ppare/archive/2009/02/24/understanding-and-working-with-generics-part-i.aspx</link><pubDate>Wed, 25 Feb 2009 03:43:23 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:583</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2009/02/24/understanding-and-working-with-generics-part-i.aspx#comments</comments><description>&lt;p&gt;The Generic namespace is a new feature of the .NET Framework 2.0 and it is, in my own opinion, the most important innovation the Framework 2.0 has to offer. &lt;/p&gt;  &lt;h4&gt;&lt;font color="#8080ff"&gt;Overview of the situation from the .NET 1.1 &lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;Before the .NET Framework 2.0, programmers had to work with the ArrayList object in order to create arrays. The problem programmers experienced was the casting (or boxing) involved when using ArrayList. Adding instances of any object to an array involved, somewhere, a cast to an Object. When the time came to retrieve them, the programmer got an Object type and had to cast the Object to the right type. The last cast could lead to a runtime bug since the programmer had no way to know what type was in the ArrayList unless the code is properly documented. Also the compiler could not check the programmer&amp;#39;s code. It had to trust the programmer for the right casting. Otherwise a bug could be generated at runtime. &lt;/p&gt;  &lt;h4&gt;&lt;font color="#8080ff"&gt;Basic needs and requirements for the .NET 2.0&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;The needs and requirements the .NET Framework 2.0 had to meet were to cut down the number of casting (or unboxing) involved, allow a better validation at compile time, increase the Intellisense capabilities, decrease the number of bug at runtime due to bad castings and finally to enhance the readability of the code.&lt;/p&gt;  &lt;p&gt;The .NET Framework answered these challenges with the Generic namespace. &lt;/p&gt;  &lt;h4&gt;&lt;font color="#8080ff"&gt;Advantages of using Generics&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;When you start using the Generic, you get the following advantages compare to ArrayList. You get an implicit documentation of the code since the programmer knows the type of the objects in the array. The compiler knows what to expect and, therefore can perform validation at compile time. These checks increase the code robustness and decrease the possibilities of encountering a bug at runtime due to a bad casting. The reduction of the number of casting (and boxing) involved at runtime involve an augmentation of performances and a better memory management. When using Generics, the programmers reduce his code manipulation hence he increases his productivity. &lt;/p&gt;  &lt;h4&gt;&lt;font color="#8080ff"&gt;Vocabulary&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;Before we get our hand dirty, let&amp;#39;s check out a few terms commonly used with Generics.&lt;/p&gt;  &lt;p&gt;Boxing means to take a Value Type (int, bool, enum) and wrap it into an instance of a reference type of an object (ie. from a integer to an object).&lt;/p&gt;  &lt;p&gt;Unboxing means to unwrap the object and downcast it to a Value Type.&lt;/p&gt;  &lt;p&gt;Casting means to convert a reference type to another reference type. &lt;/p&gt;  &lt;p&gt;Implicit cast means there is a casting without using an conversion operator.&lt;/p&gt;  &lt;p&gt;Explicit cast means a casting using a conversion operator. &lt;/p&gt;  &lt;p&gt;Parameter type is what appears between angle brackets (&amp;lt;T&amp;gt;).&lt;/p&gt;  &lt;p&gt;Argument type is the real type used with generics.&lt;/p&gt;  &lt;p&gt;Unbound Generic type is a generic declaration without Argument Types. &lt;/p&gt;  &lt;p&gt;Opened Constructed Type is a generic with argument types where the argument types are defined from somewhere else (like a generic class with generic methods). &lt;/p&gt;  &lt;p&gt;Closed Constructed Type is a generic with argument types where the argument types are known. &lt;/p&gt;  &lt;h4&gt;&lt;font color="#8080ff"&gt;Conclusion&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;This concludes the first part of the Generics where we saw why the Generics are so welcomed with the .NET Framework 2.0. We also had a look at a few vocabulary terms which are commonly used when you get started working with Generics. &lt;/p&gt;  &lt;p&gt;The second part should look at the different generics someone may work with, the constraint types and type inference. This next part will also involve some code samples to get a better idea of the concept involved. &lt;/p&gt;  &lt;p&gt;See you soon,&lt;/p&gt;  &lt;p&gt;Patrice&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:0156d977-141f-4150-a585-af520e23fae4" style="padding-right:0px;display:inline;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Generics" rel="tag"&gt;Generics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Generic" rel="tag"&gt;Generic&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Framework%202.0" rel="tag"&gt;Framework 2.0&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=583" width="1" height="1"&gt;</description><category domain="http://blogs.runatserver.com/ppare/archive/tags/ASP.NET+2.0/default.aspx">ASP.NET 2.0</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Generics/default.aspx">Generics</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Team Talks on .NET</title><link>http://blogs.runatserver.com/ppare/archive/2009/02/23/team-talks-on-net.aspx</link><pubDate>Tue, 24 Feb 2009 04:15:41 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:578</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2009/02/23/team-talks-on-net.aspx#comments</comments><description>&lt;p&gt;Last Friday was my first presentation on a topic of my choice. The idea of such talks on lunch time to team members is from &lt;a title="Laurent Duveau&amp;#39; blog" href="http://blogs.runatserver.com/lduveau" target="_blank"&gt;Laurent Duveau&lt;/a&gt;. These talks are good on developing skills for speaking in front of a crowd and to learn something on a topic relative to .NET.&lt;/p&gt;  &lt;p&gt;The topic I chose is large. I decided to talk about Generics and Nullables. My experience with .NET Framework is somewhat short (definitely not as good as I would have liked). I already have worked with Generics for three years (since VS 2005 Beta) but never enough to talk about it with a new cutting edge so other team members could benefit from it. I used a pedagogic approach to prepare my presentation: an introduction to both subjects (What were the needs), a development body (classes, methods, advantages) and a conclusion (advantages and limitations).&lt;/p&gt;  &lt;p&gt;The challenge for me for this first presentation was to talk about&amp;#160; a topic on which I had a thin knowledge. In front of colleagues who are more knowledgeable on .NET Framework, the last thing I had in mind was to make a fool of myself or to talk only of general matter. That was for me a matter of self-esteem. &lt;/p&gt;  &lt;p&gt;So I invested a lot of time on getting my presentation ready. &lt;a title="Reference Book: C# in Depth" href="http://www.manning.com/skeet/" target="_blank"&gt;&amp;quot;C# in Depth&amp;quot;&lt;/a&gt; and &lt;a title="MSDN reference for Generics" href="http://msdn.microsoft.com/en-us/library/twcad0zb(VS.80).aspx" target="_blank"&gt;MSDN&lt;/a&gt; were my primary references. I also read a few blogs for complementary information. But the topic was deep. I should have let the Nullables for another talk. Next time I will talk about a technical topic, I will be more careful on selecting my subject. Thus, I could cut down my preparation time with less thing to cover, simpler demo to set up and fewer Powerpoint slides to get ready.&lt;/p&gt;  &lt;p&gt;The presentation went well. The team members (&lt;a title="Bruno Pelletier&amp;#39;s blog" href="http://blogs.runatserver.com/bpelletier" target="_blank"&gt;Bruno&lt;/a&gt;, &lt;a title="Vincent Hamelin&amp;#39;s blog" href="http://blogs.runatserver.com/vhamelin" target="_blank"&gt;Vincent&lt;/a&gt; and &lt;a title="Louis-Philippe Pinsonneault&amp;#39;s blog" href="http://blogs.runatserver.com/lppinson/" target="_blank"&gt;Louis-Philippe&lt;/a&gt;) actively participated to the talk by asking questions, having conversations on the current topics or by asking an example or requesting a demo. I was proud of my preparation because the conversation leaded by &lt;a title="Louis-Philippe Pinsonneault&amp;#39;s blog" href="http://blogs.runatserver.com/lppinson/" target="_blank"&gt;Louis-Philippe&lt;/a&gt; went over a few things I was about to talk about. &lt;/p&gt;  &lt;p&gt;This presentation gave me the opportunity to learn a few new things that I am now applying in my day to day developing tasks. Particularly, I use the Nullable type because the web application I am working on is laying on a database. Regarding the Generics, I learnt how to use the ConvertAll&amp;lt;T&amp;gt; method to convert a list of one type to a list of another type and I apply this method wherever I can. Also, I taught to a colleague how to use the Nullable and the ConverAll method and she is now using this new knowledge in her web application building.&lt;/p&gt;  &lt;p&gt;All in all, the experience I got on presenting a technical topic was incredible: I build up a powerpoint slideshow, a demo to show a few code samples and the content preparation. I also got benefits from the presentation by having a deeper understanding of the .NET Framework and this understanding will be a good kick start for my next Microsoft Certifcation, the MCTS 70-536. There is no doubt in my mind that with this presentation my study on Generics and Nullable for the MCTS 70-536 certification is completed.&lt;/p&gt;  &lt;p&gt;In this way, I must conclude that my first presentation was a complete success.&lt;/p&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=578" width="1" height="1"&gt;</description><category domain="http://blogs.runatserver.com/ppare/archive/tags/ASP.NET+2.0/default.aspx">ASP.NET 2.0</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Generics/default.aspx">Generics</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Using multiple sitemaps in a web site</title><link>http://blogs.runatserver.com/ppare/archive/2009/02/07/using-multiple-sitemaps-in-a-web-site.aspx</link><pubDate>Sat, 07 Feb 2009 16:38:49 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:470</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2009/02/07/using-multiple-sitemaps-in-a-web-site.aspx#comments</comments><description>&lt;p&gt;There are many ways to create a web site. However, there are two templates that are commonly used around the world. One is to use a single navigation tool such as a menu. Another one is to use two menus - a top menu beneath the web site banner and a left (or right depending on your tastes) menu.&lt;/p&gt;  &lt;p&gt; The first time I used a sitemap, I struggled a few hours to get it right. The problem was me. I did not understand the workings on sitemap providers. &lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Using a custom sitemap filename&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;My first mistake, that day, was to use a custom sitemap file name, like MyWeb.sitemap instead of web.sitemap. When I added the SiteMapDataSource with default settings to my MasterPage, nothing worked properly.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let&amp;#39;s consider the following layout in a MasterPage:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/start.png" target="_blank"&gt;&lt;/a&gt;&amp;#160;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/MasterPageLayout.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="238" alt="MasterPageLayout" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/MasterPageLayout_thumb.png" width="432" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As you can see on the screenshot, I added a sitemap file named MyWeb.sitemap. Here is the content&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/sitemapContent.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="105" alt="Content of the sitemap file" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/sitemapContent_thumb.png" width="434" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;When I do a View in Browser of with the default configurations in the web.config file, I get an error from IIS: &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/error1.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="142" alt="error1" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/error1_thumb.png" width="439" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt; This means the SiteMap provider is expecting a sitemap file named web.sitemap. &lt;/p&gt;  &lt;p&gt;So, if you rename the MyWeb.sitemap file to web.sitemap and then view in browser, you are in your right to expect to see something different from the previous screen. Actually, the page is shown and IIS did not throw the previous error:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/BrowserResults.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="230" alt="Browser results" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/BrowserResults_thumb.png" width="349" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;What is the trick? Or, more down to earth, how does it work? &lt;/p&gt;  &lt;p&gt;Opening the web.config file will not get you any information. In fact, the web.config file has nothing relating to anything close to a sitemap reference. To find your answers, you need to open another web.config file located in the following directory:&lt;/p&gt;  &lt;p&gt;%WINDIR%\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config&lt;/p&gt;  &lt;p&gt;&lt;font color="#ff0000"&gt;&lt;strong&gt;** WARNING: Unless you know what you are doing, if I were you I would not change anything in this file...&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Excerpt of the web.config file:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/DefaultSiteMapConfiguration.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="152" alt="DefaultSiteMapConfiguration" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/DefaultSiteMapConfiguration_thumb.png" width="392" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The SiteMapDataSource first reads in the web.config file inside the solution to find its provider. Finding none, IIS redirects the SiteMapDatSource to the web.config file of the framework on which the web site is running. &lt;/p&gt;  &lt;p&gt;While we have the %WINDIR%\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config file opened, we will copy the sitemap node into web.config file from the current web site project.&amp;#160; The code must go inside the system.web node. A view in browser demonstrates the default web page is still showing correctly.&lt;/p&gt;  &lt;p&gt;To be able to rename the web.sitemap file to something else, we need to change a few things in the new sitemap providers section from the current web.config.&lt;/p&gt;  &lt;p&gt;First, you need to change the siteMapFile attribute. This is where you specify to IIS your sitemap file name. Let&amp;#39;s change it for TopMenuWeb.sitemap. A quick glance in IE to see if the change has been taking into effect. It does. But what we get is another error:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/error2.png" target="_blank"&gt;&lt;/a&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/error2_1.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="131" alt="Multiple sitemap providers having the same name" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/error2_thumb_1.png" width="432" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;This error means you have at least two site map providers having the same name.&lt;/p&gt;  &lt;p&gt;After providing another name to the site map provider into our web.config file name, TopMenuWebSiteMapProvider for instance, we can see the results in IE. Everything works fine because we did not change the sitemap file yet. Doing so, IIS will throw a third error:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/error3.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="66" alt="SiteMapDataSource default settings causes an error" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/error3_thumb.png" width="421" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This error is caused by the SiteMapDataSource control in the masterpage. Earlier, we changed the sitemap provider in the web.config file. We also renamed the site map file. But all the while, the SiteMapDataSource control was always expecting the web.sitemap file. Since it can not find it anymore, IIS throws the above error.&lt;/p&gt;  &lt;p&gt;The solution is to provide to the SiteMapDataSource control the site map provider name that will be bound to the TopMenuWeb.sitemap xml. In the SiteMapDataSource, you add the SiteMapProvider property and fill it with the sitemap provider name that is mapped to the existing sitemap file in the project, currently the TopMenuWebSiteMapProvider. &lt;/p&gt;  &lt;p&gt;A refresh of IE shows the expected results.&lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Adding another sitemap&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;Adding as many sitemap files as desired is child play. The trick is to add as many sitemap providers in the web.config file.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/2SiteMapProviders.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="242" alt="Two site map providers in the web.config file" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/2SiteMapProviders_thumb.png" width="386" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt; And one more menu control bound to another siteMapDataSource control in the master page:&lt;/p&gt; &lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/SiteMapeDataSource1.png"&gt;&lt;/a&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/SiteMapeDataSource2.png" target="_blank"&gt;&lt;img height="203" alt="SiteMapeDataSource for the left menu" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/SiteMapeDataSource2_thumb.png" width="404" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;And finally, the leftMenuWeb.SiteMap file content:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/LeftMenuSiteMapContent.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="96" alt="Left menu site map content" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/LeftMenuSiteMapContent_thumb.png" width="432" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Before going to the browser, I added some style to the MasterPage for the sake of seeing the skeleton of the web site. It is pretty ugly, please be kind and don&amp;#39;t throw me any shoe...&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/BrowserResults2.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="335" alt="Browser results" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/Usingmultiplesitemapsinawebsite_E22/BrowserResults2_thumb.png" width="277" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Mainly, what is different from the previous browser results is that I changed an attribute in both SiteMapDataSource. I changed the ShowStartingNode value from true to false. This way, the menu shows not the root element but the first child level. &lt;/p&gt;  &lt;p&gt;I hope this will quickly get you started when you start designing your new web site layout. &lt;/p&gt;  &lt;p&gt;Best regards,&lt;/p&gt;  &lt;p&gt;Patrice&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:db61ba57-fdcf-4a65-84ef-cc9e3140e5cd" style="padding-right:0px;display:inline;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/ASP.NET%202.0" rel="tag"&gt;ASP.NET 2.0&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SiteMap%20providers" rel="tag"&gt;SiteMap providers&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SiteMapDataSource" rel="tag"&gt;SiteMapDataSource&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=470" width="1" height="1"&gt;</description><category domain="http://blogs.runatserver.com/ppare/archive/tags/SiteMapDataSource/default.aspx">SiteMapDataSource</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/SiteMap+Providers/default.aspx">SiteMap Providers</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/ASP.NET+2.0/default.aspx">ASP.NET 2.0</category></item><item><title>In-Depth look at the GridView control bound to an AccessDataSource</title><link>http://blogs.runatserver.com/ppare/archive/2009/01/22/in-depth-look-at-the-gridview-control-bound-to-an-accessdatasource.aspx</link><pubDate>Fri, 23 Jan 2009 04:55:58 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:442</guid><dc:creator>ppare</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2009/01/22/in-depth-look-at-the-gridview-control-bound-to-an-accessdatasource.aspx#comments</comments><description>&lt;p&gt;Recently, I worked on a small home project. This project objective was to manage of a condominium. Since the condominium holds a few numbers of co-owners and the data access weight would be small, I chose to build a database with Microsoft Access 2007. After inserting some data into the database, I soon realized I had to build a web site which would stand on the database for data persistency because sooner or later, I would like to manage, in a decentralized manner, the condominium and where I would do a minimum of support. Letting people with an Access database is not the best choice and developing Access forms is not what I consider a funny hobby. However, what I consider funny, though, is to build a web site and watch it live.&lt;/p&gt;  &lt;p&gt;So, I decided to build a web site with an Access database. To make the process even funnier, I also decided to use the GridView and the AccessDataSource controls. The objectives were to maximize my productivity, minimized the code writing and build something simple quickly. The GridView control is a great tool for data presentation in a tabular layout. It also allows to quickly visualize or edit data. The choice is ideal for expenses, revenues, suppliers and contracts data presentation.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Specifications&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;I chose to work with VS 2008 SP1, .NET 3.5 SP1, Microsoft Access 2007. The language I selected is C#.&lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Adding a database&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;After starting a new project in C#, I added my Microsoft Access 2007 database into the App_Data folder. I want to specify the AccessDataSource control does not work with a secured database with a username and a password because you can not specify a ConnectionString. The work around is to use a SqlDataSource control.&lt;/p&gt;  &lt;p&gt;Here is the schema of the Access database used in the first part of this blog entry:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/schema1.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="215" alt="Database schema - ownership history " src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/schema1_thumb.png" width="385" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Design et Databinding&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;After adding the database, I dragged and dropped on the Default.aspx form, from the toolbox, a GridView control and an AccessDataSource control.&lt;/p&gt;  &lt;p&gt;A new fonctionality of VS 2008 is to work with the designer and the ASP.NET code in the same window:&lt;/p&gt; &lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/vs2008.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="329" alt="VS 2008 new feature" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/vs2008_thumb.png" width="402" border="0" /&gt;&lt;/a&gt;   &lt;p&gt;Let&amp;#8217;s configure the AccessDataSource control with the designer. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/designer.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="88" alt="designer" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/designer_thumb.png" width="407" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;When choosing the Demo.ACCDB database in the App_Data folder, we get an error from the designer. It seems the designer cannot get the database schema:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/errorconnectionwizard.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="234" alt="Error from the connection wizard" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/errorconnectionwizard_thumb.png" width="402" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Since the database as a .ACCDB extension, you need to save the database into another format: .MDB. After renaming the database, there is no other action to undergo. The database is fully recognized as you can see in the Server Explorer window. You are ready to work with it.&lt;/p&gt;  &lt;p&gt;We can now configure the connection to the database with the AccessDataSource designer. To complete the design and databinding section, you need to associate the GridView DataSourceId property with the AccessDataSource ID property.&lt;/p&gt;  &lt;h4&gt;&lt;font color="#004080"&gt;Data presentation&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;The data presentation in a GridView using an AccessDataSource control is a child play. All you need to do is to create a SelectCommand inside the AccessDataSource control: &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image_2.png" target="_blank"&gt;&lt;img height="125" alt="Code: Select command" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image_thumb.png" width="462" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;When assigning the DataSourceId of the GridView with the AccessDataSource ID property using the designer, the wizard adds for you every column identified in the SelectCommand.&lt;/p&gt;  &lt;p&gt;What is left is to delete the OwnerID, which is the primary key in the Owners Table and to add the DataKeyNames property to the GridView. The property is useful to identify the fields inside the grid that are primary keys:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image7.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="43" alt="Code: DataKeyNames definition" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image7_thumb.png" width="224" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;If we had many primary keys, we would have assigned the DataKeyNames like this:&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;div class="csharpcode"&gt;     &lt;pre&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image_6.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="49" alt="Code: multiple dataKeyNames definition" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image_thumb_2.png" width="299" border="0" /&gt;&lt;/a&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;A quick save and followed with a View in Browser show the selected data in the grid:&lt;/p&gt;

&lt;p&gt;Excerpt of the GridView configuration:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image19.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="107" alt="Code: GridView bound columns" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image19_thumb.png" width="435" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Browser results :&lt;/p&gt;

&lt;div class="csharpcode"&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result1.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="91" alt="GridView results from the SelectCommand of the AccessDataSource control" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result1_thumb.png" width="366" border="0" /&gt;&lt;/a&gt; &lt;/div&gt;

&lt;div class="csharpcode"&gt;&amp;#160;&lt;/div&gt;

&lt;p&gt;Of course, the layout is entirely customizable through the CSS or through the designer. Also, webmasters of multi-language web sites may like the fact the headers can be regionalized. Finally, the data may be formatted: &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image24%5B1%5D.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="64" alt="Code: formatting data display" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image24%5B1%5D_thumb.png" width="375" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h4&gt;&lt;font color="#004080"&gt;Updating data&lt;/font&gt;&lt;/h4&gt;

&lt;p&gt;Updating data is one of the most important aspects in a data driven application and the GridView control is an ideal control that get the job done right. While developing the web site, this technical aspect offered me the most interesting challenges. The correctly configured AccessDataSource control allows to, without any code line, to update data: &lt;/p&gt;

&lt;p&gt;Excerpt of the GridView control:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image30.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="188" alt="Code: GridView preparation for updates" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image30_thumb.png" width="342" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;UpdateCommand from the AccessDataSource control:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image36.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="37" alt="Code: UpdateCommand from the AccessDataSource" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image36_thumb.png" width="358" border="0" /&gt;&lt;/a&gt;  &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Why does the update works without any code line? It works because the GridView control adds the parameter values to the parameter collection of the UpdateCommand. It first passes the parameter values from the BoundField objects and then it passes the values from the DataKeyNames property. &lt;/p&gt;

&lt;p&gt;Adding a field in the collection of columns without adding it to the property definition of the UpdateCommand of the AccessDataSource controls breaks the order into which the fields are added to the collection. This action is responsible for breaking the UpdateCommand from working correctly and what you get is a GridView no more updating the data. We can conclude the order into which the parameter values are added to the collection is important to the UpdateCommand and it confirms the next statement from MSDN:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&amp;#8220;&amp;#8230; any parameterized SQL queries that you specify in the SelectCommand, UpdateCommand, InsertCommand, and DeleteCommand properties must match the order of any Parameter objects that are in the corresponding parameter collection. If no parameter collection is specified on the &lt;b&gt;AccessDataSource&lt;/b&gt;, the order of parameters that are supplied to these operations must match the order in which they appear in the underlying SQL statement.&amp;#8221; &lt;a href="http://msdn.microsoft.com/en-ca/library/system.web.ui.webcontrols.accessdatasource.aspx" target="_blank"&gt;Ref: MSDN&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Excerpt of the GridView configuration:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt; &lt;/span&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image44.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="199" alt="Code: Modifying the columns collection to prevent the updateCommand from working properly" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image44_thumb.png" width="340" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Excerpt of the AccessDataSource control:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image47.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="41" alt="Code: UpdateCommand without the additional column as a parameter" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image47_thumb.png" width="401" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And the fact that we specify the parameters collection inside the AccessDataSource control does not resolve the problem at hand :&lt;/p&gt;

&lt;p&gt;Excerpt of the AccessDataSource control:&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image50.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="72" alt="Code: defining the UpdateParameters does not solve the update issue" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image50_thumb.png" width="420" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The solution to the problem is to add a property to the undesired boundfield to prevent the gridview to add it to the collection. adding the readonly property and setting it to true provides the solution:&lt;/p&gt;

&lt;p&gt;excerpt of the accessdatasource control:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;&lt;a&gt;&lt;/a&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/code001.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="68" alt="Code: How to solve the issue" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/code001_thumb.png" width="303" border="0" /&gt;&lt;/a&gt;  &lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When you decide to use the templatefield, its presence does not have any impact on how the gridview control adds its parameters to the updatecommand. the accessdatasource still works perfectly:&lt;/p&gt;

&lt;p&gt;excerpt of the gridview configuration:&lt;/p&gt;

&lt;div class="csharpcode"&gt;&lt;a&gt;&lt;/a&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/code002.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="179" alt="Code: customizing the display in edit mode with a TemplateField" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/code002_thumb.png" width="312" border="0" /&gt;&lt;/a&gt;&amp;#160;&amp;#160; &lt;span class="kwrd"&gt;
    &lt;h4&gt;&lt;font color="#004080"&gt;Cascading DropDownLists&lt;/font&gt;&lt;/h4&gt;

    &lt;p&gt;When we go to the edition mode, we open the door to bad data input from the users. We can limit the mistakes by using the dropDownLists whenever it is possible. In the following example, I will use three tables to show expenses data: the Expenses table, the Suppliers table and the Contracts table:&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/schema2.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="272" alt="Database schema of expenses" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/schema2_thumb.png" width="421" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;Here is the first declarative part of the GridView control :&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image11.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="72" alt="Code: new GridView" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image11_thumb.png" width="284" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;And here is the columns that are included into the GridView control :&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image14.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="248" alt="Code: fields to be displayed" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image14_thumb.png" width="322" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;And finally the SelectCommand of the AccessDataSource : &lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image17.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="160" alt="Code: SelectCommand from the new AccessDataSource" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image17_thumb.png" width="325" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;The result in the browser:&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result2.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="207" alt="First view from the Expenses table" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result2_thumb.png" width="426" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;That said, we want to edit all values from the above Grid. The database schema of the Expenses shows the Expenses table contains foreign keys pointing toward the Supplier table and the Contract table. Those foreign keys are systemic and meaningless to the users. How do we prevent input mistakes ? &lt;/p&gt;

    &lt;p&gt;All we have to do is to give to the user restricted meaningful values to choose from, link them to systemic values and bind them to the table. Let&amp;#8217;s see how we do it&amp;#8230;&lt;/p&gt;

    &lt;p&gt;Inside the columns collection, we first add a CommandField column the property ShowEditButton. This quickly add all necessary buttons to select and update data:&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image27.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="19" alt="Code: enabling the GridView to update its content" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image27_thumb.png" width="315" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;Let&amp;#8217;s start with the suppliers. We replace the BoundField SupplierName by a TemplateField:&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image32.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="226" alt="Code: adding a dropDownList in a TemplateField and binding its selected value" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image32_thumb.png" width="374" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;And we add another AccessDataSource to bind it to the dropDownList :&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image35%5B1%5D.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="84" alt="image" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image35%5B1%5D_thumb.png" width="325" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;From this example, I need to clear out a few things. The AccessDataSource2 control is placed outside the GridView control, somewhere on the page. Also, as we can see, the SelectedValue property of the DropDownList uses the Bind function and receives as a parameter the field SupplierID. This field is provided by the AccessDataSource1 control, which is used to fill the grid. This means we need to add the field to the SelectCommand of the AccessDataSource1:&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image43.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="160" alt="Code: SelectCommand from the AccessDataSource1 used for the SelectedValue in the dropDownList" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image43_thumb.png" width="309" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;&amp;#160;&lt;/p&gt;

    &lt;p&gt;Here is the result:&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result3.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="226" alt="result3" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result3_thumb.png" width="427" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;We do the same process form the ContractNo column. We add a TemplateField, a dropDownList and a third AccessDataSource control:&lt;/p&gt;

    &lt;p&gt;Here is the excerpt of the TemplateField :&lt;/p&gt;

    &lt;div class="csharpcode"&gt;
      &lt;pre&gt;&lt;pre&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image115.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="204" alt="image" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image115_thumb.png" width="388" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;&lt;/pre&gt;
    &lt;/div&gt;

    &lt;p&gt;And the third AccessDataSource, dropped somewhere on the page, outside the GridView: &lt;/p&gt;

    &lt;div class="csharpcode"&gt;
      &lt;pre&gt;&lt;pre&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image120.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="76" alt="image" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image120_thumb.png" width="447" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;&lt;/pre&gt;
    &lt;/div&gt;

    &lt;p&gt;And the results are as follow:&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result4.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="220" alt="result4" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result4_thumb.png" width="425" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;The problem of data input of a contract number is half solved. We allow users to choose from a meaningful contract numbers lists. Some, however, are not related to the selected supplier though. We solve this problem by, first, moving the third AccessDataSource, AccessDataSource3, into theGridView, inside the contract TemplateField, right beneth the contract dropDownList:&lt;/p&gt;

    &lt;p&gt;Contract TemplateField Excerpt:&lt;/p&gt;

    &lt;div class="csharpcode"&gt;
      &lt;pre&gt;&lt;span class="lnum"&gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="kwrd"&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image125.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="250" alt="image" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image125_thumb.png" width="416" border="0" /&gt;&lt;/a&gt; &lt;/span&gt;&lt;/pre&gt;
    &lt;/div&gt;

    &lt;p&gt;After that, we add a filter into the AccessDataSource control:&lt;/p&gt;

    &lt;div class="csharpcode"&gt;
      &lt;pre&gt;&lt;pre&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image131.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="159" alt="image" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image131_thumb.png" width="386" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;&lt;/pre&gt;
    &lt;/div&gt;

    &lt;p&gt;The filter is an expression alike a where clause in an Access query (in this case since we use AccessDataSource). The filter applies to explicitly specified fields and we add it a parameter value. The filter parameters are defined by a FilterParameters collection. Each filter is associated to a control inside the GridView and to one of its property that contains the value to be retrieved in order to apply the filter. In our case, the filter expression is applied on the field SupplierID with the help of the selected value of the suppliers DropDownList. &lt;/p&gt;

    &lt;p&gt;Finally, before proceeding to results, we have to remove the property SelectedValue from the contract dropDownList. If not, an exception is raised: &lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/error1.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="43" alt="error1" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/error1_thumb.png" width="409" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;However, when showing the page in the browser, we get another exception:&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/erro21.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="75" alt="erro21" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/erro21_thumb.png" width="412" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;We need to add to the update parameters collection in order to specify the parameters : &lt;/p&gt;

    &lt;div class="csharpcode"&gt;
      &lt;pre&gt;&lt;pre&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image136.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="99" alt="image" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image136_thumb.png" width="379" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;&lt;/pre&gt;
    &lt;/div&gt;

    &lt;p&gt;When we view the page in the web browser and we test the update capabilities of the grid, we quickly realize that something is wrong. The data seems to disappear from the grid and a quick glance at the data in the database confirms it:&lt;/p&gt;

    &lt;p&gt;Before the updates :&lt;/p&gt;

    &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result5.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="148" alt="result5" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result5_thumb.png" width="453" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

    &lt;p&gt;After 3 updates : &lt;/p&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result6.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="149" alt="result6" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result6_thumb.png" width="456" border="0" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;span class="kwrd"&gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;p&gt;It seems the GridView can no more make the association between some controls and the UpdateCommand from the AccessDataSource. We also take notice that the SupplierID column accepts the new values selected by the suppliers dropDownList but the selected values associated to the contracts dropDownList are discarded.&lt;/p&gt;

    &lt;p&gt;To correct this, you need to associate the lost value to the UpdateCommand from :&lt;/p&gt;

    &lt;p&gt;First, the aspx page, by associating the OnRowUpdating command to a function name used in the code behind:&lt;/p&gt;

    &lt;div class="csharpcode"&gt;
      &lt;pre&gt;&lt;span class="lnum"&gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;pre&gt;&lt;span class="kwrd"&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image140.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="82" alt="image" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image140_thumb.png" width="307" border="0" /&gt;&lt;/a&gt; &lt;/span&gt;&lt;/pre&gt;
    &lt;/div&gt;

    &lt;p&gt;And then, the code behind:&lt;/p&gt;

    &lt;pre class="csharpcode"&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image145.png" target="_blank"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="195" alt="image" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/image145_thumb.png" width="473" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;

    &lt;p&gt;Now, let&amp;#8217;s see the results:&lt;/p&gt;

    &lt;p&gt;&amp;#160;&lt;/p&gt;
    &lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result7.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="256" alt="result7" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result7_thumb.png" width="484" border="0" /&gt;&lt;/a&gt; 

    &lt;p&gt;&lt;/p&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result8.png" target="_blank"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="322" alt="result8" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/InDepthlookattheGridViewcontrolboundtoan_14011/result8_thumb.png" width="484" border="0" /&gt;&lt;/a&gt; &lt;/span&gt;&lt;/pre&gt;

    &lt;h4&gt;&lt;font color="#004080"&gt;Inserting data through the GridView&lt;/font&gt;&lt;/h4&gt;

    &lt;p&gt;The gridView is not the tool to use for inserting data into a database. If you are using an AccessDataSource control, it is highly recommended to use a FormView control or a DetailsView control. &lt;/p&gt;

    &lt;p&gt;However there is a way to make it happen and it is to use the FooterRow. For more information, please read the following: &lt;a href="http://scottonwriting.net/sowblog/posts/11904.aspx" target="_blank"&gt;how to use FooterRow to insert data&lt;/a&gt;. As I mentioned, beware, this is not something I recommend. I rather mention it as a way some have explored.&lt;/p&gt;

    &lt;h4&gt;&lt;font color="#004080"&gt;Conclusion&lt;/font&gt;&lt;/h4&gt;

    &lt;p&gt;Finally, the gridView is a great tool for all purpose applications where you need a tabular layout for your data and apply your own CSS. Associated to a data source, such like the AccessDataSource, your productivity will no doubt increased. Correctly configured, the AccessDataSource requires the minimum of code line.&lt;/p&gt;

    &lt;p&gt;Although everything looks simple, the architecture of such application using a data souce like the AccessDataSource control is quite simple and rarely meets the n-tier architecture layers we often see in most serious business applications. &lt;/p&gt;

    &lt;p&gt;Before I wrote the condominium management web application, I never used AccessDataSource or SqlDataSource. I rather liked to write my own layers: a data access layer and a business layer. But I must confess, the AccessDataSource offers all the tools a developer requires to build a home web application.&lt;/p&gt;

    &lt;p&gt;Patrice&lt;/p&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:b2c3c414-492e-4334-af1c-afa4c027df01" style="padding-right:0px;display:inline;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/GridView" rel="tag"&gt;GridView&lt;/a&gt;,&lt;a href="http://technorati.com/tags/AccessDataSource" rel="tag"&gt;AccessDataSource&lt;/a&gt;&lt;/div&gt;
  &lt;/span&gt;

  &lt;p&gt;&lt;/p&gt;
&lt;/div&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=442" width="1" height="1"&gt;</description><category domain="http://blogs.runatserver.com/ppare/archive/tags/AccessDataSource/default.aspx">AccessDataSource</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/GridView/default.aspx">GridView</category></item><item><title>DevTeach 2008 Review</title><link>http://blogs.runatserver.com/ppare/archive/2008/12/16/devteach-2008-review.aspx</link><pubDate>Wed, 17 Dec 2008 04:01:34 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:415</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2008/12/16/devteach-2008-review.aspx#comments</comments><description>&lt;p&gt;Two weeks ago was the DevTeach event held in Montreal. The conference received more than 300 attendees and over sixty speakers. There were more than 230 sessions given in three days including special events. The topics covered were vast and diversified ranging from Agile, Web Development, .NET, Silverlight, Ajax, SQL and Architecture. For me it was the second time I attended a DevTeach Conference. The first time I was there as a &lt;a href="http://www.utcoverage.com/DevTeach/2006/"&gt;Universal Thread reporter&lt;/a&gt;. The second time I was there as a simple attendee. Both experiences were great but the second time offered me the opportunity to enjoy it every second. &lt;/p&gt;  &lt;p&gt;I attended many sessions on Silverlight. When no Silverligth session caught my attention, I gave in to other sessions where their topic touched as many aspects of .NET as possible. Of course, my choice scope remained within my professional interests. For the DevTeach review, I will present my Top 3 best sessions I attended. &lt;/p&gt;  &lt;h5&gt;Top 3 &amp;#8211; Position #1 : Laurent Duveau &amp;#8211;&lt;/h5&gt;  &lt;p&gt;The session of Laurent Duveau represented exactly what I was expecting form the DevTeach Conference regarding SilverLight 2.0. What I mean is someone taking a concise topic and drilling it down to its core. And this is exactly what did Laurent Duveau. He targeted a topic, Building a business application SilverLight, and showed how to build a simple data driven Silverlight application. For all details on his presentation, please follow the &lt;a href="http://weblogs.asp.net/lduveau/archive/2008/12/05/my-silverlight-session-at-devteach-montreal.aspx%20" target="_blank"&gt;link&lt;/a&gt;.&lt;/p&gt;  &lt;h5&gt;Top 3 &amp;#8211; Position #2 : Beth Massi &amp;#8211; &lt;/h5&gt;  &lt;p&gt;Beth Massi (&lt;a href="http://blogs.msdn.com/bethmassi/" target="_blank"&gt;Beth Massi&amp;#39;s blog&lt;/a&gt;) is a VB expert&amp;#8230; humm the best expression would be she is the Queen of VB, but this is only my opinion. I went to her session because friends of mine, used to DevTeach, warmly and highly recommanded it. Her session was on LINQ and XML with Office 2007. Two things hit me to the point I was at a certain point speechless, if not breathless. The first thing was to see how easy it was to write XML literals with embedded expressions (&lt;a href="http://msdn.microsoft.com/en-us/library/ms364068(VS.80).aspx" target="_blank"&gt;Lets see the VB 9.0 neat new features&lt;/a&gt;). The other thing is to edit Office 2007 documents (Word &amp;#8211; docx, Excel &amp;#8211; xlsx, PowerPoint &amp;#8211; pptx). For more information on how she did it, please read Beth Massi&amp;#8217;s blog and &lt;a href="http://blogs.msdn.com/bethmassi/archive/2008/12/08/back-from-devteach-montreal.aspx" target="_blank"&gt;download her solution&lt;/a&gt;.&lt;/p&gt;  &lt;h5&gt;Top 3 &amp;#8211; Position #3 : Jim Duffy &amp;#8211; &lt;/h5&gt;  &lt;p&gt;Jim Duffy&amp;#8217;s session (&lt;a href="http://geekswithblogs.net/takenote/Default.aspx" target="_blank"&gt;Jim Duffy&amp;#39;s blog&lt;/a&gt;) was the last session on the last day and it was a sweet conclusion to the 2008 edition of DevTeach. I really like Jim Duffy. He was funny, expressive, and a good teacher. Regarding his session, it was flawless because it showed an aspect of ASP.NET development and it examined it thoroughly . Jim Duffy showed how to implement Microsoft Visual Earth into ASP.NET application. To get a good idea on how to do this, please look the video on the next &lt;a href="http://www.bestechvideos.com/2008/07/12/dnrtv-show-104-jim-duffy-on-implementing-virtual-earth-in-asp-net" target="_blank"&gt;link&lt;/a&gt;. His source code of his demo can be downloaded &lt;a href="http://www.takenote.com/DesktopDefault.aspx" target="_blank"&gt;here&lt;/a&gt;. But make it hurry, the link may disappear anytime soon. Also, open the next link to download all necessary API to build Live applications: &lt;a href="http://dev.live.com/" target="_blank"&gt;dev.live.com&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Finally, DevTeach 2008 was a great event. What best describes DevTeach is the speakers and the many topics covered in a wild variety of ways. This year, although the subscription cost more than $1200 CAN the attendees received close to a $1000 in softwares : Visual Studio 2008 Pro, Expression Web 2.0 and the TechEd 2008 DVD set.&lt;/p&gt;  &lt;p&gt;Hope to see you there next year!&lt;/p&gt;  &lt;p&gt;Patrice&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ff222444-1f4f-4538-8bb2-95c878cdede8" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/Visual%20Earth" rel="tag"&gt;Visual Earth&lt;/a&gt;,&lt;a href="http://technorati.com/tags/DevTeach%202008" rel="tag"&gt;DevTeach 2008&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Live%20Services" rel="tag"&gt;Live Services&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=415" width="1" height="1"&gt;</description><category domain="http://blogs.runatserver.com/ppare/archive/tags/Visual+Earth/default.aspx">Visual Earth</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/DevTeach/default.aspx">DevTeach</category><category domain="http://blogs.runatserver.com/ppare/archive/tags/Live+Services/default.aspx">Live Services</category></item><item><title>TechDays 2008 review</title><link>http://blogs.runatserver.com/ppare/archive/2008/12/08/techdays-2008-review.aspx</link><pubDate>Tue, 09 Dec 2008 04:58:21 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:413</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2008/12/08/techdays-2008-review.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/TechDays2008review_1505B/techdays-2008_2.jpg"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;margin:0px 20px 0px 0px;border-right-width:0px;" height="100" alt="techdays-2008" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/TechDays2008review_1505B/techdays-2008_thumb.jpg" width="224" align="left" border="0" /&gt;&lt;/a&gt;Last November 6 and 7, I went to the TechDays 2008, a sponsored conference by Microsoft held on two days at the Palais des Congr&amp;#232;s de Montr&amp;#233;al. A month after the event, I would like to talk about two sessions I attended that particularly held my attention. &lt;/p&gt;  &lt;p&gt;The conference was an opportunity for me to have a first contact with the Silverlight technology. A week before the event, I started reading Scott Guthrie&amp;#8217;s blog (&lt;a href="http://weblogs.asp.net/scottgu/"&gt;http://weblogs.asp.net/scottgu/&lt;/a&gt;) on how to get started with Silverlight, but for many bad reasons (I know don&amp;#8217;t tell me) I did not persevere.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;img style="margin:0px 10px 5px 0px;" height="90" alt="Qixing Zheng" src="http://www.canitpro.ca/cdndevs/blog/qixing_small.jpg" width="90" align="left" border="1" /&gt;&lt;/p&gt;  &lt;p&gt;I attended Zheng XiQing (&lt;a href="http://blogs.msdn.com/canux/"&gt;http://blogs.msdn.com/canux/&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/canux/archive/2007/03/01/microsoft-canada-s-first-user-experience-advisor-qixing-zheng.aspx"&gt;http://blogs.msdn.com/canux/archive/2007/03/01/microsoft-canada-s-first-user-experience-advisor-qixing-zheng.aspx&lt;/a&gt; ) and Laurent Duveau (&lt;a href="http://weblogs.asp.net/lduveau/"&gt;http://weblogs.asp.net/lduveau/&lt;/a&gt; ) sessions on Silverlight. Zheng XiQing is a First User Experience Developer Advisor. Her session showed the limitless possibilities that Silverlight has to offer at the presentation layer and the customization and personalization of the GUI. I saw with her that the sky is the limit with Silverlight. Well, to get there, you will need a good share of creativity and a deep understanding of Microsoft Expression Blend.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;img style="margin:0px 10px 5px 0px;" alt="" src="http://3v9ipw.blu.livefilestore.com/y1pXgZarJ7KOK1qPHMYHe2rXFmtflvRXu6GSACBWs4UKMh4JpdJGzVAd15o8pL1PVGSOKKerl9kn-Y/Laurent-Duveau_120-60.jpg" align="left" border="0" /&gt;The other session that caught my attention was the two part sessions given by Laurent Duveau. Laurent has demonstrated that with a few Silverlight controls and the use of Blend someone can get running a Silverlight application with video feeds. The lesson I got from watching Laurent is that Silverlight is not as complicated as I first understood it. The trick is to work with Blend. Laurent also showed another tool that is worth mentioning: Deep Zoom composer. I highly recommend it because the tool renders large images in small tiles. And when you zoom in, you get another set of tiles that represents the size of your screen. For a demo on how the tool is used, please follow the link: &lt;a href="http://memorabilia.hardrock.com"&gt;http://memorabilia.hardrock.com&lt;/a&gt;. Please have a close look on the stamp on the envelope addressed to Sgt Buddy Bresner. &lt;/p&gt;  &lt;p&gt;Finally, the event was well organized and the attendees were so well taken care of by Microsoft. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Patrice&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:7c98f113-146b-4173-8879-16f8ca3bc1ee" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Mots clés Technorati : &lt;a href="http://technorati.com/tags/Silverlight" rel="tag"&gt;Silverlight&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TechDays" rel="tag"&gt;TechDays&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Deep%20zoom" rel="tag"&gt;Deep zoom&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=413" width="1" height="1"&gt;</description></item><item><title>Welcome Word</title><link>http://blogs.runatserver.com/ppare/archive/2008/12/07/welcome-word.aspx</link><pubDate>Sun, 07 Dec 2008 15:37:33 GMT</pubDate><guid isPermaLink="false">ea6c9293-c621-4a68-aeb8-5da5e5cb41f8:411</guid><dc:creator>ppare</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.runatserver.com/ppare/archive/2008/12/07/welcome-word.aspx#comments</comments><description>&lt;p&gt;Welcome .NET community to this new blog!&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;As a welcome word, I will present your new blogger and try to limit the different horizons this blog will cover here. &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;Let&amp;#8217;s start with the blogger. My name is Patrice Par&amp;#233; and I live in vicinity of Montreal, Canada. I consider myself an intermediate level web developer with three years of experience with the .NET technologies. I have been working for RunAtServer Consulting Inc. for the last 6 months.&amp;#160; In October, I got the MCPD Web Application certification.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/WelcomeWord_926C/MCP(rgb)_2.gif"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="84" alt="MCP(rgb)" src="http://blogs.runatserver.com/blogs/ppare/WindowsLiveWriter/WelcomeWord_926C/MCP(rgb)_thumb.gif" width="164" border="0" /&gt;&lt;/a&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;As an intermediate developer, I intend this blog will address a public of junior experience in .NET technologies. My hopes are this blog will cast some light on .NET technologies and how to use it. Of course, any of you are welcome to read the blog and leave a comment.&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;Finally, when I speak of .NET technologies I think at ASP.NET, Ajax and Silverlight. In fact, we will look at the development of Rich Internet Application in a Windows context. The web development will be done using Visual Studio 2008 which means I will also talk about the .NET Framework 3.5 and 2.0. Of course, any self-respectful development is intended to be somehow useful which means it as to be deployed. My staging environment will be a Windows environment with IIS running the web apps. &lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;I hope you will enjoy reading my posts&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;Best regards,&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;Patrice &lt;/p&gt;&lt;img src="http://blogs.runatserver.com/aggbug.aspx?PostID=411" width="1" height="1"&gt;</description></item></channel></rss>