protected void EditLink_Command(object sender, CommandEventArgs e)
        {
            try
            {

                projectGrid.Visible = false;

                EthicsContainer.Visible = true;

                txtEthicsStartDate.Enabled = false;

                ResetControls();

                PopulateDropDown();

                btnUpdateEthics.Visible = true;
                btnSaveEthics.Visible = false;

                TTSHWCFReference.Ethics_Details ethics = new Ethics_Details();

                int ethicsID = Convert.ToInt32(e.CommandArgument);
                HdnId.Value = Convert.ToString(ethicsID);
                // Session["EthicsID"] = ethicsID;

                TTSHWCFServiceClient client = new TTSHWCFServiceClient();

                ethics = client.GetEthics_DetailsByID(ethicsID);
                HttpContext.Current.Session["ProjectID"] = ethics.i_Project_ID;

                /*Bind values to controls*/

                //Set Text fields
                string CoInvest = ethics.CO_Investigator.ToString();

                //Parse xml and bind co investigator details
                if (CoInvest != string.Empty)
                {
                    using (XmlReader reader = XmlReader.Create(new StringReader(CoInvest)))
                    {
                        XmlDocument xml = new XmlDocument();
                        xml.Load(reader);

                        XmlNodeList xmlNodeList = xml.SelectNodes("/COINVESTIGATOR_D/COINVESTIGATOR");
                        foreach (XmlNode node in xmlNodeList)
                        {

                            string id = node["ID"] == null ? "" : node["ID"].InnerText;

                            if (id == "" || id == "0")
                            {
                                txtCoInvestigator.Enabled = false;
                            }
                            else
                            {
                                txtCoInvestigator.Text = node["Coinves_name"] == null ? "" : node["Coinves_name"].InnerText;
                                txtCoInvestigator.Enabled = true;
                            }

                        }
                    }
                }
                else
                {
                    txtCoInvestigator.Enabled = false;
                }

                txtComments.Text = ethics.s_Comments;

                txtCRIOCulledDate.Text = (ethics.dt_CRIO_culled_date == null || ethics.dt_CRIO_culled_date == DateTime.MinValue ? "" : Convert.ToDateTime(ethics.dt_CRIO_culled_date).ToString("dd-MMM-yyyy"));

                txtEthicalNotes.Text = ethics.s_Remarks;

                txtGlobalSubRecruited.Text = ethics.i_Sub_Recruited.ToString();

                txtGlobalSubTargeted.Text = ethics.i_Sub_targeted.ToString();

                txtSubRecruited.Text = ethics.i_Sub_Recruited_TTSH.ToString();

                txtSubTargeted.Text = ethics.i_Sub_Targeted_TTSH.ToString();

                ddlPeriodOfInsurance.SelectedValue = ethics.s_Insurance_Period == "" || ethics.s_Insurance_Period == null ? "-1" : ethics.s_Insurance_Period;

                txtIRBApproveDate.Text = (ethics.dt_IRB_Approve_Date == null || ethics.dt_IRB_Approve_Date == DateTime.MinValue ? "" : Convert.ToDateTime(ethics.dt_IRB_Approve_Date).ToString("dd-MMM-yyyy"));

                txtIRBApproveEndDate.Text = (ethics.dt_IRB_Approve_Enddate == null || ethics.dt_IRB_Approve_Enddate == DateTime.MinValue ? "" : Convert.ToDateTime(ethics.dt_IRB_Approve_Enddate).ToString("dd-MMM-yyyy"));

                txtIRBNumber.Text = ethics.s_IRB_No;

                //   dtCompleted_Withdrawn.Text = (ethics.dt_Project_Status_date == null || ethics.dt_Project_Status_date == DateTime.MinValue ? "" : Convert.ToDateTime(ethics.dt_Project_Status_date).ToString("dd-MMM-yyyy"));
                dtCompleted_Withdrawn.Text = Convert.ToDateTime(ethics.dt_Project_Status_date).ToString("dd-MMM-yyyy");
                if (ethics.s_IRB_File != null && ethics.s_IRB_File != "")
                {

                    btnDownIRBFile.Visible = true;

                    hdnIRBFile.Value = ethics.s_IRB_File;

                    string[] arr = ethics.s_IRB_File.Split('/');

                    string fName = arr[arr.Length - 1].Split('~')[0] + "." + arr[arr.Length - 1].Split('.')[arr[arr.Length - 1].Split('.').Length - 1];

                    btnDownIRBFile.Text = fName;
                    hdnIRBFileEnabled.Value = "Yes";
                }

                if (ethics.s_Insurance_file != null && ethics.s_Insurance_file != "")
                {
                    btnDownInsuranceFile.Visible = true;

                    hdnInsuranceFile.Value = ethics.s_Insurance_file;

                    string[] arr = ethics.s_Insurance_file.Split('/');

                    string fName = arr[arr.Length - 1].Split('~')[0] + "." + arr[arr.Length - 1].Split('.')[arr[arr.Length - 1].Split('.').Length - 1];

                    btnDownInsuranceFile.Text = fName;

                }

                txtNewStudyEndDate.Text = (ethics.dt_NewStudy_End_date == null || ethics.dt_NewStudy_End_date == DateTime.MinValue ? "" : Convert.ToDateTime(ethics.dt_NewStudy_End_date).ToString("dd-MMM-yyyy"));

                // HttpContext.Current.Session["ProjectID"] = ethics.i_Project_ID;

                // bProjectID.InnerText = "Project ID: " + ethics.i_Project_ID;

                string projectXML = ethics.Project_Data;

                //Parse xml and bind project details
                if (projectXML != string.Empty)
                {
                    using (XmlReader reader = XmlReader.Create(new StringReader(projectXML)))
                    {
                        XmlDocument xml = new XmlDocument();

                        xml.Load(reader);

                        XmlNodeList xmlNodeList = xml.SelectNodes("/PROJECT/PROJECT_DATA");

                        foreach (XmlNode node in xmlNodeList)
                        {

                            txtAlias1.Text = node["s_Project_Alias1"] == null ? "" : node["s_Project_Alias1"].InnerText;

                            txtAlias2.Text = node["s_Project_Alias2"] == null ? "" : node["s_Project_Alias2"].InnerText;

                            txtProjectTitle.Text = node["s_Project_Title"] == null ? "" : node["s_Project_Title"].InnerText;

                            txtShortTitle.Text = node["s_Short_Title"] == null ? "" : node["s_Short_Title"].InnerText;

                            ddlProjectCategory.SelectedValue = node["i_Project_Category_ID"] == null ? "" : node["i_Project_Category_ID"].InnerText;
                            bProjectID.InnerText = "Project ID: " + node["s_Display_Project_ID"].InnerText;

                        }
                    }
                }

                //Set Drop drown fields
                // ddlProjectStatus.SelectedValue = ethics.i_Project_Status_ID.ToString();

                ddlEthicsReview.SelectedValue = ethics.i_EthicsReview_ID.ToString();

                ddlIRBStatus.SelectedValue = ethics.i_IRB_Status_ID.ToString();

                ddlIRBType.SelectedValue = ethics.i_IRB_Type_ID.ToString();

                ddlRenewal.SelectedValue = (ethics.b_IsRenewal == null) ? "-1" : (ethics.b_IsRenewal == true) ? "1" : "0";

                ddlCRIOCulled.SelectedValue = ethics.b_CRIO_culled ? "1" : "0";

                ddlClinicalTrialInsurance.SelectedValue = ethics.b_IsClinical_Trial_Insurance ? "1" : "0";

                ddlChildBearing.SelectedValue = (ethics.b_IsChildBearing == null) ? "-1" : ethics.b_IsChildBearing == true ? "1" : "0";

                txtEthicsStartDate.Text = Convert.ToDateTime(ethics.dt_Ethics_Start_Date).ToString("dd-MMM-yyyy");

                FillPIGrid(ethics.Dept_PI.ToList());

            }
            catch
            {

            }
        }
        protected void btnUpdateEthics_Click(object sender, EventArgs e)
        {
            try
            {

                TTSHWCFReference.Ethics_Details ethics = new Ethics_Details();

                //File UpLoadNew
                string msg = "";
                if (fuIRBFile.HasFile && hdnIRBFileEnabled.Value == "Yes")
                {
                    string[] arr = Common.UpLoadNew(fuIRBFile, Common.FolderLocation.EthicFiles);

                    if (arr.Count() != 0)
                    {
                        hdnIRBFile.Value = arr[0];
                    }
                }

                if (fuInsuranceFile.HasFile && hdnInsuranceFileEnabled.Value == "Yes")
                {
                    string[] arr = Common.UpLoadNew(fuInsuranceFile, Common.FolderLocation.EthicFiles);

                    if (arr.Count() != 0)
                    {
                        hdnInsuranceFile.Value = arr[0];
                    }
                }
                //End of File UpLoadNew

                ethics.b_CRIO_culled = (ddlCRIOCulled.SelectedValue == "-1" || ddlCRIOCulled.SelectedValue == "0" ? false : true);

                if (ddlChildBearing.SelectedValue == "-1") ethics.b_IsChildBearing = null;
                else ethics.b_IsChildBearing = (ddlChildBearing.SelectedValue == "1") ? true : false;

                ethics.b_IsClinical_Trial_Insurance = (ddlClinicalTrialInsurance.SelectedValue == "-1" || ddlClinicalTrialInsurance.SelectedValue == "0" ? false : true);

                if (ddlRenewal.SelectedValue == "-1") ethics.b_IsRenewal = null;
                else ethics.b_IsRenewal = (ddlRenewal.SelectedValue == "1") ? true : false;

                ethics.dt_Created_Date = DateTime.Now;

                ethics.s_CreatedBy_ID = User.Identity.Name;

                if (txtCRIOCulledDate.Text.Trim() == "" || hdnRecordCulledEnabled.Value == "No")
                    ethics.dt_CRIO_culled_date = null;
                else
                    ethics.dt_CRIO_culled_date = Convert.ToDateTime(txtCRIOCulledDate.Text);

                ethics.s_Insurance_Period = ddlPeriodOfInsurance.SelectedValue == "-1" ? "" : ddlPeriodOfInsurance.SelectedValue;

                if (txtIRBApproveDate.Text.Trim() == "" || hdnIRBFileEnabled.Value == "No")
                    ethics.dt_IRB_Approve_Date = null;
                else
                    ethics.dt_IRB_Approve_Date = Convert.ToDateTime(txtIRBApproveDate.Text);

                if (txtIRBApproveEndDate.Text.Trim() == "" || hdnIRBFileEnabled.Value == "No")
                    ethics.dt_IRB_Approve_Enddate = null;
                else
                    ethics.dt_IRB_Approve_Enddate = Convert.ToDateTime(txtIRBApproveEndDate.Text);

                if (txtNewStudyEndDate.Text.Trim() == "" || hdnNewStudyEndDtEnabled.Value == "No")
                    ethics.dt_NewStudy_End_date = null;
                else
                    ethics.dt_NewStudy_End_date = Convert.ToDateTime(txtNewStudyEndDate.Text);

                //if (dtCompleted_Withdrawn.Text.Trim() == "" || hdnDtTerminatedEnabled.Value == "No")
                //    ethics.dt_Project_Status_date = null;
                //else
                ethics.dt_Project_Status_date = Convert.ToDateTime(dtCompleted_Withdrawn.Text);

                ethics.i_ID = Convert.ToInt32(HdnId.Value);

                ethics.i_IRB_Status_ID = (ddlIRBStatus.SelectedValue == "-1" || ddlIRBStatus.SelectedValue == "0" ? 0 : Convert.ToInt32(ddlIRBStatus.SelectedValue));

                ethics.i_IRB_Type_ID = (ddlIRBType.SelectedValue == "-1" || ddlIRBType.SelectedValue == "0" ? 0 : Convert.ToInt32(ddlIRBType.SelectedValue));

                //    ethics.i_Project_Status_ID = (ddlProjectStatus.SelectedValue == "-1" || ddlProjectStatus.SelectedValue == "0" ? 0 : Convert.ToInt32(ddlProjectStatus.SelectedValue));

                ethics.i_EthicsReview_ID = (ddlEthicsReview.SelectedValue == "-1" || ddlEthicsReview.SelectedValue == "0" ? 0 : Convert.ToInt32(ddlEthicsReview.SelectedValue));

                ethics.i_Sub_Recruited = (txtGlobalSubRecruited.Text.Trim() == "" ? 0 : Convert.ToInt32(txtGlobalSubRecruited.Text));

                ethics.i_Sub_targeted = (txtGlobalSubTargeted.Text.Trim() == "" ? 0 : Convert.ToInt32(txtGlobalSubTargeted.Text));

                ethics.i_Sub_Targeted_TTSH = (txtSubTargeted.Text.Trim() == "" ? 0 : Convert.ToInt32(txtSubTargeted.Text));

                ethics.i_Sub_Recruited_TTSH = (txtSubRecruited.Text.Trim() == "" ? 0 : Convert.ToInt32(txtSubRecruited.Text));

                ethics.s_Comments = txtComments.Text;

                if (hdnInsuranceFile.Value != null && hdnInsuranceFile.Value != "")
                    ethics.s_Insurance_file = hdnInsuranceFile.Value;

                if (hdnIRBFile.Value != null && hdnIRBFile.Value != "")
                    ethics.s_IRB_File = hdnIRBFile.Value;

                if (txtIRBNumber.Text != "")
                {
                    hdnIRBFileEnabled.Value = "Yes";
                }
                ethics.s_IRB_No = hdnIRBFileEnabled.Value == "Yes" ? txtIRBNumber.Text : "";

                ethics.s_Remarks = txtEthicalNotes.Text;

                ethics.CO_Investigator = txtCoInvestigator.Text;

                int ProjID = Convert.ToInt32(HttpContext.Current.Session["ProjectID"]);

                ethics.i_Project_ID = ProjID;

                ethics.i_Project_Category_ID = Convert.ToInt32(ddlProjectCategory.SelectedValue);

                ethics.s_Project_Alias1 = txtAlias1.Text;

                ethics.s_Project_Alias2 = txtAlias2.Text;

                ethics.s_Short_Title = txtShortTitle.Text;

                List<Project_PI> PIs = new List<Project_PI>();

                string[] splitPiId = HdnPi_ID.Value.Split(',');

                foreach (string pi in splitPiId)
                {
                    PIs.Add(new Project_PI { i_PI_ID = Convert.ToInt32(pi), i_Project_ID = ProjID });

                }

                ethics.Project_PIs = PIs.ToArray();

                ethics.dt_Ethics_Start_Date = Convert.ToDateTime(txtEthicsStartDate.Text);

                TTSHWCFServiceClient client = new TTSHWCFServiceClient();

                string result = client.Ethics_Details(ethics, Mode.Update);
                if (CheckDML(result))
                {
                    EthicsContainer.Visible = false;

                    projectGrid.Visible = true;

                    FillGrid();

                    //CallJS("MessageBoxEvent('Ethics saved successfully','PerformCancel();');");
                    TextBox t = ((TextBox)(SearchBox.FindControl("txtSearch")));
                    t.Text = "";
                    CallJS("MessageBox('Ethics saved successfully');");
                    HdnId.Value = "0";
                }
            }
            catch (Exception)
            {

                throw;
            }
        }