How to check if startdate and enddate overlaps when changing date in datetimecontrol? (C# or SHAREPOINT)pls.

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 change date in startdate and enddate 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. :)
this are the info about the itemtemplate in my gridview:

ID= textbox
Cost= textbox
start date= datetimecontrol
end date= datetimecontrol
status= dropdownlist

:)

this is my code in startdate and enddate when datechanged:

 protected void date_issued_DateChanged(object sender, EventArgs e)
        {
            DateTime tbdate = (DateTime) sender;
            for (int i = 0; i < gvw.Rows.Count; i++)
            {
                //DateTimeControl sd = (DateTimeControl)gvw.Rows[i].Cells[2].FindControl("date_issued");
                DateTime newdtissued = new DateTime();
                DateTime dtissued;
                DateTime dexp;
                if (DateTime.TryParse(tbdate.ToString(), out newdtissued))
                { 
                    foreach (GridViewRow row in gvw.Rows)
                    {
                        if ((DateTime.TryParse(maDDL.getControlValue("date_issued", row), out dtissued)) &&
                        (DateTime.TryParse(maDDL.getControlValue("expiration_date", row), out dexp)))
                        {
                            if (dtissued <= dexp && dexp >= dtissued)
                            {
                                string msg = string.Empty;
                                msg = "Date OverLaps!";
                                ShowMessage(msg);
                                //sd.Focus();
                            }
                            else
                            {
                                string msg = string.Empty;
                                msg = "else 2nd";
                                ShowMessage(dtissued.ToString());
                            }
                        }
                    }
                }
                else
                {
                    string msg = string.Empty;
                    msg = "else 1st";
                    ShowMessage(msg);
                }
            }
        }

in this code i get an error (Specified cast is not valid.)
pls..help me and check my code :)
thanks in advance. ...

Edited 3 Years Ago by </scorpion>

i think sir this line:

DateTime tbdate = (DateTime) sender;

if (DateTime.TryParse(tbdate.ToString(), out newdtissued))

Edited 3 Years Ago by </scorpion>

If date_issued is a DateTimecontrol you have to cast sender as a DateTimecontrol then extract the SelectedDate property. something like this:

DateTime tbdate = ((DateTimeControl)sender).SelectedDate;

Edited 3 Years Ago by tinstaafl

its a datetimecontrol sir. date_issued is the name or id of my datetimecontrol. :)

thanks for the code sir. it worked!! lol.. :)
but sir
i have another question. can you help me in validating before saving all data.as i'ved said before i have a save button. when you click save it will check or validate the startdate and enddate if it is overlapping just like i'ved ask earlier.. then if its not overlap it will save all the data and if its overlap it will throw an error message.. can you pls help me sir.. is it possible if i use javascript in validation?

here's my code in save button:

protected void btnSave_Click(object sender, EventArgs e)
        {
                string id = HttpContext.Current.Request.QueryString["id1"].ToString();
                string user_name = SPContext.Current.Web.CurrentUser.Name;

                Int32 index = gvw.Rows.Count - 1;
                DateTimeControl di = (DateTimeControl)gvw.Rows[index].Cells[2].FindControl("date_issued");
                DateTimeControl ed = (DateTimeControl)gvw.Rows[index].Cells[3].FindControl("expiration_date");
                TextBox res = (TextBox)gvw.Rows[index].Cells[4].FindControl("responsible");

                if (di.SelectedDate.ToString() == "" || ed.SelectedDate.ToString() == ""
                        || res.Text.ToString() == "")
                {
                    string msg = string.Empty;
                    Panel1.Visible = true;
                    msg = "You must specify a value for this required field!!";
                    ShowMessage(msg);

                }
                else
                {                        ***CHECKING IS IN HERE***

                    for (int i = 0; i < gvw.Rows.Count; i++)
                    {
                        string dtl = gvw.DataKeys[i].Value.ToString();
                        TextBox cost = (TextBox)gvw.Rows[i].Cells[1].FindControl("estimated_cost");
                        DateTimeControl dtissued = (DateTimeControl)gvw.Rows[i].Cells[2].FindControl("date_issued");
                        DateTimeControl dexp = (DateTimeControl)gvw.Rows[i].Cells[3].FindControl("expiration_date");
                        TextBox resp = (TextBox)gvw.Rows[i].Cells[4].FindControl("responsible");
                        DropDownList stat = (DropDownList)gvw.Rows[i].Cells[5].FindControl("status");

                        string msg = string.Empty;
                        msg = maDDL.submitItem(id.ToString(), Convert.ToInt32(dtl).ToString(), Convert.ToDateTime(dtissued.SelectedDate).ToString()
                                                , txtCode.Text.ToString(), txtArea.Text.ToString(), txtDesc.Text.ToString()
                                                , Convert.ToDecimal(cost.Text).ToString(), Convert.ToDateTime(dexp.SelectedDate).ToString()
                                                , resp.Text.ToString(), stat.SelectedItem.Text.ToString()
                                                );

                        if (msg == "Record has been successfully saved!")
                        {
                            ShowMessage(msg);

                        }
                        else
                        {
                            Panel1.Visible = true;
                            lblMsg.Text = msg;
                            lblMsg.ForeColor = System.Drawing.Color.Red;
                        }
                    }
                    BindGrid(id);
                }
            }

sir there's a problem in my first question.. it doesn't check all the dates/rows in gridview it's only check the first row.. why this happen.. i follow the code sir.. here's my new code :

 protected void date_issued_DateChanged(object sender, EventArgs e)
        {
            DateTime tbdate = ((DateTimeControl)sender).SelectedDate;
            for (int i = 0; i < gvw.Rows.Count; i++)
            {
                //DateTimeControl sd = (DateTimeControl)gvw.Rows[i].Cells[2].FindControl("date_issued");
                DateTime newdtissued = new DateTime();
                DateTime dtissued;
                DateTime dexp;
                if (DateTime.TryParse(tbdate.ToString(), out newdtissued))
                {
                    foreach (GridViewRow row in gvw.Rows)
                    {
                        if ((DateTime.TryParse(maDDL.getControlValue("date_issued", row), out dtissued)) &&
                        (DateTime.TryParse(maDDL.getControlValue("expiration_date", row), out dexp)))
                        {
                            if (newdtissued <= dexp && newdtissued >= dtissued)
                            {
                                string msg = string.Empty;
                                msg = "Date OverLaps!";
                                ShowMessage(msg);
                                //sd.Focus();
                            }
                            else
                            {
                                string msg = string.Empty;
                                msg = "else 2nd";
                                ShowMessage(msg);
                            }
                        }
                    }
                }
            }
        }

I think you might need to look at breaking the check for overlap routine into a separate method, since you seem tobe doing this in several places. Something like this might work:

 bool CheckOverlap(DateTime newdt)
 {
    foreach (GridViewRow row in gvw.Rows)
    {
        DateTime dtissued = ((DateTimeControl)row.Cells["date_issued"]).SelectedDate;
        DateTime dexp = ((DateTimeControl)row.Cells["expiration_date"]).SelectedDate;
        if (newdt <= dexp && newdt >= dtissued)
        {                
            return false;
        }
    }
    return true;
}

I don't know much about the DateTimeControl or the GridView control, but generally the cells collections can be indexed by column name or index number. This code isn't tested but the general principles there should help.

Edited 3 Years Ago by tinstaafl

i fixed the problem sir.. :D
tnx again.. sir i have a question do you know how to use javascript?? can you help me in my second question yesteday??pls.. :)

ok sir. tnx again.. :D
till nxt time.. :D
you helped me a lot.. :)

Edited 3 Years Ago by </scorpion>

This question has already been answered. Start a new discussion instead.