/// <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(); } }