private void UpdateDatabase()
        {
            DB.Open();
            DB.BeginTransaction();
            try
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);

                // Update Comments and History
                FlatSectionJlCommentDetails flatSectionJlCommentDetails = new FlatSectionJlCommentDetails(flatSectionJlTDS);
                flatSectionJlCommentDetails.Save(companyId);

                FlatSectionJlHistoryDetails flatSectionJlHistoryDetails = new FlatSectionJlHistoryDetails(flatSectionJlTDS);
                flatSectionJlHistoryDetails.Save(companyId);

                // Update FlatSectionJl for new Comments
                int workId = Int32.Parse(hdfWorkId.Value.Trim());
                string workType = "Junction Lining Lateral";

                FlatSectionJl flatSectionJl = new FlatSectionJl(flatSectionJlTDS);
                flatSectionJl.UpdateCommentsHistoryForSummaryEdit(workId, workType, companyId);

                JlNavigator jlNavigator = new JlNavigator(jlNavigatorTDS);
                jlNavigator.UpdateComments(workId, workType, companyId);

                Session["flatSectionJlTDS"] = flatSectionJlTDS;
                Session["jlNavigatorTDS"] = jlNavigatorTDS;

                DB.CommitTransaction();
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        /// <summary>
        /// Transfer to History
        /// </summary>
        /// <param name="loginId">loginId</param>
        /// <param name="adminPermission">admin Permission</param>
        /// <param name="workHistory">workHistory</param>
        public void TransferToHistory(int loginId, bool adminPermission, FlatSectionJlHistoryDetails flatSectionJlHistoryDetails)
        {
            int? libraryFilesId = null;

            foreach (FlatSectionJlTDS.FlatSectionJlCommentDetailsRow commentRow in (FlatSectionJlTDS.FlatSectionJlCommentDetailsDataTable)Table)
            {
                if (adminPermission)
                {
                    if (!commentRow.IsToHistoryNull())
                    {
                        if ((commentRow.ToHistory) && (!commentRow.Deleted))
                        {
                            if (!commentRow.IsLIBRARY_FILES_IDNull())
                            {
                                libraryFilesId = commentRow.LIBRARY_FILES_ID;
                            }
                            else
                            {
                                libraryFilesId = null;
                            }
                            string workType = ""; if (!commentRow.IsWorkTypeNull()) workType = commentRow.WorkType;
                            // Insert to history
                            flatSectionJlHistoryDetails.Insert(commentRow.WorkID, commentRow.Type, commentRow.Subject, commentRow.UserID, commentRow.DateTime_, commentRow.Comment, libraryFilesId, commentRow.Deleted, commentRow.COMPANY_ID, false, commentRow.UserFullName, commentRow.ToHistory, workType);

                            // Delete from comments
                            commentRow.Deleted = true;
                        }
                    }
                }
                else
                {
                    if (commentRow.UserID == loginId)
                    {
                        if (!commentRow.IsToHistoryNull())
                        {
                            if ((commentRow.ToHistory) && (!commentRow.Deleted))
                            {
                                if (!commentRow.IsLIBRARY_FILES_IDNull())
                                {
                                    libraryFilesId = commentRow.LIBRARY_FILES_ID;
                                }
                                else
                                {
                                    libraryFilesId = null;
                                }
                                string workType = ""; if (!commentRow.IsWorkTypeNull()) workType = commentRow.WorkType;
                                // Insert to history
                                flatSectionJlHistoryDetails.Insert(commentRow.WorkID, commentRow.Type, commentRow.Subject, commentRow.UserID, commentRow.DateTime_, commentRow.Comment, libraryFilesId, commentRow.Deleted, commentRow.COMPANY_ID, false, commentRow.UserFullName, false, workType);

                                // Delete from comments
                                commentRow.Deleted = true;
                            }
                        }
                    }
                }
            }
        }
        private void TransferToHistory()
        {
            int loginId = Convert.ToInt32(hdfLoginId.Value);
            bool adminPermission = Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_ADMIN"]);

            FlatSectionJlCommentDetails flatSectionJlCommentDetails = new FlatSectionJlCommentDetails(flatSectionJlTDS);
            FlatSectionJlHistoryDetails flatSectionJlHistoryDetails = new FlatSectionJlHistoryDetails(flatSectionJlTDS);

            flatSectionJlCommentDetails.TransferToHistory(loginId, adminPermission, flatSectionJlHistoryDetails);
        }
        private void GrdCommentsAdd()
        {
            if (ValidateCommentsFooter())
            {
                Page.Validate("commentsDataAdd");
                if (Page.IsValid)
                {
                    int workId = Int32.Parse(hdfWorkId.Value);
                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    int loginId = Convert.ToInt32(Session["loginID"]);
                    DateTime dateTime_ = DateTime.Now;
                    bool inDatabase = false;
                    bool deleted = false;
                    string workType = hdfWorkType.Value;

                    string newSubject = ((TextBox)grdHistory.FooterRow.FindControl("tbxSubjectNew")).Text.Trim();
                    string newType = "Junction Lining Lateral";
                    string newHistory = ((TextBox)grdHistory.FooterRow.FindControl("tbxHistoryNew")).Text.Trim();
                    int? libraryFilesId = null; if (((Label)grdHistory.FooterRow.FindControl("lblLIBRARY_FILES_IDNew")).Text != "") libraryFilesId = Int32.Parse(((Label)grdHistory.FooterRow.FindControl("lblLIBRARY_FILES_IDNew")).Text.Trim());
                    bool toComments = ((CheckBox)grdHistory.FooterRow.FindControl("ckbxToCommentsNew")).Checked;

                    LoginGateway loginGateway = new LoginGateway();
                    loginGateway.LoadByLoginId(loginId, companyId);
                    string userFullName = loginGateway.GetLastName(loginId, companyId) + " " + loginGateway.GetFirstName(loginId, companyId);

                    FlatSectionJlHistoryDetails model = new FlatSectionJlHistoryDetails(flatSectionJlTDS);
                    model.Insert(workId, newType, newSubject, loginId, dateTime_, newHistory, libraryFilesId, deleted, companyId, inDatabase, userFullName, toComments, workType);

                    Session.Remove("flatSectionJlHistoryDetailsDummy");
                    Session["flatSectionJlTDS"] = flatSectionJlTDS;
                    Session["flatSectionJlHistoryDetails"] = flatSectionJlTDS.FlatSectionJlHistoryDetails;

                    grdHistory.DataBind();
                    grdHistory.PageIndex = grdHistory.PageCount - 1;
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_VIEW"]) && Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_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["client_id"] == null) || ((string)Request.QueryString["project_id"] == null) || ((string)Request.QueryString["row_asset_id"] == null) || ((string)Request.QueryString["row_focus"] == null) || ((string)Request.QueryString["work_type"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in jl_history.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfCurrentProjectId.Value = Request.QueryString["project_id"].ToString();
                hdfCurrentClientId.Value = Request.QueryString["client_id"].ToString();
                hdfLoginId.Value = Convert.ToInt32(Session["loginID"]).ToString();
                hdfAssetId.Value = Convert.ToString(Request.QueryString["row_asset_id"]);
                hdfAdminPermission.Value = Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_ADMIN"]).ToString();
                hdfWorkType.Value = Request.QueryString["work_type"].ToString();
                hdfSection_.Value = Request.QueryString["section_"].ToString();
                hdfUpdate.Value = "yes";

                // ... Names for UserList
                string workType = hdfWorkType.Value.Trim();
                int companyId = Int32.Parse(hdfCompanyId.Value);

                LoginGateway loginGateway = new LoginGateway();
                loginGateway.LoadByLoginId(Convert.ToInt32(hdfLoginId.Value), companyId);
                hdfCreatedBy.Value = loginGateway.GetLastName(Convert.ToInt32(hdfLoginId.Value), companyId) + " " + loginGateway.GetFirstName(Convert.ToInt32(hdfLoginId.Value), companyId);
                hdfCreationDateTime.Value = DateTime.Now.ToString();

                // Prepare initial data
                Session.Remove("flatSectionJlHistoryDetailsDummy");

                // If comming from
                // ... flat_section_jl_summary.aspx or flat_section_jl_edit.aspx
                if (Request.QueryString["source_page"] == "flat_section_jl_summary.aspx" || Request.QueryString["source_page"] == "flat_section_jl_edit.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];
                    Session["rowFocus"] = Convert.ToInt32(Request.QueryString["row_focus"].ToString());

                    // ... Load comments to edit
                    flatSectionJlTDS = (FlatSectionJlTDS)Session["flatSectionJlTDS"];
                    jlNavigatorTDS = (JlNavigatorTDS)Session["jlNavigatorTDS"];

                    // Get workId
                    int assetId = Int32.Parse(hdfAssetId.Value.Trim());
                    int currentProjectId = Int32.Parse(hdfCurrentProjectId.Value.Trim());
                    int jlWorkId = 0;
                    int raWorkId = 0;
                    int flWorkId = 0;

                    WorkGateway searchWorkGateway = new WorkGateway();
                    searchWorkGateway.LoadByProjectIdAssetIdWorkType(currentProjectId, assetId, "Junction Lining Lateral", companyId);
                    if (searchWorkGateway.Table.Rows.Count > 0)
                    {
                        jlWorkId = searchWorkGateway.GetWorkId(assetId, "Junction Lining Lateral", currentProjectId);

                        // ... Tag Page
                        hdfWorkId.Value = jlWorkId.ToString().Trim();

                        // ... ...Get ra work id for comments
                        int section_ = Int32.Parse(hdfSection_.Value);
                        WorkGateway workGatewayRAFL = new WorkGateway();

                        // ... ...Get fl work id for comments
                        workGatewayRAFL.LoadByProjectIdAssetIdWorkType(currentProjectId, section_, "Full Length Lining", companyId);
                        if (workGatewayRAFL.Table.Rows.Count > 0)
                        {
                            flWorkId = workGatewayRAFL.GetWorkId(section_, "Full Length Lining", currentProjectId);
                        }

                        workGatewayRAFL.LoadByProjectIdAssetIdWorkType(currentProjectId, section_, "Rehab Assessment", companyId);
                        if (workGatewayRAFL.Table.Rows.Count > 0)
                        {
                            raWorkId = workGatewayRAFL.GetWorkId(section_, "Rehab Assessment", currentProjectId);
                        }
                        // Get history
                        FlatSectionJlHistoryDetailsGateway flatSectionJlHistoryDetailsGateway = new FlatSectionJlHistoryDetailsGateway(flatSectionJlTDS);
                        flatSectionJlHistoryDetailsGateway.LoadAllByJlWorkIdRaWorkIdFlWorkId(jlWorkId, raWorkId, flWorkId, companyId);

                        FlatSectionJlHistoryDetails flatSectionJlHistoryDetails = new FlatSectionJlHistoryDetails(flatSectionJlTDS);
                        flatSectionJlHistoryDetails.UpdateForProcess();

                        // Get comments
                        FlatSectionJlCommentDetailsGateway flatSectionJlCommentDetailsGateway = new FlatSectionJlCommentDetailsGateway(flatSectionJlTDS);
                        flatSectionJlCommentDetailsGateway.LoadAllByJlWorkIdRaWorkIdFlWorkId(jlWorkId, raWorkId, flWorkId, companyId);
                    }

                    // ... Store datasets
                    Session["flatSectionJlTDS"] = flatSectionJlTDS;
                    Session["jlNavigatorTDS"] = jlNavigatorTDS;
                    Session["flatSectionJlHistoryDetails"] = flatSectionJlTDS.FlatSectionJlHistoryDetails;
                }
            }
            else
            {
                // Restore datasets
                flatSectionJlTDS = (FlatSectionJlTDS)Session["flatSectionJlTDS"];
                jlNavigatorTDS = (JlNavigatorTDS)Session["jlNavigatorTDS"];
                flatSectionJlHistoryDetails = flatSectionJlTDS.FlatSectionJlHistoryDetails;

                // Store
                Session["flatSectionJlHistoryDetails"] = flatSectionJlTDS.FlatSectionJlHistoryDetails;
            }
        }
        protected void grdHistory_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("commentsDataEdit");
            if (Page.IsValid)
            {
                int workId = (int)e.Keys["WorkID"];
                int refId = (int)e.Keys["RefID"];
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int loginId = Convert.ToInt32(Session["loginID"]);
                bool adminPermission = Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_ADMIN"]);
                string type = ((Label)grdHistory.Rows[e.RowIndex].Cells[4].FindControl("lblType")).Text.Trim();

                string newSubject = ((TextBox)grdHistory.Rows[e.RowIndex].Cells[5].FindControl("tbxSubjectEdit")).Text.Trim();
                string newComment = ((TextBox)grdHistory.Rows[e.RowIndex].Cells[5].FindControl("tbxHistoryEdit")).Text.Trim();
                bool toComments = ((CheckBox)grdHistory.Rows[e.RowIndex].Cells[5].FindControl("ckbxToCommentsEdit")).Checked;

                // Update data
                FlatSectionJlHistoryDetails model = new FlatSectionJlHistoryDetails(flatSectionJlTDS);
                model.Update(workId, refId, type, newSubject, companyId, newComment, loginId, adminPermission, toComments);

                // Store dataset
                Session["flatSectionJlTDS"] = flatSectionJlTDS;
                Session["flatSectionJlHistoryDetails"] = flatSectionJlTDS.FlatSectionJlHistoryDetails;
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void grdHistory_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Comments Gridview, if the gridview is edition mode
            if (grdHistory.EditIndex >= 0)
            {
                grdHistory.UpdateRow(grdHistory.EditIndex, true);
            }

            // Delete history
            int workId = (int)e.Keys["WorkID"];
            int refId = (int)e.Keys["RefID"];
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int loginId = Convert.ToInt32(hdfLoginId.Value);
            bool adminPermission = Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_ADMIN"]);

            // Delete comment
            FlatSectionJlHistoryDetails model = new FlatSectionJlHistoryDetails(flatSectionJlTDS);
            model.Delete(workId, refId, companyId, loginId, adminPermission);

            // Store dataset
            Session["flatSectionJlTDS"] = flatSectionJlTDS;
        }