0

I have to create a timetable and so I am firstly starting off by adding a class. In the below code I am adding class to my database, but before sending I want to have a button (clashBtn) which checks for clashes. Basically I want to check to see if a room is booked for the same time on a particular day. I am not sure how to do this with the way I have coded it. Please help :)

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<div id="breadcrumb">
    <a href="AdminDefault.aspx" title="Dashboard" class="tip-bottom"><i class="icon-home"></i>Admin Dashboard</a>
    <a href="EditProgramme.aspx" class="current">Edit Programme</a>
</div>

<div class="container-fluid">
    <div class="headpass">
            <h2>Add Module Class</h2>
        </div>

        <div class="widget-content nopadding">
            <table class="table table-bordered">
                <tbody>
                    <tr>
                        <td>
                           <h5>    <asp:Label ID="TitleLabel" runat="server" Text="Course Name:"></asp:Label> </h5>
                        </td>

                        <td>
                            <asp:DropDownList ID="ddlCourse" Width="400px" runat="server" AutoPostBack="true" 
                                OnSelectedIndexChanged="ddlModule_SelectedIndexChanged">
                                <asp:ListItem Text="--Select Course--" ></asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td>
                          <h5>  <asp:Label ID="Label1" runat="server" Text="Module Name"></asp:Label></h5>
                        </td>

                        <td>
                            <asp:DropDownList ID="ddlModule" Width="400px" runat="server" Enabled="false" AutoPostBack="true">
                                <asp:ListItem Text="--Select Module--"></asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>

                     <tr>
                        <td>
                            <h5>
                                <asp:Label ID="Label2" runat="server" Text="Class Type"></asp:Label></h5>
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlClassType" Width="400px" runat="server">
                                <asp:ListItem Value="1">Advisory</asp:ListItem>
                                <asp:ListItem Value="2">Lecture</asp:ListItem>
                                <asp:ListItem Value="3">Practical</asp:ListItem>
                                <asp:ListItem Value="4">Tutorial</asp:ListItem>
                                <asp:ListItem Value="5">Seminar</asp:ListItem>
                            </asp:DropDownList></td>
                    </tr>

                              <tr>
                      <td>
                           <h5>    <asp:Label ID="StartDateText" runat="server" Text="Start Date:"></asp:Label> </h5>
                        </td>
                                  <td>
                            <asp:TextBox ID="txtstartdate"  TextMode="Date" Width="150px" runat="server" >

                            </asp:TextBox>
                        </td>
                    </tr>
                            <tr>
                      <td>
                           <h5>    <asp:Label ID="EndDateText" runat="server" Text="End Date:"></asp:Label> </h5>
                        </td>
                                  <td>
                            <asp:TextBox ID="txtenddate"  TextMode="Date" Width="150px" runat="server">

                            </asp:TextBox>
                        </td>
                    </tr>
                                 <tr>
                        <td>
                            <h5>
                                <asp:Label ID="reoccuranceTxt" runat="server" Text="Repeat:"></asp:Label></h5>
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlRecurrence" Width="150px" runat="server" AutoPostBack="true" 
                                OnSelectedIndexChanged="ddlRecurrance_SelectedIndexChanged">
                                 <asp:ListItem Value="0"> --Select Repeat--</asp:ListItem>
                                <asp:ListItem Value="1">Daily</asp:ListItem>
                                <asp:ListItem Value="2">Weekly</asp:ListItem>
                                <asp:ListItem Value="3">None</asp:ListItem>
                            </asp:DropDownList></td>
                    </tr>
                      <tr>
                        <td>
                            <h5>
                                <asp:Label ID="dayTxt" runat="server"  Text="Day:"></asp:Label></h5>
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlDay" Enabled="false" Width="150px" runat="server">
                                <asp:ListItem Value="0"> --Select Day--</asp:ListItem>
                                <asp:ListItem Value="1">Monday</asp:ListItem>
                                <asp:ListItem Value="2">Tuesday</asp:ListItem>
                                <asp:ListItem Value="3">Wednesday</asp:ListItem>
                                <asp:ListItem Value="4">Thursday</asp:ListItem>
                                <asp:ListItem Value="5">Friday</asp:ListItem>
                                <asp:ListItem Value="6">Saturday</asp:ListItem>
                                <asp:ListItem Value="7">Sunday</asp:ListItem>
                            </asp:DropDownList></td>
                    </tr>
                        <tr>
                        <td>
                            <h5>
                                <asp:Label ID="Label3" runat="server" Text="Class Start Time"></asp:Label></h5>
                        </td>
                            <td>
                                <asp:DropDownList ID="ddlStartClassTime" Width="150px" runat="server" DataSourceID="dsDay">
                                    <asp:ListItem Value="0"> --Select Time--</asp:ListItem>
                                    <asp:ListItem Value="1">08:00</asp:ListItem>
                                    <asp:ListItem Value="2">09:00</asp:ListItem>
                                    <asp:ListItem Value="3">10:00</asp:ListItem>
                                    <asp:ListItem Value="4">11:00</asp:ListItem>
                                    <asp:ListItem Value="5">12:00</asp:ListItem>
                                    <asp:ListItem Value="6">13:00</asp:ListItem>
                                    <asp:ListItem Value="7">14:00</asp:ListItem>
                                    <asp:ListItem Value="8">15:00</asp:ListItem>
                                    <asp:ListItem Value="9">16:00</asp:ListItem>
                                </asp:DropDownList>
                              <font color="red">
                                  <asp:Label ID="lblErrorStartTime"  runat="server" Visible="false" Text="Error - Start time is greater that the end time"></asp:Label> </font>


                                <asp:SqlDataSource runat="server" ID="dsDay"></asp:SqlDataSource>
                            </td>
                    </tr>
                    <tr>
                        <td>
                            <h5>
                                <asp:Label ID="Label4" runat="server" Text="Class End Time"></asp:Label></h5>
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlEndClassTime" Width="150px" runat="server"  AutoPostBack="true" OnSelectedIndexChanged="ddlEndClassTime_SelectedIndexChanged">
                                <asp:ListItem Value="0"> --Select Time--</asp:ListItem>
                                <asp:ListItem Value="1">08:00</asp:ListItem>
                                <asp:ListItem Value="2">09:00</asp:ListItem>
                                <asp:ListItem Value="3">10:00</asp:ListItem>
                                <asp:ListItem Value="4">11:00</asp:ListItem>
                                <asp:ListItem Value="5">12:00</asp:ListItem>
                                <asp:ListItem Value="6">13:00</asp:ListItem>
                                <asp:ListItem Value="7">14:00</asp:ListItem>
                                <asp:ListItem Value="8">15:00</asp:ListItem>
                                <asp:ListItem Value="9">16:00</asp:ListItem>
                            </asp:DropDownList><font color="red">
                              <asp:Label ID="lblErrorEndTime"  runat="server" Visible="false" Text="Error - End time must be greater than the start time"></asp:Label> </font>


                        </td>

                    </tr>

                                    <tr>
                        <td>
                           <h5>    <asp:Label ID="Label5" runat="server" Text="Building Name:"></asp:Label> </h5>
                        </td>

                        <td>
                            <asp:DropDownList ID="ddlBuilding" Width="400px" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlRoomCode_SelectedIndexChanged">
                                <asp:ListItem Text="--Select Building--" ></asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                      <td>
                           <h5>    <asp:Label ID="Label6" runat="server" Text="Room Code:"></asp:Label> </h5>
                        </td>
                                  <td>
                            <asp:DropDownList ID="ddlRoomCode" Width="400px" runat="server" Enabled="false" AutoPostBack="true">
                                <asp:ListItem Text="--Select Room--"></asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>      
                      <asp:Label ID="lbldateValue" runat="server" Text=""></asp:Label>
        </tbody>
  </table>  
  </div>
   <br />
             <asp:Button ID="clashBtn" runat="server" class="btn btn-primary"  Text="Clash check" />
             <asp:Button ID="submitBtn" runat="server" class="btn btn-success" OnClick="insertbutton_Click" Text="Add Class" />
    </div>
</asp:Content>

C# code:

protected void insertbutton_Click(object sender, EventArgs e)
{
    string DefaultConnection = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    SqlConnection myConnection = new SqlConnection(DefaultConnection);

    myConnection.Open();

    // string coursedata = ddlCourse.Text;
    string moduledata = ddlModule.Text;
    string classtypedata = ddlClassType.Text;
    string startdatedata = txtstartdate.Text;
    string enddatedata = txtenddate.Text;
    string recurrencedata = ddlRecurrence.Text;
    string daydata = ddlDay.Text;
    string starttimedata = ddlStartClassTime.Text;
    string endtimedata = ddlEndClassTime.Text;
    string buildingdata = ddlBuilding.Text;
    string roomcodedata = ddlRoomCode.Text;

    string query = "INSERT INTO CLASS (DayId, ModuleId, ClassTypeId, ClassScheduleStartTimeId, ClassScheduleEndTimeId, RecurrenceId, RoomCodeId, StartTime, EndTime ) VALUES ('" + daydata + "', '" + moduledata + "', '" + classtypedata + "', '" + starttimedata + "', '" + endtimedata + "', '" + recurrencedata + "',  '" + roomcodedata + "',  '" + startdatedata + "',  '" + enddatedata + "')";

    SqlCommand myCommand = new SqlCommand(query, myConnection);

    myCommand.ExecuteNonQuery();
    myConnection.Close();
}
FLICKER
  • 6,439
  • 4
  • 45
  • 75
David Craven
  • 57
  • 10
  • 3
    *Please* read up about and **[start using parameterized queries](http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work)** before [Bobby Tables](http://bobby-tables.com/) comes to teach you the hard way. – Siyual Mar 01 '17 at 19:01
  • You probably want to check for clashes regardless of whether or not the clash button is clicked. You can change your Insert query so that it only inserts if no clash exists. Or do a separate query to check for the clash before the insert. – devlin carnate Mar 01 '17 at 19:04
  • Thanks. I plan to set the visibility of the submit button as false unless no clashes exist. I just don't know how to write a query to check for clashes – David Craven Mar 01 '17 at 19:08
  • Possible duplicate of [SQL query to search for room availability](http://stackoverflow.com/questions/29213183/sql-query-to-search-for-room-availability) – Tab Alleman Mar 01 '17 at 19:49

0 Answers0