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.
The scenario is as follows:
I have a table in my database named tblSessionList
which stores different sessions per subject and I want to check each time the user adds a new session if a particular Room
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 tblSessionList
.
tblSessionList
| 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 |
I have tried doing the following:
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 < 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
}
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.
Note: Every column in my database is a varchar. My apologies for having a dirty code for this is my first system ever.
Any help would be much appreciated.