Hey guys,
I have two fields in a table namely Start_date and End_date.
Now, a user is expected to select a start date and end date on a web page. These dates should be compared with the dates in the database in such a way that the dates entered by the user are not in between the dates in the table.
For eg..
if the user enters:
Start date: 12/07/2009
End Date: 15/07/2009

The entry stored in the table is :
Start_Date End_Date
11/07/2009 14/07/2009

When the user submits, the the message should be displayed "Dates not available"
Please suggest some logic with some code.

Try this
let use input dates be u_Start_date and u_End_date
and dates in database be d_Start_date and d_End_date

if( ( (u_Start_date >= d_Start_date && u_Start_date<=d_End_date) || (u_End_date >= d_Start_date && u_End_date<=d_End_date) )    ||
((d_Start_date >=u_Start_date && u_End_date >= d_Start_date) ||
(d_End_date >=u_Start_date && u_End_date >= d_End_date)))

Try the following logic.

DateTime m_StartDate, m_EndDate;
        DateTime db_StartDate, db_EndDate;

        //Write code m_StartDate and m_EndDate from the web page
        /*
        m_StartDate = Start Date from the web page
        m_EndDate = End Date from the web page
        */

        /*
         * Write code to get Start Date and End Date from 
         * the database using a data reader 
         * fill it in the followin variables
          db_StartDate = Start Date from db
          db_EndDate = End Date from db
        */

        bool isValidRange = false;

        if (m_StartDate <= m_EndDate)
        {
            if ((m_StartDate >= db_StartDate) &&
                (m_EndDate <= db_EndDate))
                isValidRange = true;
        }

        if (isValidRange)
            Response.Write("Valid date range");
        else
            Response.Write("Invalid date range");

Also you can pass the m_StartDate and m_EndDate values to a stored precedure where you can validate the date range and return a boolean value based on that validation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.