title: Removing web part from page programatically - problem with usings
body: <p>I've tried to do that in four different ways:</p>
  
  <p>Like this:</p>
  
  <pre><code>try
  {
      SPSite site = SPContext.Current.Site;
      SPWeb web = site.OpenWeb();
  }
  catch (Exception ex) {}
  </code></pre>
  
  <p>And like this:</p>
  
  <pre><code>try
  {
      using (SPSite site = SPContext.Current.Site) {
         using (SPWeb web = site.OpenWeb()) {
         }
       }
  }
  catch (Exception ex) {}
  </code></pre>
  
  <p>And in other 2 combination with/without usings and other 2 with</p>
  
  <pre><code>SPWeb web = SPContext.Current.Web;
  </code></pre>
  
  <p>Every of them give me one of two errors:</p>
  
  <p>"Trying to use an SPWeb object that has been closed or disposed and is no longer valid."</p>
  
  <p>or</p>
  
  <p>"The operation could not be completed because the Web Part is not on this page.".</p>
  
  <p>Any ideas how to do that properly?</p>