Hi All,

I am getting the error "There is already an open DataReader associated with this Command which must be closed first.
"

protected void Page_Load(object sender, EventArgs e)
        {
            nbseapi nbsi = new nbseapi();
            Guid guid = new Guid();
             
            if (Request.QueryString["Id"] != null)
            {
                string Id = Request.QueryString["Id"];
               

                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SimplifyNew"].ToString()))
                {

                    SqlCommand cmd = new SqlCommand("uspSelectOrder", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    
                    cmd.Parameters.Add(new SqlParameter("@CompId", SqlDbType.VarChar)).Value = Id;
                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {


                        string strId = reader["BundleIds"].ToString();
                       string [] str1=strId.Split(',');

                       foreach (string id in str1)
                        {
                            string BundleID = id;

                            string strNode = reader["FieldOrdering"].ToString();
                            string[] str2 = strNode.Split(',');
                            foreach(string nodes in str2)
                            {
                                BundleSearchResultStruct BundleSearchResult = nbsi.GetProductDetails(guid, "marketingPartnerPassword", Convert.ToInt32(BundleID));
                             using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SimplifyNew"].ToString()))
                             {
                              SqlCommand cmd1 = new SqlCommand("usp_selectnodes", conn);
                                 cmd.CommandType = CommandType.StoredProcedure;
                                  cmd.Parameters.Add(new SqlParameter("@node", SqlDbType.VarChar)).Value = nodes;
                                  conn1.Open();
                                  SqlDataReader reader1 = cmd.ExecuteReader();

                                 while (reader.Read())
                                 {
                                      
                                   TableRow tRow = new TableRow();
                                   Table1.Rows.Add(tRow);  
                                      
                                     int id1=Convert.ToInt32(id);
                                     for(int cellcntr=1;cellcntr<=id1;cellcntr++)
                                     {
                                     TableCell tCell = new TableCell();
                                      tRow.Cells.Add(tCell);
                                     
                                     }
                                     int nodes1=Convert.ToInt32(nodes);
                                      for(int rowCtr=1; rowCtr <= nodes1; rowCtr++)
                                      {
                                     TableRow tRow1 = new TableRow();
                                     Table1.Rows.Add(tRow1); 

                                          TableCell tCell = new TableCell();
                                           tRow.Cells.Add(tCell); 
                                            tCell.Text=reader["Description"].ToString();
                                              for(int cellcntr=1;cellcntr<=id1;cellcntr++)
                                                {
                                                    TableCell tCell1 = new TableCell();
                                                    tRow.Cells.Add(tCell1);
                                                  //string prp=reader["XmlName"].ToString();
                                                  //tCell1.Text = BundleSearchResult.

                                                    Type type = BundleSearchResult.GetType();
                                                    PropertyInfo senderproperty = type.GetProperty(reader["XmlName"].ToString());

                                                    object PropertyValue = senderproperty.GetValue(BundleSearchResult, null);
                                                    tCell1.Text = PropertyValue.ToString();

                                                    
                                     
                                                }

                                      }
                                     
                                     
                                 
                                 }
                                 reader1.Close();
                                   conn1.Close();
                             
                             }
                            
                            
                            }
                             

                           

                               
                               }
                           
                           }
                    reader.Close();
                         conn.Close();
                    
                           }

Please help

Thanks,
sniigg

Dani AI

Generated

Short diagnosis and practical fixes.

The exception happens because a SqlDataReader was left open on the same connection/command when another ExecuteReader was attempted. In the posted code the inner command/reader were accidentally wired to the outer objects (a typo/copy‑paste issue), which left the original reader active — exactly what flagged and what caused the error in ’s trace.

Why this matters: ADO.NET does not allow two active readers on one SqlConnection by default. Options are to (a) close the first reader before issuing the next command, (b) open a separate SqlConnection for the nested query, or (c) enable MultipleActiveResultSets (MARS) in the connection string (add MultipleActiveResultSets=True). MARS solves the simultaneous-reader constraint but has nontrivial behavioral and performance implications, so it should be chosen deliberately.

Recommended patterns and checklist

  • Fix the variable mix-up so the inner SqlCommand uses the inner SqlConnection and ExecuteReader is called on the correct command (avoid reusing the outer cmd variable).
  • Prefer reading the outer resultset into a small in‑memory structure (List/array/DataTable), dispose the reader, then run child queries.
  • If nested readers are required and immediate, use a second connection for the inner ExecuteReader.
  • Always use using blocks (or try/finally) to guarantee reader.Close()/Dispose() on every path.
  • Verify inner loops call the inner reader (not the outer one), and that Close() runs even on exceptions.

This addresses the immediate cause flagged by and gives safer long‑term patterns to avoid the same exception later.

Recommended Answers

All 2 Replies

Line 39 should be like this
SqlCommand cmd1 = new SqlCommand("usp_selectnodes", conn1);

Thanks a lot...silly me :):)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.