private void Button_Click(object sender, RoutedEventArgs e) { bool flag = false; if (string.IsNullOrEmpty(PrimerNombre.Text)) { PrimerNombre.Text = ""; flag = true; } if (string.IsNullOrEmpty(PrimerApellido.Text)) { PrimerApellido.Text = ""; flag = true; } if (string.IsNullOrEmpty(NoDocumento.Text)) { NoDocumento.Text = ""; flag = true; } FechaNacimiento.Text = FechaNacimientodata.Text; if (string.IsNullOrEmpty(FechaNacimiento.Text)) { FechaNacimiento.Text = ""; flag = true; } DateTime dob; DateTimeFormatInfo info = new DateTimeFormatInfo(); info.ShortDatePattern = "dd/MM/yy"; bool valid = DateTime.TryParse(FechaNacimiento.Text, info, DateTimeStyles.None, out dob); if (valid) { DateTime now = DateTime.Today; int age = now.Year - dob.Year; if (now <= dob) { FechaNacimientoError.Text = "Fecha nacimiento no superar la actual."; FechaNacimientoError.Visibility = Visibility.Visible; flag = true; } else { if (age < 15) { FechaNacimientoError.Text = "No se acepta el registro de menores de 15 años."; FechaNacimientoError.Visibility = Visibility.Visible; flag = true; } else { if (age > 99) { FechaNacimientoError.Text = "Fecha nacimiento no valida."; FechaNacimientoError.Visibility = Visibility.Visible; flag = true; } else { FechaNacimientoError.Visibility = Visibility.Hidden; } } } } if (string.IsNullOrEmpty(RepetirNoDocumento.Text)) { RepetirNoDocumento.Text = ""; flag = true; } if (string.IsNullOrEmpty(Direccion.Text)) { Direccion.Text = ""; flag = true; } if (string.IsNullOrEmpty(Telefono.Text)) { Telefono.Text = ""; flag = true; } if (string.IsNullOrEmpty(Celular.Text)) { Celular.Text = ""; flag = true; } //if (string.IsNullOrEmpty(CorreoElectronico.Text)) { CorreoElectronico.Text = ""; flag = true; } if (!string.IsNullOrEmpty(CorreoElectronico.Text)) { if (string.IsNullOrEmpty(CorreoElectronicoRepeat.Text)) { CorreoElectronicoRepeat.Text = ""; flag = true; } Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); Match match = regex.Match(CorreoElectronico.Text); if (!match.Success) { flag = true; } match = regex.Match(CorreoElectronicoRepeat.Text); if (!match.Success) { flag = true; } } if (CorreoElectronicoRepeat.Text != CorreoElectronico.Text) { flag = true; CorreoElectronicoRepeat.Text = ""; } if (!string.IsNullOrEmpty(CorreoElectronicoRepeat.Text) && string.IsNullOrEmpty(CorreoElectronico.Text)) { flag = true; } string sexo = ""; if (!(Femenino.IsChecked.Value) && !Masculino.IsChecked.Value) { Sexo.Visibility = Visibility.Visible; flag = true; } else { if (Femenino.IsChecked.Value) sexo = "F"; if (Masculino.IsChecked.Value) sexo = "M"; } if (ListDepartamento.SelectedValue.ToString() == "Seleccione Departamento") { DepartamentoError.Visibility = Visibility.Visible; flag = true; } if (Ciudad.SelectedValue == null) { CiudadError.Visibility = Visibility.Visible; flag = true; } if ((TipoDocumento.SelectedItem).ToString() == "Seleccione tipo documento") { TipoDocumentoError.Visibility = Visibility.Visible; flag = true; } //if ( Person.ReferenceEquals(Error).Equals(true)) if (PrimerNombre.Text.Length > 50) { flag = true; } if (SegundoNombre.Text.Length > 50) { flag = true; } if (PrimerApellido.Text.Length > 50) { flag = true; } if (SegundoApellido.Text.Length > 50) { flag = true; } if (NoDocumento.Text.Length > 50) { flag = true; } if (NoDocumento.Text.Length > 50) { flag = true; } if (NoDocumento.Text.Length > 50) { flag = true; } if (Direccion.Text.Length > 50) { flag = true; } if (Barrio.Text.Length > 50) { flag = true; } if (Telefono.Text.Length > 50) { flag = true; } if (Celular.Text.Length > 50) { flag = true; } if (CorreoElectronico.Text.Length > 50) { flag = true; } if (string.IsNullOrEmpty(Telefono.Text)) { flag = true; } else { Regex regex = new Regex(@"^\d+$"); if (!regex.IsMatch(Telefono.Text)) { flag = true; } if (Telefono.Text.ToString().Length != 7) { flag = true; } } if (string.IsNullOrEmpty(Celular.Text)) { flag = true; } else { Regex regex = new Regex(@"^\d+$"); if (!regex.IsMatch(Celular.Text)) { flag = true; } if (Celular.Text.ToString().Length != 10) { flag = true; } } if (flag) { NoDocumento.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty).UpdateSource(); } else { var NextID = (from i in db.Personas.OrderByDescending(u => u.Id) select i.Id).FirstOrDefault(); Personas personas = new Personas(); personas.Id = NextID + 1; personas.PrimerNombre = PrimerNombre.Text; personas.SegundoNombre = SegundoNombre.Text; personas.PrimerApellido = PrimerApellido.Text; personas.SegundoApellido = SegundoApellido.Text; personas.Sexo = sexo; personas.TipoDocumento = ((ComboBoxItem)TipoDocumento.SelectedItem).Tag.ToString(); personas.NoDocumento = NoDocumento.Text; personas.FechaNacimiento = DateTime.Parse(FechaNacimiento.Text); personas.Usuario = NoDocumento.Text; personas.Contrasena = NoDocumento.Text; personas.Departamento = ((ComboBoxItem)ListDepartamento.SelectedItem).Tag.ToString(); personas.Ciudad = ((ComboBoxItem)Ciudad.SelectedItem).Tag.ToString(); if (Vereda.SelectedItem != null && Vereda.SelectedValue.ToString() != "") { personas.Vereda = ((ComboBoxItem)Vereda.SelectedItem).Tag.ToString(); } else { personas.Vereda = ""; } personas.Direccion = Direccion.Text; personas.Barrio = Barrio.Text; personas.Telefono = Telefono.Text; personas.Celular = Celular.Text; personas.CorreoElectronico = CorreoElectronico.Text; string Usuario = LebelUsuario.Content.ToString(); string id_sede = (from u in db.UsuariosPuntos where u.Usuario.ToString().ToLower().Equals(Usuario.ToLower(), StringComparison.OrdinalIgnoreCase) select u.Id_sedeRedempleo).First().ToString(); personas.CentroEmpleo = id_sede; personas.Estado = null ; personas.AllowEdit = true; personas.Fecha_Creacion = DateTime.Now; personas.Fecha_Modificacion = DateTime.Now; personas.Fecha_Sincronizacion = null; db.Personas.Add(personas); try { db.SaveChanges(); var win = new MainWindow() { DataContext = new ViewModelMain() }; win.Show(); this.Close(); } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); System.Windows.MessageBox.Show(exceptionMessage); } } }
public DetallePersona(string NoIdentificacion, string Usuario) { AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.UnauthenticatedPrincipal); // Crear una identidad con el nombre de usuario IIdentity usuario = new GenericIdentity(Usuario, "Database"); //Crear lista roles String[] roles = { "Usuario", "Administrador" }; // Crear La credencial GenericPrincipal credencial = new GenericPrincipal(usuario, roles); // Asignar e sta credencial a la aplicacion System.Threading.Thread.CurrentPrincipal = credencial; var departamentos = (from d in db.Departamento select d); // ListDepartamento.Items.Insert(0, "Seleccione Departamento"); string myPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "img/logo.png"; Uri iconUri = new Uri(myPath.Replace(@"\", @"/")); this.Icon = BitmapFrame.Create(iconUri); InitializeComponent(); myPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "img/logo_central.fw.png"; iconUri = new Uri(myPath.Replace(@"\", @"/")); Logo.Source = new BitmapImage(iconUri); myPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "img/bg_banderin.jpg"; iconUri = new Uri(myPath.Replace(@"\", @"/")); Logo_back.Source = new BitmapImage(iconUri); myPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "icons/arrow_left.png"; iconUri = new Uri(myPath.Replace(@"\", @"/")); Volver.Source = new BitmapImage(iconUri); LebelUsuario.Content = Thread.CurrentPrincipal.Identity.Name; string tipodoc = NoIdentificacion.ToString().Substring(0, 2); NoIdentificacion = NoIdentificacion.ToString().Replace("CC", "").Replace("TI", "").Replace("CE", "").Replace("PA", ""); var personasedit = (from p in db.Personas where p.NoDocumento.Equals(NoIdentificacion) && p.TipoDocumento.Equals(tipodoc) select p).First(); var UsuarioSync= (from p in db.Personas where p.NoDocumento.Equals(NoIdentificacion) select p.Estado).FirstOrDefault() ; if (UsuarioSync != null) { Editar.Visibility = Visibility.Hidden; } Personas personas = new Personas(); PrimerNombre.Text = personasedit.PrimerNombre; SegundoNombre.Text = personasedit.SegundoNombre; PrimerApellido.Text = personasedit.PrimerApellido; SegundoApellido.Text = personasedit.SegundoApellido; if (personasedit.Sexo == "M") { Sexo.Text = "Masculino"; } else { Sexo.Text = "Femenino"; } TipoDocumento.Text = personasedit.TipoDocumento; NoDocumento.Text = personasedit.NoDocumento; FechaNacimiento.Text = personasedit.FechaNacimiento.ToString("dd/MM/yyyy"); NoDocumento.Text = personasedit.NoDocumento; Departamento.Text = personasedit.Municipio.Departamento.nombre; Ciudad.Text = personasedit.Municipio.nombre; Direccion.Text = personasedit.Direccion; Barrio.Text = personasedit.Barrio; Telefono.Text = personasedit.Telefono; Celular.Text = personasedit.Celular; CorreoElectronico.Text = personasedit.CorreoElectronico; Fecha_Creacion.Text = " " + personasedit.Fecha_Creacion.ToString(); Fecha_Modificacion.Text = " " + personasedit.Fecha_Modificacion.ToString(); Fecha_Sincronizacion.Text = " " + personasedit.Fecha_Sincronizacion.ToString(); if(personasedit.Estado==true){ Estado.Text = " Sincronizado con Exito"; Detalle.Text = "Oferente registrado en el Servicio Público de Empleo"; }else{ if (personasedit.Estado == null) { Estado.Text = " Sin sincronizar"; Detalle.Text = "Ofrente pendiente por sincronizar"; } else { if (personasedit.UsuarioValido == false) { Estado.Text = " Usuario ya existe"; Detalle.Text = personasedit.Respuesta; } else { Estado.Text = " Oferente ya registrado"; Detalle.Text = personasedit.Respuesta; } } } Detalle.TextWrapping = TextWrapping.Wrap; Model.Person person = new Model.Person(); DataContext = person; }