title: "already an open DataReader" exception with nested SqlDataReader in ASP.NET
body: <p>I wanted to use nested SqlDataReader in the code below but I couldn't make it.I get "System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first" with the code. Any Suggestions</p>
  
  <pre><code>            c = "select user_Reps.rep_key,Officers.Officer_Name from user_Reps left join Officers on user_Reps.rep_key = Officers.Officer_code where [user_key]="+userCode;
              if (c == null)
                  c = sr.ReadToEnd();
              try
              {
  
                  SqlCommand cmd = new SqlCommand(c, cn);
                  cn.Open();
                  SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                  List&lt;Dictionary&lt;string, object&gt;&gt; list = new List&lt;Dictionary&lt;string, object&gt;&gt;();
                  SqlDataReader rdr2;
                  while (rdr.Read())
                  {
                      string c2 = "select Active_Clients.Clients_code,Active_Clients.Clients_Name,Active_Clients.Geo_code  from Active_Clients where Active_Clients.[Officer_code] =" + rdr.GetValue(0) + " order by Clients_Name";
                      SqlCommand cmd2 = new SqlCommand(c2, cn);
  
                      rdr2 = cmd2.ExecuteReader(CommandBehavior.CloseConnection);
  
  
                      Dictionary&lt;string, object&gt; d = new Dictionary&lt;string, object&gt;(rdr.FieldCount+rdr2.FieldCount);
                      while (rdr2.Read())
                      {
                          int i = 0;
                          for (; i &lt; rdr.FieldCount; i++)
                          {
                              d[rdr.GetName(i)] = rdr.GetValue(i);
                          }
  
                          for (; i &lt; rdr2.FieldCount; i++)
                          {
                              d[rdr2.GetName(i)] = rdr2.GetValue(i);
                          }
  
                          list.Add(d);
                      }
  
  
  
  
                      rdr2.Close();
  
                      //list.Add(d);
                  }
                  JavaScriptSerializer j = new JavaScriptSerializer();
                  Response.Write(j.Serialize(list.ToArray()));
  
              }
  </code></pre>