简体   繁体   中英

Booking system SQL query: Search through database using start and end date and size available

I am trying to create an SQL query so that when a user enters a start date, end date and selects a size from the drop down list which is connected to my database, the results show the available dresses for the selected dates when the button is clicked.

This is my schema.

Product (Product_ID, Size, Image, Description)

Booking (Booking_ID, Product_ID, Start_Date, End_Date)

This is the first query I tried.

SELECT Product_ID FROM Product P 
WHERE Size = 'DropDownList1.SelectedValue' AND NOT EXISTS 
(SELECT * FROM Booking 
WHERE Product_ID = P.Product_ID AND Start_Date >= 'Start' AND End_Date <= 'End')

I have no VB code yet as I'm not too sure where to put it or what to write. When I try to test the query it says that the conversion of the date failed.

Also, what code should I put into the search button to get it to show results in a listview?

This is the jQuery I am using.

<script>
    $(function () {
        $("#Start").datepicker({
            dateFormat: 'dd/mm/yy',
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 1,
            onClose: function (selectedDate) {
                $("#End").datepicker("option", "minDate", selectedDate);
            }
        });
        $("#End").datepicker({
            dateFormat: 'dd/mm/yy',
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 1,
            onClose: function (selectedDate) {
                $("#Start").datepicker("option", "maxDate", selectedDate);
            }
        });
    });

And this is where the user enters the date using the jQuery datepicker.

Date Needed: <input type="text" id="Start">

Return Date: <input type="text" id="End">

I have tried a few different things and it is still not working. I have added some VB which looks like this. But I don't think it is right. Start and End have blue lines under them. It is saying for start that it "is not declared" and for End it is saying "Expression expected".

Partial Class Pages_Default
Inherits System.Web.UI.Page

Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    Dim recordsAffected As Integer
    Dim cmdstring As String = "SELECT Booking(Start_Date, End_Date) Values(@Start, @End)"
    conn = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=F:\BBB.mdf;Integrated Security=True")
    cmd = New SqlCommand(cmdstring, conn)
    cmd.Parameters.Add("@Start", SqlDbType.Date).Value = Start.SelectedValue
    cmd.Parameters.Add("@End", SqlDbType.Date).Value = End.SelectedValue
    conn.Open()
    recordsAffected = cmd.ExecuteNonQuery
    conn.Close()
End Sub
End Class

I have also changed the SQL query to this and it is not working.

SELECT Image, Price, Description FROM Product P WHERE Size = 'DropDownList1.SelectedValue' AND NOT EXISTS (SELECT * FROM Booking WHERE Product_ID = P.Product_ID AND Start_Date >= @Start AND End_Date <= @End)

Conversion failed because it is trying to turn the text "Start" into a date (which it can't). Your first step is to replace those text fields with parameters, so that your search posts the date fields into those parts of the SQL.

Edit: This page has an example of how to do the query properly and securely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM