private void AgregarTerritorio()
        {
            string id = txtID.Text;

            if (string.IsNullOrWhiteSpace(id))
            {
                errorProvider.SetError(txtID, "Ingrese Id correcta.");
            }
            bool existeID = db.Territories.Where(t => t.TerritoryID.Equals(id)).Any();

            if (existeID)
            {
                errorProvider.SetError(txtID, "El ID ya está registrado.");
            }
            string nombre = txtTerritorio.Text;

            if (string.IsNullOrWhiteSpace(nombre))
            {
                errorProvider.SetError(txtTerritorio, "Ingrese nombre de territorio.");
            }
            bool existeTerritorio = db.Territories.Where(t => t.TerritoryDescription.Equals(nombre)).Any();

            if (existeTerritorio)
            {
                errorProvider.SetError(txtTerritorio, "El nombre del territorio ya está registrado.");
            }
            if (errorProvider.GetError())
            {
                this.DialogResult = DialogResult.None;//Impide que la ventana se cierre por causa del botón aceptar.
                return;
            }
            int         regionid   = (int)cboRegión.SelectedValue;
            Territories territorio = new Territories
            {
                TerritoryID          = id,
                TerritoryDescription = nombre,
                RegionID             = regionid,
                bitHabilitado        = true
            };

            //Agregar territorio nuevo al datacontext.
            db.Territories.InsertOnSubmit(territorio);
            try
            {
                //Actualizar la base de datos.
                db.SubmitChanges();
                MessageBox.Show("Territorio agregado correctectamente.");
            }
            catch
            {
                MessageBox.Show("Ocurrió un error.");
            }
        }
 /// <summary>
 /// Realiza la operación de inserción o actualización segun la acción del formulario.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnAceptar_Click(object sender, EventArgs e)
 {
     //Limpiar errores pasados.
     errorProvider.InitializeError();
     //Condicional de Id vacío.
     errorProvider.HasError(String.IsNullOrWhiteSpace(txtID.Text), txtID, "Rellenar campo de Id");
     //Condicional de Producto vacío.
     errorProvider.HasError(String.IsNullOrWhiteSpace(txtNombre.Text), txtNombre, "Rellenar campo de Nombre del producto");
     if (errorProvider.GetError(this))
     {
         return;
     }
     //Condicional de Id númerico.
     errorProvider.HasError(!int.TryParse(txtID.Text, out var id), txtID, "Ingrese código númerico de maximo 10 dígitos.");
     if (Accion.Equals(AccionPopup.Nuevo))
     {
         if (errorProvider.GetError(this))
         {
             return;
         }
         //Condicional de Id repetido.
         var existeID = db.Products.Any(p => p.ProductID.Equals(txtID.Text));
         errorProvider.HasError(existeID, txtID, "El Id ya está registrado.");
         //Condicional de Producto repetido.
         var existeProducto = db.Products.Any(p => p.ProductName.Equals(txtNombre.Text));
         errorProvider.HasError(existeProducto, txtNombre, "El producto ya está registrado.");
         if (errorProvider.GetError(this))
         {
             return;
         }
         //Agregar Recurso.
         AgregarRecurso();
     }
     else
     {
         //Condicional de Producto repetido.
         var existeProducto = db.Products.Any(p => p.ProductName == txtNombre.Text && p.ProductID != id);
         errorProvider.HasError(existeProducto, txtNombre, "El producto ya está registrado.");
         if (errorProvider.GetError(this))
         {
             return;
         }
         //Editar el recurso.
         EditarRecurso();
     }
 }
 private bool VerificarIdSeleccionado()
 {
     errorProvider.InitializeError();//Limpia los errores pasados.
     if (String.IsNullOrWhiteSpace(id))
     {
         errorProvider.SetError(dgvTerritorios, "Seleccionar ID dando clic en fila.");
     }
     if (errorProvider.GetError())
     {
         MessageBox.Show("Seleccionar un ID primero");
         return(true);
     }
     return(false);
 }
Example #4
0
        public int ContarErrores(Form formulario)
        {
            int total = 0;

            foreach (Control controles in formulario.Controls)
            {
                if (controles is TextBox)
                {
                    if (ErrorProv.GetError(controles).Length > 0)
                    {
                        total++;
                    }
                    else
                    {
                    }
                }
            }
            return(total);
        }
        private void btnAceptar_Click(object sender, EventArgs e)
        {
            errorProvider.InitializeError();//Lia errores pasados.
            if (string.IsNullOrWhiteSpace(txtCódigo.Text))
            {
                errorProvider.SetError(txtCódigo, "Ingrese código");
            }
            if (!int.TryParse(txtCódigo.Text, out var n))
            {
                errorProvider.SetError(txtCódigo, "Ingrese código númerico de maximo 10 dígitos.");
            }
            if (string.IsNullOrWhiteSpace(txtNombre.Text))
            {
                errorProvider.SetError(txtNombre, "Ingrese nombre del empleado");
            }
            if (string.IsNullOrWhiteSpace(txtApellidos.Text))
            {
                errorProvider.SetError(txtApellidos, "Ingrese apellidos del empleado");
            }
            if (string.IsNullOrWhiteSpace(txtTitulo.Text))
            {
                errorProvider.SetError(txtTitulo, "Ingrese título del empleado");
            }
            if (errorProvider.GetError())
            {
                this.DialogResult = DialogResult.None;
                return;
            }
            //Insertar un nuevo dato.
            int      Código    = Convert.ToInt32(txtCódigo.Text);
            string   Nombre    = txtNombre.Text;
            string   Apellidos = txtApellidos.Text;
            string   Dirección = txtDirección.Text;
            string   Título    = txtTitulo.Text;
            DateTime Fecha     = txtFecha.Value;

            if (accion.Equals("Nuevo"))
            {
                Employees empleado = new Employees
                {
                    EmployeeID    = Código,
                    FirstName     = Nombre,
                    LastName      = Apellidos,
                    Address       = Dirección,
                    BirthDate     = Fecha,
                    Title         = Título,
                    bitHabilitado = true
                };
                try
                {
                    //Agregar
                    db.Employees.InsertOnSubmit(empleado);
                    //Actualizar en base de datos
                    db.SubmitChanges();
                    MessageBox.Show("Empleado agregado correctamente.", "Insertar", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Ocurrió un error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                var empleado = db.Employees.Where(em => em.EmployeeID.Equals(id)).Single();
                empleado.FirstName = Nombre;
                empleado.LastName  = Apellidos;
                empleado.Address   = Dirección;
                empleado.BirthDate = Fecha;
                empleado.Title     = Título;
                try
                {
                    //Actualizar en base de datos
                    db.SubmitChanges();
                    MessageBox.Show("Empleado actualizado correctamente.", "Actualizar", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Ocurrió un error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }