private void button1_Click(object sender, EventArgs e)
        {
            OperationResult objOperationResult = new OperationResult();

            _protocolcomponentListDTO = new List <protocolcomponentDto>();
            if (uvProtocol.Validate(true, false).IsValid)
            {
                var id  = cbOrganization.SelectedValue.ToString().Split('|');
                var id1 = cbOrganizationInvoice.SelectedValue.ToString().Split('|');
                var id2 = cbIntermediaryOrganization.SelectedValue.ToString().Split('|');

                if (_protocolDTO == null)
                {
                    _protocolDTO = new protocolDto();
                }
                //if (_serviceDTO == null)
                //{
                //    _serviceDTO = new serviceDto();
                //}
                _serviceDTO = new ServiceBL().GetService(ref objOperationResult, _serviceId);

                _protocolDTO.v_Name = txtProtocolName.Text;
                _protocolDTO.v_EmployerOrganizationId = id[0];
                _protocolDTO.v_EmployerLocationId     = id[1];
                _protocolDTO.i_EsoTypeId              = int.Parse(cbEsoType.SelectedValue.ToString());
                _protocolDTO.v_GroupOccupationId      = cbGeso.SelectedValue.ToString();
                _protocolDTO.v_CustomerOrganizationId = id1[0];
                _protocolDTO.v_CustomerLocationId     = id1[1];
                _protocolDTO.v_WorkingOrganizationId  = id2[0];
                _protocolDTO.v_WorkingLocationId      = cbIntermediaryOrganization.SelectedValue.ToString() != "-1" ? id2[1] : "-1";
                _protocolDTO.i_MasterServiceId        = int.Parse(cbService.SelectedValue.ToString());
                _protocolDTO.i_MasterServiceTypeId    = int.Parse(cbServiceType.SelectedValue.ToString());
                _protocolDTO.v_ProtocolId             = _protocolId;
                _protocolDTO.v_ComentaryUpdate        = GetChangesProtocol();
                _protocolBL.UpdateProtocol(ref objOperationResult,
                                           _protocolDTO,
                                           _protocolcomponentListDTO,
                                           _protocolcomponentListDTO,
                                           _protocolcomponentListDTO,
                                           Globals.ClientSession.GetAsList());

                _serviceDTO.v_centrocosto = txtCentroCosto.Text;
                _serviceDTO.v_ServiceId   = _serviceId;
                //_ServiceBL.UpdateService(ref objOperationResult, _serviceDTO, Globals.ClientSession.GetAsList());
                _ServiceBL.UpdateService(ref objOperationResult, _serviceDTO, Globals.ClientSession.GetAsList());
            }

            if (objOperationResult.Success == 1)      // Operación sin error
            {
                MessageBox.Show("Se grabo correctamente.", "INFORMACION!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else      // Operación con error
            {
                MessageBox.Show(Constants.GenericErrorMessage, "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            OperationResult objOperationResult = new OperationResult();

            _protocolcomponentListDTO = new List <protocolcomponentDto>();

            if (uvProtocol.Validate(true, false).IsValid)
            {
                #region Validations

                if (_tmpProtocolcomponentList == null || _tmpProtocolcomponentList.Count == 0)
                {
                    MessageBox.Show("Por favor agregue Examenes al protocolo", "Error de validación", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                #endregion

                #region En un protocolo No se debe permitir agregar un Componente que tenga un campo formula que depende de otr componente que NO está en mismo protocolo. Si esto ocurre debe decir indicar lo siguiente: "El campo formula XXXXX depende de los campos YYY, ZZZZ que están en los componentes LLLLLL, y MMMMMM. Por favor agrege previamente los componentes LLLL y MMMM al protocolo.

                OperationResult objOperationResult1 = new OperationResult();

                string[] componentIdFromProtocol = _tmpProtocolcomponentList.FindAll(p => p.i_RecordStatus != (int)RecordStatus.EliminadoLogico)
                                                   .Select(p => p.v_ComponentId).ToArray();
                foreach (var item in componentIdFromProtocol)
                {
                    SiNo IsExists__ = _protocolBL.IsExistsFormula(ref objOperationResult1, componentIdFromProtocol, item);

                    if (IsExists__ == SiNo.NO)
                    {
                        MessageBox.Show(objOperationResult1.ReturnValue, "Error de validación", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }

                #endregion

                var id  = cbOrganization.SelectedValue.ToString().Split('|');
                var id1 = cbOrganizationInvoice.SelectedValue.ToString().Split('|');
                var id2 = cbIntermediaryOrganization.SelectedValue.ToString().Split('|');

                if (_protocolDTO == null)
                {
                    _protocolDTO = new protocolDto();
                }

                _protocolDTO.v_Name = txtProtocolName.Text;
                _protocolDTO.v_EmployerOrganizationId = id[0];
                _protocolDTO.v_EmployerLocationId     = id[1];
                _protocolDTO.i_EsoTypeId              = int.Parse(cbEsoType.SelectedValue.ToString());
                _protocolDTO.v_GroupOccupationId      = cbGeso.SelectedValue.ToString();
                _protocolDTO.v_CustomerOrganizationId = id1[0];
                _protocolDTO.v_CustomerLocationId     = id1[1];
                _protocolDTO.v_WorkingOrganizationId  = id2[0];
                _protocolDTO.v_WorkingLocationId      = cbIntermediaryOrganization.SelectedValue.ToString() != "-1" ? id2[1] : "-1";
                _protocolDTO.i_MasterServiceId        = int.Parse(cbService.SelectedValue.ToString());
                _protocolDTO.v_CostCenter             = txtCostCenter.Text;
                _protocolDTO.i_MasterServiceTypeId    = int.Parse(cbServiceType.SelectedValue.ToString());
                _protocolDTO.i_HasVigency             = Convert.ToInt32(chkIsHasVigency.Checked);
                _protocolDTO.i_ValidInDays            = txtValidDays.Text != string.Empty ? int.Parse(txtValidDays.Text) : (int?)null;
                _protocolDTO.i_IsActive       = Convert.ToInt32(chkIsActive.Checked);
                _protocolDTO.v_NombreVendedor = cboVendedor.Text;

                // Grabar componentes del protocolo
                if (_mode == "New" || _mode == "Clon")
                {
                    #region Validar Nombre del prorocolo

                    if (IsExistsProtocolName())
                    {
                        MessageBox.Show("Por favor Ingrese otro nombre de protocolo, este nombre ya existe", "Error de validación", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    #endregion

                    foreach (var item in _tmpProtocolcomponentList)
                    {
                        protocolcomponentDto protocolComponent = new protocolcomponentDto();

                        protocolComponent.v_ComponentId     = item.v_ComponentId;
                        protocolComponent.r_Price           = item.r_Price;
                        protocolComponent.i_OperatorId      = item.i_OperatorId;
                        protocolComponent.i_Age             = item.i_Age;
                        protocolComponent.i_GenderId        = item.i_GenderId;
                        protocolComponent.i_IsAdditional    = item.i_isAdditional;
                        protocolComponent.i_IsConditionalId = item.i_IsConditionalId;

                        protocolComponent.i_IsConditionalIMC = item.i_IsConditionalIMC;
                        protocolComponent.r_Imc = item.r_Imc;

                        _protocolcomponentListDTO.Add(protocolComponent);
                    }

                    _protocolId = _protocolBL.AddProtocol(ref objOperationResult, _protocolDTO, _protocolcomponentListDTO, Globals.ClientSession.GetAsList());

                    //Agregar Usuario Externo
                    _protocolBL.AgregarUsuarioExterno(_protocolDTO.v_CustomerOrganizationId, _protocolId, Globals.ClientSession.GetAsList());

                    if (!string.IsNullOrEmpty(_protocolId))
                    {
                        _mode         = "Edit";
                        _protocolName = txtProtocolName.Text;
                    }
                }
                else if (_mode == "Edit")
                {
                    #region Validar Nombre del prorocolo

                    if (txtProtocolName.Text != _protocolName)
                    {
                        if (IsExistsProtocolName())
                        {
                            MessageBox.Show("Por favor Ingrese otro nombre de protocolo, este nombre ya existe", "Error de validación", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                    #endregion

                    _protocolDTO.v_ProtocolId = _protocolId;

                    _protocolcomponentListDTOUpdate = new List <protocolcomponentDto>();
                    _protocolcomponentListDTODelete = new List <protocolcomponentDto>();

                    foreach (var item in _tmpProtocolcomponentList)
                    {
                        // Add
                        if (item.i_RecordType == (int)RecordType.Temporal && item.i_RecordStatus == (int)RecordStatus.Agregado)
                        {
                            protocolcomponentDto protocolComponent = new protocolcomponentDto();

                            protocolComponent.v_ProtocolComponentId = item.v_ProtocolComponentId;
                            protocolComponent.v_ComponentId         = item.v_ComponentId;
                            protocolComponent.r_Price            = item.r_Price;
                            protocolComponent.i_OperatorId       = item.i_OperatorId;
                            protocolComponent.i_Age              = item.i_Age;
                            protocolComponent.i_GenderId         = item.i_GenderId;
                            protocolComponent.i_IsAdditional     = item.i_isAdditional;
                            protocolComponent.i_IsConditionalIMC = item.i_IsConditionalIMC;
                            protocolComponent.r_Imc              = item.r_Imc;

                            protocolComponent.i_IsConditionalId = item.i_IsConditionalId;
                            _protocolcomponentListDTO.Add(protocolComponent);
                        }

                        // Update
                        if (item.i_RecordType == (int)RecordType.NoTemporal && item.i_RecordStatus == (int)RecordStatus.Modificado)
                        {
                            protocolcomponentDto protocolComponent = new protocolcomponentDto();

                            protocolComponent.v_ProtocolComponentId = item.v_ProtocolComponentId;
                            protocolComponent.v_ComponentId         = item.v_ComponentId;
                            protocolComponent.r_Price            = item.r_Price;
                            protocolComponent.i_OperatorId       = item.i_OperatorId;
                            protocolComponent.i_Age              = item.i_Age;
                            protocolComponent.i_GenderId         = item.i_GenderId;
                            protocolComponent.i_IsAdditional     = item.i_isAdditional;
                            protocolComponent.i_IsConditionalIMC = item.i_IsConditionalIMC;
                            protocolComponent.r_Imc              = item.r_Imc;
                            protocolComponent.i_IsConditionalId  = item.i_IsConditionalId;
                            _protocolcomponentListDTOUpdate.Add(protocolComponent);
                        }

                        // Delete
                        if (item.i_RecordType == (int)RecordType.NoTemporal && item.i_RecordStatus == (int)RecordStatus.EliminadoLogico)
                        {
                            protocolcomponentDto protocolComponent = new protocolcomponentDto();

                            protocolComponent.v_ProtocolComponentId = item.v_ProtocolComponentId;
                            _protocolcomponentListDTODelete.Add(protocolComponent);
                        }
                    }
                    _protocolBL.UpdateProtocol(ref objOperationResult,
                                               _protocolDTO,
                                               _protocolcomponentListDTO,
                                               _protocolcomponentListDTOUpdate.Count == 0 ? null : _protocolcomponentListDTOUpdate,
                                               _protocolcomponentListDTODelete.Count == 0 ? null : _protocolcomponentListDTODelete,
                                               Globals.ClientSession.GetAsList());
                }

                // Analizar el resultado de la operación
                if (objOperationResult.Success == 1)  // Operación sin error
                {
                    //this.DialogResult = DialogResult.OK;
                    MessageBox.Show("Se grabo correctamente.", "INFORMACION!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    _mode = "Edit";
                    LoadData();
                    //this.Close();
                }
                else  // Operación con error
                {
                    MessageBox.Show(Constants.GenericErrorMessage, "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    // Se queda en el formulario.
                }
            }
            else
            {
                MessageBox.Show("Por favor corrija la información ingresada. Vea los indicadores de error.", "Error de validación", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }