title: Check if Time Range intersects in C#
body: <p>I am currently developing a simple enrollment system in C# and I am thinking if there is a straightforward way to check if a certain time range (From - To) of my input session intersects with a time range in the database.</p>
  
  <p>The scenario is as follows:</p>
  
  <p>I have a table in my database named <code>tblSessionList</code> which stores different sessions per subject and I want to check each time the user adds a new session if a particular <code>Room</code> is occupied already in that particular time range. Technically, I want to check if the chosen time range does not conflict with the sessions in <code>tblSessionList</code>.</p>
  
  <p><img src="https://i.stack.imgur.com/0xsKa.jpg" alt="Add Session Form"></p>
  
  <p><code>tblSessionList</code></p>
  
  <pre><code> | Subject Code |   Venue    |   Room   |        Date             |  From   |   To    |
   |              |            |          |                         |         |         |
   |   CRGOV401   |  GT Tower  | Room 3D  | Thursday, June 19, 2014 | 8:00 AM |10:00 AM |
   |   CRGOV401   |  GT Tower  | Room 59  | Sunday, June 29, 2014   | 1:00 PM |3:00  PM |
   |   GNBNK201   | Main Plaza | HR Hall  | Monday, June 30, 2014   | 9:00 AM |11:00 AM |
   |   GNBNK201   | Main Plaza | HR Hall  | Monday, June 30, 2014   | 1:00 PM |3:00     |
  </code></pre>
  
  <p>I have tried doing the following:</p>
  
  <pre><code> private void btnAddSession_Click(object sender, EventArgs e)
      {
          bool proceedCopy = true;
  
          DataTable timeHolder = new DataTable();
          SqlDataAdapter selectTime = new SqlDataAdapter("select [From],[To] from tblSessionList where [Venue] = @venue and [Room] = @room", Connection.conn);
          selectTime.SelectCommand.Parameters.AddWithValue("@venue", venue.Text);
          selectTime.SelectCommand.Parameters.AddWithValue("@room", room.Text);
  
          selectTime.Fill(timeHolder);
  
  
          for(int i = 0; i &lt; timeHolder.Rows.Count; i++)
         {
  
          if(timeHolder[i][0].toString() == From.Text) //checks if the 'From' of the query is the same with the 'From' of the new entry
                {
                  MessageBox.Show("The room is already occupied on that time range");
                  proceedCopy = false;
                  break;
                }
  
          else if(timeHolder[i][1].toString() == To.Text) //checks if the 'To' of the query is the same with the 'To' of the new entry.
                {
                   MessageBox.Show("The room is already occupied on that time range");
                   proceedCopy = false;
                   break;
                }
          }
         if(proceedCopy == true)
             //insert code to insert new session to database here
      }
  </code></pre>
  
  <p>This is a hard-coded checking and this only checks the end points of the time range, not the time range itself. I'm thinking if there's a function in C# that I could use like Time only instead of DateTime.</p>
  
  <p>Note: Every column in my database is a varchar. My apologies for having a dirty code for this is my first system ever.</p>
  
  <p>Any help would be much appreciated.</p>