예제 #1
0
        private void guardar()
        {
            var     context = new PruebaEntities();
            Persona persona = context.Persona.Find(cbPersona.SelectedValue);

            // Si la caja de texto está vacía, se tratará de una inserción
            if (String.IsNullOrEmpty(txtId.Text))
            {
                try
                {
                    // Creamos un nuevo usuario y lo añadimos, guardando a continuación los cambios
                    Asueto asueto = new Asueto
                    {
                        id          = 0,
                        fechaInicio = dpFechaInicio.Value.ToShortDateString(),
                        fechaFin    = dpFechaFin.Value.ToShortDateString(),
                        tipo        = cbTipo.Text,
                        Persona     = persona
                    };
                    context.Asueto.Add(asueto);
                    context.SaveChanges();
                    cargar();
                    MessageBox.Show("REGISTRO NUEVO EXITOSO!");
                }
                catch (Exception)
                {
                    throw;
                }
            }

            // En caso contrario, se tratará de una modificación
            else
            {
                try
                {
                    int id = int.Parse(txtId.Text);
                    // Recuperamos el usuario cuyo identificador coincida con el de la caja de texto
                    Asueto u = context.Asueto.Where(asueto => asueto.id == id).First();

                    // Modificamos el contenido
                    u.fechaInicio = dpFechaInicio.Value.ToShortDateString();
                    u.fechaFin    = dpFechaFin.Value.ToShortDateString();
                    u.tipo        = cbTipo.Text;
                    u.Persona     = persona;

                    // Guardamos los cambios
                    context.SaveChanges();
                    cargar();
                    MessageBox.Show("ACTUALIZACION EXITOSA!");
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
예제 #2
0
        private void eliminar()
        {
            var context = new PruebaEntities();

            if (!String.IsNullOrEmpty(txtId.Text))
            {
                // Recuperamos el usuario cuyo identificador coincida con el de la caja de texto
                int    id = int.Parse(txtId.Text);
                Asueto u  = context.Asueto.Where(asueto => asueto.id == id).First();

                // Lo eliminamos y salvamos los cambios
                context.Asueto.Remove(u);
                context.SaveChanges();
                cargar();
            }
        }