How to check if start date and end date overlaps when adding or updating in gridview?

is there anyone who knows how to check if dates are overlapping?
this is my gridview:

| ID | cost | start date | end date | status |
---------------------------------------------
| 1  | 66.00| 11/02/13   | 11/20/13 | active | =no overlap
| 2  | 12.00| 11/21/13   | 11/30/13 | active | =no overlap
| 3  | 67.00| 11/28/13   | 12/10/13 | active | =overlap
---------------------------------------------
|____|______|____________|__________|________| =new row
---------------------------------------------
                                         |Add|
|SAVE|=insert/update data to sql database`

i have a gridview and 2 button(save and add),add button it will add a new row. when i put a data in start date and end date it will need to check the gridview if it will overlap other dates. if overlaps it will throw an error message. i dont know what to do..so please help me. :) all itemtemplate in gridview are texboxes except ID and status. :)

this is my code in startdate and enddate textchanged:

protected void date_issued_TextChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvw.Rows)
            {
                DateTime dtissued = Convert.ToDateTime(maDDL.getControlValue("date_issued", row));
                DateTime dexp = Convert.ToDateTime(maDDL.getControlValue("expiration_date", row));

                if (dtissued <= dexp || dexp >= dtissued)
                {
                    lblMsg.Text="Date OverLaps!";
                }
            }
        }

        protected void expiration_date_TextChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvw.Rows)
            {
                DateTime dtissued = Convert.ToDateTime(maDDL.getControlValue("date_issued", row));
                DateTime dexp = Convert.ToDateTime(maDDL.getControlValue("expiration_date", row));

                if (dtissued <= dexp || dexp >= dtissued)
                {
                    lblMsg.Text = "Date OverLaps!";
                }
            }
        }

pls..help me :)
thanks in advance. ...

Recommended Answers

All 12 Replies

Your code never compares the values in the gridview with the value in the textbox. See if something like this helps:

    protected void date_issued_TextChanged(object sender, EventArgs e)
    {
        DateTime newdtissued = new DateTime();
        if (DateTime.TryParse(date_issued.Text, out newdtissued))
        {
            foreach (GridViewRow row in gvw.Rows)
            {
                DateTime dtissued = Convert.ToDateTime(maDDL.getControlValue("date_issued", row));
                DateTime dexp = Convert.ToDateTime(maDDL.getControlValue("expiration_date", row));
                if (newdtissued <= dexp && newdtissued >= dtissued)
                {
                    lblMsg.Text = "Date OverLaps!";
                }
            }
        }
    }

@tinstaaf: thank you for ur reply..i will try your code. :)
@ddanbe: i don't know how to use timespan structure.

@tinstaaf: i get an error in this line,(date_issued.text)

if (DateTime.TryParse(date_issued.Text, out newdtissued))

its still saving and updating even if its overlap between the startdate and enddate.what should i do?

Click Here for some example use of TimeSpan

i don't display time sir.i only display the dates. i think timespan covers only in day,hour,minutes and seconds?

can you help me or can you give me a code in looping in gridview and check each rows if there's overlapping in dates.pls.

Could this help?

i get an error in this line,(date_issued.text)

@mhineq: what error do you get?

thank you so much to all of you :) i solved my problem.. :D

hi :)
is there anyone who knows how to add a datepicker when you click the textbox? or pop up a datepicker when you click the textbox and when you click a date in datepicker it will pass the selected date to the textbox???
pls.

thanks in advance :)

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.