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

                // Update comments
                ManholeRehabilitationCommentDetails manholeRehabilitationCommentDetails = new ManholeRehabilitationCommentDetails(manholeRehabilitationTDS);
                manholeRehabilitationCommentDetails.Save(companyId);

                // Update works
                workUpdate();

                // Update section
                int workId = Int32.Parse(hdfWorkId.Value.Trim());
                ManholeRehabilitationWorkDetails fullLengthLiningWorkDetails = new ManholeRehabilitationWorkDetails(manholeRehabilitationTDS);
                fullLengthLiningWorkDetails.UpdateCommentsForSummaryEdit(workId, companyId);

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

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

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        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 toHistory = false;
                    bool deleted = false;
                    string workType = hdfWorkType.Value;

                    string newSubject = ((TextBox)grdComments.FooterRow.FindControl("tbxSubjectNew")).Text.Trim();
                    string newType = ((DropDownList)grdComments.FooterRow.FindControl("ddlTypeNew")).SelectedValue.ToString().Trim();
                    string newComment = ((TextBox)grdComments.FooterRow.FindControl("tbxCommentsNew")).Text.Trim();
                    int? libraryFilesId = null; if (((Label)grdComments.FooterRow.FindControl("lblLIBRARY_FILES_IDNew")).Text != "") libraryFilesId = Int32.Parse(((Label)grdComments.FooterRow.FindControl("lblLIBRARY_FILES_IDNew")).Text.Trim());

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

                    ManholeRehabilitationCommentDetails model = new ManholeRehabilitationCommentDetails(manholeRehabilitationTDS);
                    model.Insert(workId, newType, newSubject, loginId, dateTime_, newComment, libraryFilesId, deleted, companyId, inDatabase, userFullName, toHistory, workType);

                    Session.Remove("manholeRehabilitationCommentDetailsDummy");
                    Session["manholeRehabilitationTDS"] = manholeRehabilitationTDS;
                    Session["manholeRehabilitationCommentDetails"] = manholeRehabilitationTDS.CommentDetails;

                    grdComments.DataBind();
                    grdComments.PageIndex = grdComments.PageCount - 1;
                }
            }
        }
        protected void grdComments_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_MANHOLEREHABILITATION_ADMIN"]);
                bool toHistory = false;

                string newSubject = ((TextBox)grdComments.Rows[e.RowIndex].Cells[3].FindControl("tbxSubjectEdit")).Text.Trim();
                string newComment = ((TextBox)grdComments.Rows[e.RowIndex].Cells[4].FindControl("tbxCommentsEdit")).Text.Trim();
                string newType = "";
                if (((DropDownList)grdComments.Rows[e.RowIndex].Cells[3].FindControl("ddlTypeEdit")).Visible)
                {
                    newType = ((DropDownList)grdComments.Rows[e.RowIndex].Cells[3].FindControl("ddlTypeEdit")).SelectedValue.ToString().Trim();
                }
                else
                {
                    newType = ((TextBox)grdComments.Rows[e.RowIndex].Cells[3].FindControl("tbxTypeEdit")).Text;
                }

                // Update data
                ManholeRehabilitationCommentDetails model = new ManholeRehabilitationCommentDetails(manholeRehabilitationTDS);
                model.Update(workId, refId, newType, newSubject, companyId, newComment, loginId, adminPermission, toHistory);

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

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_CWP_MANHOLEREHABILITATION_VIEW"]) && Convert.ToBoolean(Session["sgLFS_CWP_MANHOLEREHABILITATION_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["work_id"] == null) || ((string)Request.QueryString["asset_id"] == null) || ((string)Request.QueryString["in_project"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in mr_comments.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfCurrentProjectId.Value = Request.QueryString["project_id"].ToString();
                hdfCurrentClientId.Value = Request.QueryString["client_id"].ToString();
                hdfInProject.Value = Request.QueryString["in_project"].ToString();
                hdfLoginId.Value = Convert.ToInt32(Session["loginID"]).ToString();
                hdfAdminPermission.Value = Convert.ToBoolean(Session["sgLFS_CWP_MANHOLEREHABILITATION_ADMIN"]).ToString();
                hdfWorkType.Value = "Manhole Rehabilitation";
                hdfWorkId.Value = Request.QueryString["work_id"].ToString();
                hdfAssetId.Value = Request.QueryString["asset_id"].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);

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

                // If coming from
                // ... mr_summary.aspx and mr_edit.aspx
                if (Request.QueryString["source_page"] == "mr_summary.aspx" || Request.QueryString["source_page"] == "mr_edit.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    manholeRehabilitationTDS = (ManholeRehabilitationTDS)Session["manholeRehabilitationTDS"];

                    int assetId = Int32.Parse(hdfAssetId.Value.Trim());
                    int currentProjectId = Int32.Parse(hdfCurrentProjectId.Value.Trim());
                    int workId = Int32.Parse(hdfWorkId.Value.Trim());

                    // ... If the project has works
                    if (workId != 0)
                    {
                        ManholeRehabilitationCommentDetailsGateway manholeRehabilitationCommentDetailsGateway = new ManholeRehabilitationCommentDetailsGateway(manholeRehabilitationTDS);
                        manholeRehabilitationCommentDetailsGateway.LoadAllByWorkIdWorkType(workId,companyId, "Manhole Rehabilitation");

                        ManholeRehabilitationCommentDetails manholeRehabilitationCommentDetailsForLoad = new ManholeRehabilitationCommentDetails(manholeRehabilitationCommentDetailsGateway.Data);
                        manholeRehabilitationCommentDetailsForLoad.UpdateForProcess();
                    }

                    // ... Store datasets
                    Session["manholeRehabilitationTDS"] = manholeRehabilitationTDS;
                    Session["manholeRehabilitationCommentDetails"] = manholeRehabilitationTDS.CommentDetails;
                }
            }
            else
            {
                // Restore datasets
                manholeRehabilitationTDS = (ManholeRehabilitationTDS)Session["manholeRehabilitationTDS"];
                manholeRehabilitationCommentDetails = manholeRehabilitationTDS.CommentDetails;

                // Store
                Session["manholeRehabilitationCommentDetails"] = manholeRehabilitationTDS.CommentDetails;
            }
        }
        protected void grdComments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Comments Gridview, if the gridview is edition mode
            if (grdComments.EditIndex >= 0)
            {
                grdComments.UpdateRow(grdComments.EditIndex, true);
            }

            // Delete comments
            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_MANHOLEREHABILITATION_ADMIN"]);

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

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