title: If condition is true then break or close sqlDatareader and get out from using (SqlDataReader rdr = cmd.ExecuteReader()) method
body: <pre><code>using (SqlDataReader rdr = cmd.ExecuteReader())
  {
      while(rdr.Read())
      {
          DateTime dt2 = Convert.ToDateTime(rdr["Date"]);
  
          int Classroom = Convert.ToInt32(rdr["ClassroomNo"]);
  
          TimeSpan ST = (TimeSpan)rdr["StartingTime"];
          TimeSpan ET = (TimeSpan)rdr["EndingTime"];
          TimeSpan span = ET.Subtract(ST);
          double Minutesdifference = span.TotalMinutes;
  
          TimeSpan NewTime = (TimeSpan)rdr["StartingTIme"];
  
          for (int i = 1; i &lt;= Minutesdifference; i++)
          {
              NewTime = NewTime.Add(TimeSpan.FromMinutes(1));
              // Response.Write(NewTime + "&lt;br&gt;");
              Response.Write(Classroom + "&lt;br&gt;");
              Response.Write(ClassroomNo + "&lt;br&gt;");
              Response.Write(dt + "&lt;br&gt;");
              Response.Write(dt2 + "&lt;br&gt;");
              Response.Write(StartTime + "&lt;br&gt;");
              Response.Write(NewTime + "&lt;br&gt;");
              Response.Write(ST + "&lt;br&gt;");
  
              if ((Classroom == ClassroomNo &amp;&amp; dt == dt2 &amp;&amp; StartTime == NewTime) || (Classroom == ClassroomNo &amp;&amp; dt == dt2 &amp;&amp; StartTime == ST))
              {
                  flag = 1;
                  break;
              }
              else
              {
                  flag = 0;
              }
          }
      }
  }
  
  if (flag == 0)
  {
      SqlCommand cmd2 = new SqlCommand("spStoreTimeSlotDetails2", con);
      cmd2.CommandType = CommandType.StoredProcedure;
  
      cmd2.Parameters.AddWithValue("@Username", hfUsername.Value);
      cmd2.Parameters.AddWithValue("@EmpName", lblFacultyName.Text);
      cmd2.Parameters.AddWithValue("@Date", dt);
      cmd2.Parameters.AddWithValue("@Day", day);
      cmd2.Parameters.AddWithValue("@ST", StartTime);
      cmd2.Parameters.AddWithValue("@ET", EndTime);
      cmd2.Parameters.AddWithValue("@TimeSlot", hfTmeSlot.Value);
      cmd2.Parameters.AddWithValue("@Topic", txtTopic.Text);
      cmd2.Parameters.AddWithValue("@ClassroomNo", ClassroomNo);
  
      cmd2.ExecuteNonQuery();
  
      lblMessage.ForeColor = System.Drawing.Color.Green;
      lblMessage.Text = "Data Saved Successfully";
  }
  else if (flag == 1)
  {
      lblMessage.ForeColor = System.Drawing.Color.Red;
      lblMessage.Text = "Time Slot already allocated by some other Employee. Please choose another";
  }
  </code></pre>
  
  <p>I write If Condition into for loop. If that condition is true then break the for loop but <code>while(rdr.Read())</code> method still executing. I just want to close Reader method and directly go out from <code>using(sqlDataReader rdr=cmd.ExecuteReader)</code> method and execute if(flag==0).</p>