private void LoadActivityData(int supportTicketId, int companyId)
        {
            SupportTicketInformationActivityInformation supportTicketInformationActivityInformation = new SupportTicketInformationActivityInformation();
            supportTicketInformationActivityInformation.LoadAllBySupportTicketId(supportTicketId, companyId);
            int lastRefId = supportTicketInformationActivityInformation.GetLastAssignedUserRefId();

            SupportTicketInformationActivityInformationGateway supportTicketInformationActivityInformationGateway = new SupportTicketInformationActivityInformationGateway(supportTicketInformationActivityInformation.Data);
            supportTicketInformationActivityInformationGateway.LoadAllBySupportTicketId(supportTicketId, companyId);

            if (supportTicketInformationActivityInformationGateway.Table.Rows.Count > 0)
            {
                // For last assigned user
                tbxAssignedUser.Text = supportTicketInformationActivityInformationGateway.GetEmployeeFullName(supportTicketId, lastRefId);
                hdfAssignedUser.Value = supportTicketInformationActivityInformationGateway.GetEmployeeID(supportTicketId, lastRefId).ToString();
            }
        }
        /// <summary>
        /// Save all activities to database (direct)
        /// </summary>
        /// <param name="companyId">companyId</param>
        public void Save(int companyId)
        {
            SupportTicketInformationTDS supportTicketInformationChanges = (SupportTicketInformationTDS)Data.GetChanges();

            if (supportTicketInformationChanges != null)
            {
                if (supportTicketInformationChanges.ActivityInformation.Rows.Count > 0)
                {
                    SupportTicketInformationActivityInformationGateway supportTicketInformationActivityInformationGateway = new SupportTicketInformationActivityInformationGateway(supportTicketInformationChanges);

                    foreach (SupportTicketInformationTDS.ActivityInformationRow row in (SupportTicketInformationTDS.ActivityInformationDataTable)supportTicketInformationChanges.ActivityInformation)
                    {
                        // Insert new activity
                        if ((!row.Deleted) && (!row.InDataBase))
                        {
                            // new values
                            int supportTicketId = row.SupportTicketID;
                            int refId = row.RefID;

                            int employeeId = supportTicketInformationActivityInformationGateway.GetEmployeeID(supportTicketId, refId);
                            string type_ = supportTicketInformationActivityInformationGateway.GetType_(supportTicketId, refId);
                            DateTime dateTime_ = supportTicketInformationActivityInformationGateway.GetDateTime_(supportTicketId, refId);
                            string comment = supportTicketInformationActivityInformationGateway.GetComment(supportTicketId, refId);

                            SupportTicketSupportTicketActivity supportTicketSupportTicketActivity = new SupportTicketSupportTicketActivity(null);
                            supportTicketSupportTicketActivity.InsertDirect(supportTicketId, refId, type_, dateTime_, employeeId, comment, row.Deleted, row.COMPANY_ID);
                        }

                        // Update activities
                        if ((!row.Deleted) && (row.InDataBase))
                        {
                            int supportTicketId = row.SupportTicketID;
                            int refId = row.RefID;
                            bool originalDeleted = false;
                            int originalCompanyId = companyId;

                            // original values
                            int originalEmployeeId = supportTicketInformationActivityInformationGateway.GetEmployeeID(supportTicketId, refId);
                            string originalType_ = supportTicketInformationActivityInformationGateway.GetType_(supportTicketId, refId);
                            DateTime originalDateTime_ = supportTicketInformationActivityInformationGateway.GetDateTime_(supportTicketId, refId);
                            string originalComment = supportTicketInformationActivityInformationGateway.GetComment(supportTicketId, refId);

                            // new values
                            int newEmployeeId = supportTicketInformationActivityInformationGateway.GetEmployeeIDOriginal(supportTicketId, refId);
                            string newComment = supportTicketInformationActivityInformationGateway.GetCommentOriginal(supportTicketId, refId);

                            SupportTicketSupportTicketActivity supportTicketSupportTicketActivity = new SupportTicketSupportTicketActivity(null);
                            supportTicketSupportTicketActivity.UpdateDirect(supportTicketId, refId, originalType_, originalDateTime_, originalEmployeeId, originalComment, originalDeleted, originalCompanyId, supportTicketId, refId, originalType_, originalDateTime_, newEmployeeId, newComment, originalDeleted, originalCompanyId);
                        }

                        // Deleted activity
                        if ((row.Deleted) && (row.InDataBase))
                        {
                            SupportTicketSupportTicketActivity supportTicketSupportTicketActivity = new SupportTicketSupportTicketActivity(null);
                            supportTicketSupportTicketActivity.DeleteDirect(row.SupportTicketID, row.RefID, row.COMPANY_ID);
                        }
                    }
                }
            }
        }
        protected void grdSupportTicket_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Control of footer row
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                int loginId = Convert.ToInt32(Session["loginID"]);
                EmployeeGateway employeeGateway = new EmployeeGateway();
                int employeeId = employeeGateway.GetEmployeIdByLoginId(loginId);

                // Select action
                DropDownList actions = (DropDownList)e.Row.FindControl("ddlActionsNew");
                actions.SelectedValue = hdfAction.Value;

                // Only the assigned user could close the todo
                if (hdfAssignedUser.Value != employeeId.ToString())
                {
                    actions.Items.Remove("Close");
                }

                // Make fields visible for actions
                if (hdfAction.Value == "Assign To User")
                {
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberNew")).Visible = true;
                    ((CustomValidator)e.Row.FindControl("cvTeamMemberNew")).Visible = true;
                    ((TextBox)e.Row.FindControl("tbxUserNew")).Visible = false;
                    ((Label)e.Row.FindControl("lblClosed")).Visible = false;
                    ((Label)e.Row.FindControl("lblUserNew")).Text = "Assign To";
                }

                // Make fields visible for actions
                if (hdfAction.Value == "Assign To Owner")
                {
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberNew")).Visible = true;
                    ((CustomValidator)e.Row.FindControl("cvTeamMemberNew")).Visible = true;
                    ((TextBox)e.Row.FindControl("tbxUserNew")).Visible = false;
                    ((Label)e.Row.FindControl("lblClosed")).Visible = false;
                    ((Label)e.Row.FindControl("lblUserNew")).Text = "Assign To";

                    int currentSupportTicketId = Int32.Parse(hdfSupportTicketId.Value.ToString());
                    SupportTicketInformationBasicInformationGateway supportTicketInformationBasicInformationGatewayForState = new SupportTicketInformationBasicInformationGateway(supportTicketInformationTDS);
                    int ownerId = supportTicketInformationBasicInformationGatewayForState.GetCreatedByID(currentSupportTicketId);
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberNew")).SelectedValue = ownerId.ToString();
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberNew")).Enabled = false;
                }

                if (hdfAction.Value == "Add Comment")
                {
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberNew")).Visible = false;
                    ((CustomValidator)e.Row.FindControl("cvTeamMemberNew")).Visible = false;
                    ((TextBox)e.Row.FindControl("tbxUserNew")).Visible = true;

                    EmployeeGateway employeeGatewayForUser = new EmployeeGateway();
                    employeeGatewayForUser.LoadByEmployeeId(employeeId);
                    ((TextBox)e.Row.FindControl("tbxUserNew")).Text = employeeGatewayForUser.GetFullName(employeeId);
                    ((Label)e.Row.FindControl("lblClosed")).Visible = false;
                    ((Label)e.Row.FindControl("lblUserNew")).Text = "Comment Added By";
                }

                if (hdfAction.Value == "Complete")
                {
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberNew")).Visible = false;
                    ((CustomValidator)e.Row.FindControl("cvTeamMemberNew")).Visible = false;
                    ((TextBox)e.Row.FindControl("tbxUserNew")).Visible = true;

                    EmployeeGateway employeeGatewayForUser = new EmployeeGateway();
                    employeeGatewayForUser.LoadByEmployeeId(Int32.Parse(hdfAssignedUser.Value));
                    ((TextBox)e.Row.FindControl("tbxUserNew")).Text = employeeGatewayForUser.GetFullName(Int32.Parse(hdfAssignedUser.Value));
                    ((Label)e.Row.FindControl("lblClosed")).Visible = false;

                    ((Label)e.Row.FindControl("lblUserNew")).Text = "Support Ticket Completed By";
                }

                if (hdfCompleted.Value == "True")
                {
                    ((DropDownList)e.Row.FindControl("ddlActionsNew")).Visible = false;
                    ((TextBox)e.Row.FindControl("tbxDateTimeNew")).Visible = false;
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberNew")).Visible = false;
                    ((CustomValidator)e.Row.FindControl("cvTeamMemberNew")).Visible = false;
                    ((TextBox)e.Row.FindControl("tbxUserNew")).Visible = false;
                    ((TextBox)e.Row.FindControl("tbxCommentsNew")).Visible = false;
                    ((Label)e.Row.FindControl("lblActionNew")).Visible = false;
                    ((Label)e.Row.FindControl("lblDateTimeNew")).Visible = false;
                    ((Label)e.Row.FindControl("lblUserNew")).Visible = false;
                    ((Label)e.Row.FindControl("lblCommentsNew")).Visible = false;
                    ((ImageButton)e.Row.FindControl("ibtnAdd")).Visible = false;
                    ((Label)e.Row.FindControl("lblClosed")).Visible = true;
                }

                if (hdfAction.Value == "Put On Hold")
                {
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberNew")).Visible = false;
                    ((CustomValidator)e.Row.FindControl("cvTeamMemberNew")).Visible = false;
                    ((TextBox)e.Row.FindControl("tbxUserNew")).Visible = true;

                    EmployeeGateway employeeGatewayForOnHold = new EmployeeGateway();
                    employeeGatewayForOnHold.LoadByEmployeeId(employeeId);
                    ((TextBox)e.Row.FindControl("tbxUserNew")).Text = employeeGatewayForOnHold.GetFullName(employeeId);
                    ((Label)e.Row.FindControl("lblClosed")).Visible = false;
                    ((Label)e.Row.FindControl("lblUserNew")).Text = "On Hold By";
                }

                // Date and time
                DateTime creationDate = DateTime.Now;
                ((TextBox)e.Row.FindControl("tbxDateTimeNew")).Text = creationDate.ToString();
            }

            // Control of edit rows
            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))))
            {
                // Date and time
                DateTime creationDate = DateTime.Now;
                ((TextBox)e.Row.FindControl("tbxDateTimeEdit")).Text = creationDate.ToString();
                string type_ = ((Label)e.Row.FindControl("lblType")).Text;
                int loginId = Convert.ToInt32(Session["loginID"]);
                EmployeeGateway employeeGateway = new EmployeeGateway();
                int employeeId = employeeGateway.GetEmployeIdByLoginId(loginId);
                int supportTicketId = Int32.Parse(((Label)e.Row.FindControl("lblSupportTicketID")).Text);
                int refId = Int32.Parse(((Label)e.Row.FindControl("lblRefID")).Text);

                SupportTicketInformationActivityInformationGateway supportTicketInformationActivityInformationGateway = new SupportTicketInformationActivityInformationGateway(supportTicketInformationTDS);

                // Make fields visible for actions
                if (type_ == "AssignUser")
                {
                    int employeeIdForAssign = supportTicketInformationActivityInformationGateway.GetEmployeeID(supportTicketId, refId);
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberEdit")).Visible = true;
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberEdit")).SelectedValue = employeeIdForAssign.ToString();
                    ((CustomValidator)e.Row.FindControl("cvTeamMemberEdit")).Visible = true;
                    ((TextBox)e.Row.FindControl("tbxUserEdit")).Visible = false;
                    ((Label)e.Row.FindControl("lblUserEdit")).Text = "Assign To";
                }

                if (type_ == "AddComment")
                {
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberEdit")).Visible = false;
                    ((CustomValidator)e.Row.FindControl("cvTeamMemberEdit")).Visible = false;
                    ((TextBox)e.Row.FindControl("tbxUserEdit")).Visible = true;

                    EmployeeGateway employeeGatewayForUser = new EmployeeGateway();
                    employeeGatewayForUser.LoadByEmployeeId(employeeId);
                    ((TextBox)e.Row.FindControl("tbxUserEdit")).Text = employeeGatewayForUser.GetFullName(employeeId);
                    ((Label)e.Row.FindControl("lblUserEdit")).Text = "Comment Added By";
                }

                if (type_ == "CloseTicket")
                {
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberEdit")).Visible = false;
                    ((CustomValidator)e.Row.FindControl("cvTeamMemberEdit")).Visible = false;
                    ((TextBox)e.Row.FindControl("tbxUserEdit")).Visible = true;

                    EmployeeGateway employeeGatewayForUser = new EmployeeGateway();
                    employeeGatewayForUser.LoadByEmployeeId(Int32.Parse(hdfAssignedUser.Value));
                    ((TextBox)e.Row.FindControl("tbxUserEdit")).Text = employeeGatewayForUser.GetFullName(Int32.Parse(hdfAssignedUser.Value));
                    ((Label)e.Row.FindControl("lblUserEdit")).Text = "Support Ticket Completed By";
                }

                if (type_ == "OnHold")
                {
                    ((DropDownList)e.Row.FindControl("ddlAssignToTeamMemberEdit")).Visible = false;
                    ((CustomValidator)e.Row.FindControl("cvTeamMemberEdit")).Visible = false;
                    ((TextBox)e.Row.FindControl("tbxUserEdit")).Visible = true;

                    EmployeeGateway employeeGatewayForOnHold = new EmployeeGateway();
                    employeeGatewayForOnHold.LoadByEmployeeId(employeeId);
                    ((TextBox)e.Row.FindControl("tbxUserEdit")).Text = employeeGatewayForOnHold.GetFullName(employeeId);
                    ((Label)e.Row.FindControl("lblUserEdit")).Text = "On Hold By";
                }
            }

            // Control normal rows
            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Normal) || (e.Row.RowState == (DataControlRowState.Normal | DataControlRowState.Alternate))))
            {
                // Validate admin actions
                if ((Convert.ToBoolean(Session["sgLFS_ITTST_SUPPORTTICKET_ADMIN"])))
                {
                    ((ImageButton)e.Row.FindControl("ibtnEdit")).Visible = true;
                    ((ImageButton)e.Row.FindControl("ibtnDelete")).Visible = true;
                }
                else
                {
                    bool inDataBase = bool.Parse(((Label)e.Row.FindControl("lblInDataBase")).Text);
                    if (inDataBase)
                    {
                        ((ImageButton)e.Row.FindControl("ibtnEdit")).Visible = false;
                        ((ImageButton)e.Row.FindControl("ibtnDelete")).Visible = false;
                    }
                    else
                    {
                        ((ImageButton)e.Row.FindControl("ibtnEdit")).Visible = true;
                        ((ImageButton)e.Row.FindControl("ibtnDelete")).Visible = true;
                    }
                }

                // Change label text
                string type_ = ((Label)e.Row.FindControl("lblType")).Text;
                if (type_ == "AssignUser")
                {
                    ((Label)e.Row.FindControl("lblUser")).Text = "Assign To";
                }

                if (type_ == "AddComment")
                {
                    ((Label)e.Row.FindControl("lblUser")).Text = "Comment Added By";
                }

                if (type_ == "CloseTicket")
                {
                    ((Label)e.Row.FindControl("lblUser")).Text = "Support Ticket Completed By";
                }

                if (type_ == "OnHold")
                {
                    ((Label)e.Row.FindControl("lblUser")).Text = "On Hold By";
                }
            }
        }
 // ////////////////////////////////////////////////////////////////////////
 // PUBLIC METHODS
 //
 /// <summary>
 /// LoadAllBySupportTicketId
 /// </summary>
 /// <param name="supportTicketId">supportTicketId</param>              
 /// <param name="companyId">companyId</param>
 public void LoadAllBySupportTicketId(int supportTicketId, int companyId)
 {
     SupportTicketInformationActivityInformationGateway supportTicketInformationActivityInformationGateway = new SupportTicketInformationActivityInformationGateway(Data);
     supportTicketInformationActivityInformationGateway.LoadAllBySupportTicketId(supportTicketId, companyId);
 }