I am creating a database for a project. In this project there is a table called 'Bookings' this table has 2 fields called DateOfBooking
(which is data type DateAndTime) and TimeOfBooking
(which is data type DateAndTime also). In a UI form on the same project I have created a date and time picker that corresponds to the DateOfBooking
field and a text box that corresponds to the TimeOfBooking
field. For my system to be as robust as possible, I would like there to be no double bookings. So I need to create a way to check that the specific dates and times haven't already been entered to the database. I can only find ways that allow for one field to be checked, which is not what I would like. I need to check both fields. Here is an attempt of what I have done:
//To Check that there is no double bookings
using(SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["SimpsonsConnection"].ConnectionString))
{
sc.Open();
SqlCommand commandToCheckDoubleBookings = new SqlCommand("SELECT DateOfBooking, TimeOfBooking FROM Bookings WHERE DateOfBooking= '" + dtp1.Text + "' AND TimeOfBooking= '" + txtTimeOfBooking.Text + "'", sc);
string db = (string)commandToCheckDoubleBookings.ExecuteScalar();
sc.Close();
if(db == dtp1.Text )
{
MessageBox.Show("This booking slot has already been taken, please choose another!");
}
else if(db ==)
}
In this code, I am getting problems with the if statements, as they aren't checking the two fields at the same time. Only one field is being checked at a time. If there is any way you could help, it would be highly appreciated. If you need any extra info about the problem let me know! (My code is c#)