/// <summary>
        /// UpdateForDashboard
        /// </summary>        
        /// <param name="companyId">companyId</param>
        private void UpdateForDashboard(int companyId)
        {
            foreach (DashboardTDS.DashboardToDoListAssignedToMeRow row in (DashboardTDS.DashboardToDoListAssignedToMeDataTable)Table)
            {
                int toDoId = row.ToDoID;

                ToDoListInformationActivityInformation toDoListInformationActivityInformation = new ToDoListInformationActivityInformation();
                toDoListInformationActivityInformation.LoadAllByToDoId(toDoId, companyId);

                int lastRefId = toDoListInformationActivityInformation.GetLastAssignedUserRefId();

                if (row.RefID != lastRefId)
                {
                    row.Delete();
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // ToDoListIVATE METHODS
        //
        /// <summary>
        /// UpdateForToDoListocess
        /// </summary>
        /// <param name="companyId">companyId</param>
        private void UpdateForReport(int companyId)
        {
            // Delete extra rows
            foreach (ToDoListToDoAssignedToMeReportTDS.ToDoListToDoAssignedToMeRow row in (ToDoListToDoAssignedToMeReportTDS.ToDoListToDoAssignedToMeDataTable)Table)
            {
                int toDoId = row.ToDoID;
                ToDoListInformationActivityInformation toDoListInformationActivityInformation = new ToDoListInformationActivityInformation();
                toDoListInformationActivityInformation.LoadAllByToDoId(toDoId, companyId);
                int lastRefId = toDoListInformationActivityInformation.GetLastAssignedUserRefId();

                if (row.RefID != lastRefId)
                {
                    row.Deleted = true;
                }
            }

            // Update text for report
            ToDoListToDoAssignedToMeActivityReport toDoListToDoAssignedToMeActivityReport = new ToDoListToDoAssignedToMeActivityReport(Data);

            foreach (ToDoListToDoAssignedToMeReportTDS.ToDoListToDoAssignedToMeRow row in (ToDoListToDoAssignedToMeReportTDS.ToDoListToDoAssignedToMeDataTable)Table)
            {
                if (!row.Deleted)
                {
                    toDoListToDoAssignedToMeActivityReport.LoadToDoAssignedToMeActivityByToDoId(row.ToDoID, row.COMPANY_ID);
                }
            }

            toDoListToDoAssignedToMeActivityReport.UpdateForReport();

            // Delete extra rows
            foreach (ToDoListToDoAssignedToMeReportTDS.ToDoListToDoAssignedToMeRow row in (ToDoListToDoAssignedToMeReportTDS.ToDoListToDoAssignedToMeDataTable)Table)
            {
                if (row.Deleted)
                {
                    row.Delete();
                }
            }
        }
        private void UpdateDatabase()
        {
            // Get ids
            int companyId = Int32.Parse(hdfCompanyId.Value);

            DB.Open();
            DB.BeginTransaction();
            try
            {
                // ... Update general todo list state
                ToDoListInformationBasicInformation toDoListInformationBasicInformation = new ToDoListInformationBasicInformation(toDoListInformationTDS);
                toDoListInformationBasicInformation.Save(companyId);

                // ... Save to do list details
                ToDoListInformationActivityInformation toDoListInformationActivityInformation = new ToDoListInformationActivityInformation(toDoListInformationTDS);
                toDoListInformationActivityInformation.Save(companyId);

                // ... Send mails
                if (hdfCompleted.Value == "True")
                {
                    SendMailForCompletedToDo();
                }

                DB.CommitTransaction();

                // Store datasets
                toDoListInformationTDS.AcceptChanges();
                Session["toDoListInformationTDS"] = toDoListInformationTDS;
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void LoadActivityData(int toDoId, int companyId)
        {
            ToDoListInformationActivityInformation toDoListInformationActivityInformation = new ToDoListInformationActivityInformation();
            toDoListInformationActivityInformation.LoadAllByToDoId(toDoId, companyId);
            int lastRefId = toDoListInformationActivityInformation.GetLastAssignedUserRefId();

            ToDoListInformationActivityInformationGateway toDoListInformationActivityInformationGateway = new ToDoListInformationActivityInformationGateway(toDoListInformationActivityInformation.Data);
            toDoListInformationActivityInformationGateway.LoadAllByToDoId(toDoId, companyId);

            if (toDoListInformationActivityInformationGateway.Table.Rows.Count > 0)
            {
                // For last assigned user
                tbxAssignedUser.Text = toDoListInformationActivityInformationGateway.GetEmployeeFullName(toDoId, lastRefId);
                hdfAssignedUser.Value = toDoListInformationActivityInformationGateway.GetEmployeeID(toDoId, lastRefId).ToString();
            }
        }
        private void GrdToDoAdd()
        {
            if (ValidateToDoFooter())
            {
                Page.Validate("activityDataNew");

                if (Page.IsValid)
                {
                    int employeeId = 0;
                    EmployeeGateway employeeGateway = new EmployeeGateway();
                    string dateTime_ = ((TextBox)grdToDoList.FooterRow.FindControl("tbxDateTimeNew")).Text;
                    string comment = ((TextBox)grdToDoList.FooterRow.FindControl("tbxCommentsNew")).Text;
                    string type_ = "";
                    string employeeFullName = "";

                    GridViewRow row = grdToDoList.FooterRow;
                    DropDownList action = ((DropDownList)row.FindControl("ddlActionsNew"));

                    switch (action.SelectedValue)
                    {
                        case "Assign To User":
                            employeeId = Int32.Parse(((DropDownList)grdToDoList.FooterRow.FindControl("ddlAssignToTeamMemberNew")).SelectedValue);
                            employeeGateway.LoadByEmployeeId(employeeId);
                            employeeFullName = employeeGateway.GetFullName(employeeId);
                            type_ = "AssignUser";
                            hdfToDoListState.Value = "In Progress";
                            break;

                        case "Add Comment":
                            int loginId = Convert.ToInt32(Session["loginID"]);
                            employeeId = employeeGateway.GetEmployeIdByLoginId(loginId);
                            type_ = "AddComment";
                            employeeFullName = employeeGateway.GetFullName(employeeId);
                            hdfToDoListState.Value = "In Progress";
                            break;

                        case "Complete":
                            employeeId = Int32.Parse(hdfAssignedUser.Value);
                            employeeGateway.LoadByEmployeeId(employeeId);
                            employeeFullName = employeeGateway.GetFullName(employeeId);
                            type_ = "CloseToDo";
                            hdfCompleted.Value = "True";
                            hdfToDoListState.Value = "Completed";
                            break;

                        case "Put On Hold":
                            int loginIdOnHold = Convert.ToInt32(Session["loginID"]);
                            employeeId = employeeGateway.GetEmployeIdByLoginId(loginIdOnHold);
                            type_ = "OnHoldToDo";
                            employeeFullName = employeeGateway.GetFullName(employeeId);
                            hdfToDoListState.Value = "On Hold";
                            break;
                    }

                    // static values
                    int toDoId = Int32.Parse(hdfToDoId.Value);
                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    bool inDatabase = false;

                    ToDoListInformationBasicInformation generalModel = new ToDoListInformationBasicInformation(toDoListInformationTDS);
                    generalModel.UpdateState(toDoId, hdfToDoListState.Value);

                    ToDoListInformationActivityInformation model = new ToDoListInformationActivityInformation(toDoListInformationTDS);
                    model.Insert(toDoId, employeeId, type_, DateTime.Parse(dateTime_), comment, false, companyId, inDatabase, employeeFullName);

                    Session.Remove("activityInformationDummy");
                    Session["toDoListInformationTDS"] = toDoListInformationTDS;

                    grdToDoList.DataBind();
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_TODOLIST_VIEW"]) && Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_TODOLIST_EDIT"])))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["toDo_id"] == null) || ((string)Request.QueryString["action"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in toDoList_activity.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfToDoId.Value = Request.QueryString["toDo_id"].ToString();
                hdfAction.Value = Request.QueryString["action"].ToString();
                hdfDashboard.Value = Request.QueryString["dashboard"].ToString();

                // If coming from
                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int currentToDoId = Int32.Parse(hdfToDoId.Value.ToString());

                Session.Remove("activityInformationDummy");

                // ... toDoList_navigator2.aspx
                if (Request.QueryString["source_page"] == "toDoList_navigator2.aspx")
                {
                    StoreNavigatorState();
                    toDoListInformationTDS = new ToDoListInformationTDS();
                    activityInformation = new ToDoListInformationTDS.ActivityInformationDataTable();

                    ToDoListInformationBasicInformation toDoListInformationBasicInformation = new ToDoListInformationBasicInformation(toDoListInformationTDS);
                    toDoListInformationBasicInformation.LoadAllByToDoId(currentToDoId, companyId);

                    ToDoListInformationBasicInformationGateway toDoListInformationBasicInformationGatewayForState = new ToDoListInformationBasicInformationGateway(toDoListInformationBasicInformation.Data);
                    string state = toDoListInformationBasicInformationGatewayForState.GetState(currentToDoId);
                    if (state == "Completed") hdfCompleted.Value = "True";

                    ToDoListInformationActivityInformation toDoListInformationActivityInformation = new ToDoListInformationActivityInformation(toDoListInformationTDS);
                    toDoListInformationActivityInformation.LoadAllByToDoId(currentToDoId, companyId);

                    // Store dataset
                    Session["toDoListInformationTDS"] = toDoListInformationTDS;
                    Session["activityInformation"] = activityInformation;
                }

                // ... toDoList_summary.aspx or toDoList_edit.aspx
                if ((Request.QueryString["source_page"] == "toDoList_summary.aspx") || (Request.QueryString["source_page"] == "toDoList_edit.aspx"))
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // Restore dataset
                    toDoListInformationTDS = (ToDoListInformationTDS)Session["toDoListInformationTDS"];
                    activityInformation = (ToDoListInformationTDS.ActivityInformationDataTable)Session["activityInformation"];

                    if (ViewState["update"].ToString().Trim() == "yes")
                    {
                        ToDoListInformationBasicInformation toDoListInformationBasicInformation = new ToDoListInformationBasicInformation(toDoListInformationTDS);
                        toDoListInformationBasicInformation.LoadAllByToDoId(currentToDoId, companyId);

                        ToDoListInformationBasicInformationGateway toDoListInformationBasicInformationGatewayForState = new ToDoListInformationBasicInformationGateway(toDoListInformationBasicInformation.Data);
                        string state = toDoListInformationBasicInformationGatewayForState.GetState(currentToDoId);
                        if (state == "Completed") hdfCompleted.Value = "True";

                        ToDoListInformationActivityInformation toDoListInformationActivityInformation = new ToDoListInformationActivityInformation(toDoListInformationTDS);
                        toDoListInformationActivityInformation.LoadAllByToDoId(currentToDoId, companyId);

                        // Store dataset
                        Session["toDoListInformationTDS"] = toDoListInformationTDS;
                        Session["activityInformation"] = activityInformation;
                    }
                }

                // Prepare initial data
                // ... for subject
                ToDoListInformationBasicInformationGateway toDoListInformationBasicInformationGateway = new ToDoListInformationBasicInformationGateway();
                toDoListInformationBasicInformationGateway.LoadAllByToDoId(currentToDoId, companyId);
                lblTitleSubjectName.Text = " " + toDoListInformationBasicInformationGateway.GetSubject(currentToDoId);

                // ... Data for current to do list
                LoadToDoListData(currentToDoId, companyId);
            }
            else
            {
                // Restore datasets
                toDoListInformationTDS = (ToDoListInformationTDS)Session["toDoListInformationTDS"];
                activityInformation = (ToDoListInformationTDS.ActivityInformationDataTable)Session["activityInformation"];
            }
        }
        protected void grdToDoList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("activityDataEdit");

            if (Page.IsValid)
            {
                int toDoId = (int)e.Keys["ToDoID"];
                int refId = (int)e.Keys["RefID"];

                int employeeId = 0;
                int loginId = Convert.ToInt32(Session["loginID"]);

                EmployeeGateway employeeGateway = new EmployeeGateway();
                string comment = ((TextBox)grdToDoList.Rows[e.RowIndex].Cells[4].FindControl("tbxCommentsEdit")).Text;
                string employeeFullName = "";

                string type_ = ((Label)grdToDoList.Rows[e.RowIndex].Cells[4].FindControl("lblType")).Text;
                switch (type_)
                {
                    case "AssignUser":
                        employeeId = Int32.Parse(((DropDownList)grdToDoList.Rows[e.RowIndex].Cells[4].FindControl("ddlAssignToTeamMemberEdit")).SelectedValue);
                        employeeGateway.LoadByEmployeeId(employeeId);
                        employeeFullName = employeeGateway.GetFullName(employeeId);
                        break;

                    case "AddComment":
                        employeeId = employeeGateway.GetEmployeIdByLoginId(loginId);
                        EmployeeGateway employeeGatewayForComment = new EmployeeGateway();
                        employeeGatewayForComment.LoadByEmployeeId(employeeId);
                        employeeFullName = employeeGatewayForComment.GetFullName(employeeId);
                        break;

                    case "CloseToDo":
                        employeeId = Int32.Parse(hdfAssignedUser.Value);
                        employeeGateway.LoadByEmployeeId(employeeId);
                        employeeFullName = employeeGateway.GetFullName(employeeId);
                        hdfCompleted.Value = "True";
                        break;

                    case "OnHoldToDo":
                        employeeId = employeeGateway.GetEmployeIdByLoginId(loginId);
                        EmployeeGateway employeeGatewayForOnHold = new EmployeeGateway();
                        employeeGatewayForOnHold.LoadByEmployeeId(employeeId);
                        employeeFullName = employeeGatewayForOnHold.GetFullName(employeeId);
                        break;
                }

                ToDoListInformationActivityInformation model = new ToDoListInformationActivityInformation(toDoListInformationTDS);
                model.Update(toDoId, refId, employeeId, employeeFullName, comment);

                // Store dataset
                Session["toDoListInformationTDS"] = toDoListInformationTDS;
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void grdToDoList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Repair Gridview, if the gridview is edition mode
            if (grdToDoList.EditIndex >= 0)
            {
                grdToDoList.UpdateRow(grdToDoList.EditIndex, true);
            }

            // Delete repairs
            int toDoId = (int)e.Keys["ToDoID"];
            int refId = (int)e.Keys["RefID"];

            ToDoListInformationActivityInformation model = new ToDoListInformationActivityInformation(toDoListInformationTDS);

            // Delete repair
            model.Delete(toDoId, refId);

            // Store dataset
            Session["toDoListInformationTDS"] = toDoListInformationTDS;
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_TODOLIST_VIEW"]) && Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_TODOLIST_DELETE"])))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["toDo_id"] == null) || ((string)Request.QueryString["dashboard"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in toDoList_delete.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfToDoListId.Value = Request.QueryString["toDo_id"].ToString();
                hdfDashboard.Value = Request.QueryString["dashboard"].ToString();
                hdfFmType.Value = "ToDoList";

                // If comming from
                // ... toDoList_navigator2.aspx
                if (Request.QueryString["source_page"] == "toDoList_navigator2.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = "no";

                    toDolistInformationTDS = new ToDoListInformationTDS();
                    toDoListTDS = new ToDoListTDS();

                    int toDolistId = Int32.Parse(hdfToDoListId.Value);
                    int companyId = Int32.Parse(hdfCompanyId.Value);

                    ToDoListInformationBasicInformation toDolistInformationBasicInformation = new ToDoListInformationBasicInformation(toDolistInformationTDS);
                    toDolistInformationBasicInformation.LoadAllByToDoId(toDolistId, companyId);

                    ToDoListInformationActivityInformation toDoListInformationActivityInformation = new ToDoListInformationActivityInformation(toDolistInformationTDS);
                    toDoListInformationActivityInformation.LoadAllByToDoId(toDolistId, companyId);

                    // Store dataset
                    Session["toDolistInformationTDS"] = toDolistInformationTDS;
                    Session["toDoListTDS"] = toDoListTDS;
                }

                // ... toDoList_summary.aspx
                if (Request.QueryString["source_page"] == "toDoList_summary.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // Restore dataset
                    toDoListTDS = (ToDoListTDS)Session["toDoListTDS"];
                    toDolistInformationTDS = (ToDoListInformationTDS)Session["toDolistInformationTDS"];
                }
            }
            else
            {
                // Restore datasets
                toDoListTDS = (ToDoListTDS)Session["toDoListTDS"];
                toDolistInformationTDS = (ToDoListInformationTDS)Session["toDolistInformationTDS"];
            }
        }
        private void Delete()
        {
            Page.Validate();

            if (Page.IsValid)
            {
                // Delete all data for toDolist
                int toDolistId = Int32.Parse(hdfToDoListId.Value);

                ToDoListInformationBasicInformation toDolistInformationBasicInformation = new ToDoListInformationBasicInformation(toDolistInformationTDS);
                toDolistInformationBasicInformation.Delete(toDolistId);

                ToDoListInformationActivityInformation toDoListInformationActivityInformation = new ToDoListInformationActivityInformation(toDolistInformationTDS);
                toDoListInformationActivityInformation.DeleteAll(toDolistId);

                // Update databse
                UpdateDatabase();

                // Store datasets
                Session["toDolistInformationTDS"] = toDolistInformationTDS;

                // Redirect
                string url = "";

                if (Request.QueryString["dashboard"] == "True")
                {
                    url = "./../../FleetManagement/Dashboard/dashboard_login.aspx?source_page=out";
                }
                else
                {
                    url = "./toDoList_navigator2.aspx?source_page=toDoList_delete.aspx&toDo_id=" + hdfToDoListId.Value + GetNavigatorState() + "&update=yes";
                }
                Response.Redirect(url);
            }
        }