title: when putting multiple sql commands showing error :There is already an open DataReader associated with this Command which must be closed first
body: <p>Friends, when I write these code in asp.net I get an error like this: </p>
  
  <blockquote>
    <p>There is already an open DataReader associated with this Command which
    must be closed first</p>
  </blockquote>
  
  <p>help me please</p>
  
  <pre><code>SqlConnection con = obj.getcon();
  con.Open();
  
         SqlCommand cmd = new SqlCommand("select student_id from student_details", con);
         SqlDataReader dr = cmd.ExecuteReader();
         while (dr.Read())
         {
             String ss=dr[0].ToString();
             if (val == ss)
             {
  
  
  
                 SqlCommand cmd1 = new SqlCommand("insert student_vs_testsession_details(student_id,testsession_id,testsession_status) values('" + val + "','" + Test + "','')", con);
                 int val1 = cmd1.ExecuteNonQuery();
  
  
             }
             else
             {
  
  
                 string message = "&lt;script language=JavaScript&gt; alert('StudentID does not exists!'); &lt;/script&gt;";
                 if (!Page.IsStartupScriptRegistered("clientScript"))
                 {
                     Page.RegisterStartupScript("clientScript", message);
                 }
             }
  
         } 
       dr.Close();
        // con.Close();
  
      }
  </code></pre>