protected void gridViewStageMapping_Command(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                GetMyAccessRights();

                modalBtnDelete.Visible = myAccessRights.CanDelete == null ? false : Convert.ToBoolean(myAccessRights.CanDelete);

                modalLabelError.Text = "";
                modalLabelError.Visible = false;
                int index = Convert.ToInt32(e.CommandArgument);
                int stageMappingId = Convert.ToInt32(((Label)gridViewStageMapping.Rows[index].FindControl("labelStageMappingId")).Text);
                modalLabelStageMappingId.Text = stageMappingId.ToString();

                CAPStageMapping capStageMapping = new CAPStageMapping();
                capStageMapping = capStageMapping.GetCapStageMapping(stageMappingId);

                InitializeModalDropDownDepartment(capStageMapping.DepartmentId.ToString());
                InitializeModalDropDownJobType(Convert.ToInt32(modalDropDownDepartment.SelectedItem.Value),capStageMapping.JobTypeId.ToString().Trim());
                InitializeModalDatabase(capStageMapping.DatabaseMap.Trim());
                InitializeModalCAPStages(modalDropDownDatabase.SelectedItem.Value.Trim(),capStageMapping.SD_Stage_No.ToString().Trim());
                modalBtnSubmit.Visible = Convert.ToBoolean(myAccessRights.CanUpdate);
                modalBtnSubmit.CommandArgument = "Update";
                programmaticModalPopup.Show();
            }
        }
        protected void InitializeModalCAPStages(string databasemap,string selectedStage = "")
        {
            CAPStageMapping capstagemapping = new CAPStageMapping();
            List<CAPStageMapping> capStageList = new List<CAPStageMapping>();
            capStageList = capstagemapping.GetCAPStagesByCAPDatabase(databasemap);

            modalDropDownCapStage.DataSource = capStageList;
            modalDropDownCapStage.DataTextField = "StageDescription";
            modalDropDownCapStage.DataValueField = "SD_Stage_No";
            modalDropDownCapStage.DataBind();

            if (selectedStage != null && selectedStage.Trim() != "")
            {
                foreach (ListItem i in modalDropDownCapStage.Items)
                {
                    if (i.Value.Trim() == selectedStage.Trim())
                    {
                        i.Selected = true;
                        break;
                    }
                }
            }
        }
        protected void modalBtnSubmit_Command(object sender, CommandEventArgs e)
        {
            CAPStageMapping capstageMapping = new CAPStageMapping();

            bool haserror = false;
            if (e.CommandArgument.ToString().Trim() == "Add")
            {
                capstageMapping = capstageMapping.GetCapStageMapping(capstageMapping.DepartmentId, capstageMapping.JobTypeId);
                if (capstageMapping != null)
                {
                    modalLabelError.Text = "Error: JobType already been map.";
                    modalLabelError.Visible = true;
                    haserror = true;
                }
                capstageMapping = new CAPStageMapping();
            }
            else
            {
                int mappingId = Convert.ToInt32(modalLabelStageMappingId.Text);
                capstageMapping = capstageMapping.GetCapStageMapping(capstageMapping.DepartmentId, capstageMapping.JobTypeId);
                if (capstageMapping != null && capstageMapping.Id != mappingId)
                {
                    modalLabelError.Text = "Error: JobType already been map.";
                    modalLabelError.Visible = true;
                    haserror = true;
                }
                capstageMapping = new CAPStageMapping();
                capstageMapping.Id = mappingId;
            }
            if (haserror)
            {
                this.programmaticModalPopup.Show();
            }
            else
            {
                capstageMapping.DepartmentId = Convert.ToInt32(modalDropDownDepartment.SelectedItem.Value);
                capstageMapping.JobTypeId = Convert.ToInt32(modalDropDownJobType.SelectedItem.Value);
                capstageMapping.DatabaseMap = modalDropDownDatabase.SelectedItem.Value.Trim();
                capstageMapping.SD_Stage_No = Convert.ToInt32(modalDropDownCapStage.SelectedItem.Value);
                capstageMapping.StageDescription = modalDropDownCapStage.SelectedItem.Text.Trim();
                if (e.CommandArgument.ToString().Trim() == "Add")
                {
                    capstageMapping.Insert(capstageMapping);
                }
                else
                {
                    capstageMapping.Update(capstageMapping);
                }
                modalLabelError.Text = "";
                modalLabelError.Visible = false;
                this.programmaticModalPopup.Hide();
                InitializeMainGrid();
            }
        }
        private void InitializeMainGrid()
        {
            GetMyAccessRights();

            CAPStageMapping capStageMapping = new CAPStageMapping();
            if (myAccessRights.CanAdd == true)
            {
                linkBtnAdd.Visible = true;
            }
            else
                linkBtnAdd.Visible = false;

            List<CAPStageMapping> stagemappings = new List<CAPStageMapping>();
            if (dropDownListDepartment.SelectedItem.Text == "All")
            {
                stagemappings = capStageMapping.GetCapStageMappingList();
            }
            else
            {
                stagemappings = capStageMapping.GetCapStageMappingListByDepartment(Convert.ToInt32(dropDownListDepartment.SelectedItem.Value));
            }

            gridViewStageMapping.DataSource = stagemappings;
            gridViewStageMapping.DataBind();
        }
        protected void modalBtnDelete_Command(object sender, CommandEventArgs e)
        {
            CAPStageMapping capStageMapping = new CAPStageMapping();
            int mappingId = Convert.ToInt32(modalLabelStageMappingId.Text);

            capStageMapping.Delete(mappingId);
            this.programmaticModalPopup.Hide();
            InitializeMainGrid();
        }
 private void ParseMapping(T_CAPStageMapping t_capstagemapping, CAPStageMapping capstagemapping)
 {
     t_capstagemapping.Id = capstagemapping.Id;
     t_capstagemapping.DepartmentId = capstagemapping.DepartmentId;
     t_capstagemapping.JobTypeId = capstagemapping.JobTypeId;
     t_capstagemapping.SD_Stage_No = capstagemapping.SD_Stage_No;
     t_capstagemapping.DatabaseMap = capstagemapping.DatabaseMap;
     t_capstagemapping.StageDescription = capstagemapping.StageDescription;
 }
 public void Update(CAPStageMapping capstagemapping)
 {
     using (TimeTrackerEntities db = new TimeTrackerEntities())
     {
         try
         {
             T_CAPStageMapping t_capstagemapping = db.T_CAPStageMapping.FirstOrDefault(p => p.Id == capstagemapping.Id);
             ParseMapping(t_capstagemapping, capstagemapping);
             db.SaveChanges();
         }
         catch (Exception ex)
         {
             string msg = ex.Message;
         }
     }
 }
        public void Insert(CAPStageMapping capstagemapping)
        {
            T_CAPStageMapping t_capstagemapping = new T_CAPStageMapping();
            ParseMapping(t_capstagemapping, capstagemapping);

            using (TimeTrackerEntities db = new TimeTrackerEntities())
            {
                try
                {
                    db.T_CAPStageMapping.Add(t_capstagemapping);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }
            }
        }
 public List<CAPStageMapping> GetCAPStagesByCAPDatabase(string databaselocation)
 {
     //Gets all stages from STAGE_DEFINITION table in CAP by databaselocation
     //databaselocation is set in WebConfig. (CAPHWConnection or CAPSWConnection)
     List<CAPStageMapping> capStageList = new List<CAPStageMapping>();
     using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[databaselocation].ToString()))
     {
         SqlCommand cmd = new SqlCommand("Select  SD_Stage_No,SD_Stage_Desc from STAGE_DEFINITION ", con);
         con.Open();
         SqlDataReader reader = cmd.ExecuteReader();
         while (reader.Read())
         {
             CAPStageMapping capStage = new CAPStageMapping();
             capStage.SD_Stage_No = Convert.ToInt32(reader["SD_Stage_No"].ToString());
             capStage.StageDescription = reader["SD_Stage_Desc"].ToString();
             capStage.DatabaseMap = databaselocation;
             capStageList.Add(capStage);
         }
     }
     return capStageList;
 }