title: SQL ERROR: The connection was not closed. The connection's current state is open
body: <p><strong>EDIT</strong></p>
  
  <p>After staring at this for 2 days, I do see one issue.  I was still opening the original connection.  So I changed the inner open statements to <code>conn2.Open</code>.  Then, I changed the second inner query to where all the variables were number 3 instead of 2 so that they were completely different than the previous query.  At that point, I got the error:</p>
  
  <blockquote>
    <p>There is already an open DataReader associated with this Command which must be closed first.</p>
  </blockquote>
  
  <p>I took out the inner connections, thinking I could use the outer connection and took out the inner <code>.Close</code> lines, but that also returned an error saying the connection was not closed.</p>
  
  <p><strong>END EDIT</strong></p>
  
  <p>I am writing a script that updates user information with data pulled from other tables where that user may be in it multiple times for purchases made.</p>
  
  <p>So first, the "outside" sql query pulls some data from the items table which contains purchaser information as well as category information.  For each item, it is going to check it's purchaser's information.</p>
  
  <p>Second, the first "inner" sql query pulls category information from the user table.  Some code is then run to see if they're already marked as purchasing from the category of the "outside" query.  If they are not, it adds the category to a string variable.</p>
  
  <p>Lastly, the second "inner" sql query updates the user table for the current user with the new category list.</p>
  
  <p>I've asked about how to perform queries like this before, but was always given a solution of combining the queries into one.  That worked for the other queries, but I cannot do that here.  I must iterate through each record of the outer query to perform the necessary functions inside of it.  But my issue here is that I get an SQL error saying that the connection was not closed, and it points to the catch of the outer query (for '<code>conn</code>').</p>
  
  <p>I had tried to set my 2 inner queries so that they used different connection variables (<code>conn2</code> and <code>conn3</code>), and also different strSQL variables, but that didn't help.  And I'm still a newb when it comes to SQL, having programmed using MySQL until this probject.  Any help would be greately appreciated.</p>
  
  <pre><code>    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionName"].ToString()))
      using (SqlCommand strSQL = conn.CreateCommand())
      {
          strSQL.CommandText = "SELECT field FROM itemsTable";
          try
          {
              conn.Open();
              using (SqlDataReader itemReader = strSQL.ExecuteReader())
              {
                  while (itemReader.Read())
                  {
                      {Do some stuff here}
  
                      using (SqlConnection conn2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionName"].ToString()))
                      using (SqlCommand strSQL2 = conn2.CreateCommand())
                      {
                          strSQL2.CommandText = "SELECT fields FROM userTable";
                          try
                          {
                              conn2.Open();
                              using (SqlDataReader itemReader2 = strSQL2.ExecuteReader())
                              {
                                  while (itemReader2.Read())
                                  {
                                      {Do stuff here}
                                  }
                                  itemReader2.Close();
                              }
                          }
                          catch (Exception e3)
                          {
                              throw new Exception(e3.Message);
                          }
                          finally
                          {
                              conn2.Close();
                          }
                      }
  
                      {Do some more stuff here}
  
                      using (SqlConnection conn2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionName"].ToString()))
                      using (SqlCommand strSQL2 = conn2.CreateCommand())
                      {
                          strSQL2.CommandText = "UPDATE userTable set field='value'";
                          try
                          {
                              conn2.Open();
                              strSQL2.ExecuteNonQuery();
                          }
                          catch (Exception e2)
                          {
                              throw new Exception(e2.Message);
                          }
                          finally
                          {
                              conn2.Close();
                          }
                      }
  
                      {Do even more stuff here.}
                  }
  
                  itemReader.Close();
              }
          }
          catch (Exception e1)
          {
              throw new Exception(e1.Message);
          }
          finally
          {
              conn.Close();
          }
      }
  </code></pre>