Error 0x80020009

I am sure you all know the Best Practices: Using Disposable Windows SharePoint Services Objects Guide from Microsoft. If not, read it 🙂

Please be carefull what objects you dispose after you don’t need them anymore. If you dispose the SPContext.Current.Site object, you might get strange behaviours. e.g. you might not be able to save Webpart properties. Doing so, will generate an error:

Cannot save the property settings for this Web Part. Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))

   1: SPSite site = SPContext.Current.Site;
<pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, 'Courier New', courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;border-bottom-style:none"><span style="color:#606060">   2:</span> <span style="color:#008000">// do something with it</span></pre>

<pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, 'Courier New', courier, monospace;border-right-style:none;border-left-style:none;background-color:white;border-bottom-style:none"><span style="color:#606060">   3:</span> site.Dispose();</pre>

This code fragment will actually dispose the SPContext.Current.Site object!

   1: SPSite site = new SPSite(SPContext.Current.Site.ID);
<pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, 'Courier New', courier, monospace;border-right-style:none;border-left-style:none;background-color:#f4f4f4;border-bottom-style:none"><span style="color:#606060">   2:</span> <span style="color:#008000">// do something</span></pre>

<pre style="padding-right:0px;padding-left:0px;font-size:8pt;padding-bottom:0px;margin:0em;overflow:visible;width:100%;color:black;border-top-style:none;line-height:12pt;padding-top:0px;font-family:consolas, 'Courier New', courier, monospace;border-right-style:none;border-left-style:none;background-color:white;border-bottom-style:none"><span style="color:#606060">   3:</span> site.Dispose();</pre>

The above sample will work!