Ejemplo n.º 1
0
        private void btnGuardarA_Click(object sender, EventArgs e)
        {
            //validaciones
            BorrarMensaje();
            if (validarCampos() && ValidarFechaySexo())
            {
                //creo un objeto de la clase persona y guardo a través de las propiedades
                if (txtFoto.Text == "Seleccionar foto...")
                {
                    MessageBox.Show("Debe seleccionar una foto", "¡Cuidado!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    try
                    {
                        Alumno alumno = new Alumno();

                        alumno.Nombres         = txtNombreA.Text;
                        alumno.Apellidos       = txtApellidoA.Text;
                        alumno.FechaNac        = Convert.ToString(dtpFechanacimiento.Value.ToString("yyyy-MM-dd"));
                        alumno.Telefono        = mtxtTelefonoA.Text;
                        alumno.Nie             = mtxtNIEA.Text;
                        alumno.NumPartida      = mtxtNumPartidaA.Text;
                        alumno.NombrePadre     = txtPadre.Text;
                        alumno.NombreMadre     = txtMadre.Text;
                        alumno.NombreEncargado = txtEncargado.Text;
                        alumno.Sexo            = cmbSexoA.Text;
                        alumno.Direccion       = rtbDireccionA.Text;

                        byte[] file   = null;
                        Stream stream = openFileFoto.OpenFile();

                        using (MemoryStream ms = new MemoryStream())
                        {
                            stream.CopyTo(ms);
                            file = ms.ToArray();
                        }

                        alumno.Foto = file;


                        if (alumno.Agregar(alumno.Nombres, alumno.Apellidos, alumno.FechaNac, alumno.Sexo, alumno.Telefono, alumno.Direccion, alumno.NumPartida, alumno.Nie,
                                           alumno.NombrePadre, alumno.NombreMadre, alumno.NombreEncargado, alumno.Foto) == true)
                        {
                            MessageBox.Show("Los datos se han ingresado correctamente", "¡Enhorabuena!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            ActualizarDataGrid();
                            Limpiar();
                            btnEliminarA.Enabled = false;
                            btnGuardarA.Enabled  = true;
                            btnEditarA.Enabled   = false;
                        }
                        else
                        {
                            MessageBox.Show("Error al agregar el registro", "¡Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    catch (Exception Ex)
                    {
                        MessageBox.Show("Error al agregar el registro " + Ex.Message, "¡Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Debe ingresar todos los datos", "¡Cuidado!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }