title: Running out of connections in pool
body: <p>I have a web forms app that will display a list of records in a GridView and by checking multiple checkboxes you can mass delete the records. The code itself is straightforward:</p>
  
  <pre><code>protected void btnDelete_Click(object sender, EventArgs e)
  {
      int i = 0;
      try
      {
          foreach (GridViewRow row in GridView1.Rows)
          {
              CheckBox cb = (CheckBox)row.FindControl("ID");
              if (cb != null &amp;&amp; cb.Checked)
              {
                  int profileID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                  Profile profile = new Profile(profileID); //instantiate profile
                  profile.Delete(); //call delete method
                  i++;
              }
          }
          if (i &gt; 0)
          {
              //report success to UI
          }
      }
      catch (Exception ex)
      {
          //report error to UI
      }
  }
  </code></pre>
  
  <p>In the profile constructor, it hydrates the object by opening up a connection, opening a datareader and then setting the properties of the object. I am meticulous about <code>using()</code> blocks in my code so every db connection looks about like this:</p>
  
  <pre><code>using (SQLHelper db = new SQLHelper())
  {
      db.AddParameterToSQLCommand("@ProfileID", SqlDbType.Int);
      db.SetSQLCommandParameterValue("@ProfileID", id);
  
      using (SqlDataReader dr = db.GetReaderByCmd("up_GetProfile"))
      {
          if (dr.Read())
          {
              _profileID = id;
              if (!dr.IsDBNull(0))
                  ProfileName = dr.GetString(0);
              //... and so on
              return true;
          }
          else
          {
              return false;
          }
      }
  }
  </code></pre>
  
  <p>A datareader implements iDisposible, as does my helper class, and the destructor looks like this:</p>
  
  <pre><code>public void Dispose()
  {
      try
      {
          //Clean Up Connection Object
          if (mobj_SqlConnection != null)
          {
              if (mobj_SqlConnection.State != ConnectionState.Closed)
              {
                  mobj_SqlConnection.Close();
              }
              mobj_SqlConnection.Dispose();
          }
  
          //Clean Up Command Object
          if (mobj_SqlCommand != null)
          {
              mobj_SqlCommand.Dispose();
          }
      }
  
      catch (Exception ex)
      {
          throw new Exception("Error disposing data class." + Environment.NewLine + ex.Message);
      }
  }
  </code></pre>
  
  <p>When I step through my code I see that connections are always being opened and closed correctly, my stack is never more than five or six calls deep (I'm not running into any recursion problems) I have confirmed that all my data access code is correctly wrapped in using blocks yet my connections aren't being released back to the pool. Instead I get this error:</p>
  
  <pre><code>Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
  </code></pre>
  
  <p>This happens in a dedicated app pool with a single user effecting a delete on 10+ profiles. 
  It <em>seems</em> like I'm doing everything correctly but I am at a loss as to why connections are not being released back to the pool. At <em>most</em> there should only ever be two connections open by the executing thread, both of which should (and do!) dispose when they go out of scope. </p>
  
  <p>I'm clearly doing something wrong but can't for the life of me figure out what. </p>