public static IEnumerable <Articulos> Buscar(string Nombre, bool?Activo, int numeroPagina, out int RegistrosTotal)
        {
            //ref Entity Framework

            using (PymesEntities db = new PymesEntities())     //el using asegura el db.dispose() que libera la conexion de la base
            {
                IQueryable <Articulos> consulta = db.Articulos;
                // aplicar filtros
                //ref LinQ
                //Expresiones lambda, metodos de extension
                if (!string.IsNullOrEmpty(Nombre))
                {
                    consulta = consulta.Where(x => x.Nombre.ToUpper().Contains(Nombre.ToUpper()));    // equivale al like '%TextoBuscar%'
                }
                if (Activo != null)
                {
                    consulta = consulta.Where(x => x.Activo == Activo);
                }
                RegistrosTotal = consulta.Count();

                // ref EF; consultas paginadas
                int RegistroDesde = (numeroPagina - 1) * 10;
                var Lista         = consulta.OrderBy(x => x.Nombre).Skip(RegistroDesde).Take(10).ToList(); // la instruccion sql recien se ejecuta cuando hacemos ToList()
                return(Lista);
            }
        }
 public static Articulos BuscarPorId(int sId)
 {
     using (PymesEntities db = new PymesEntities())
     {
         return(db.Articulos.Find(sId));
     }
 }
Esempio n. 3
0
        public static void Grabar(Empleado emple)
        {
            //validar campos
            string erroresValidacion = "";

            if (string.IsNullOrEmpty(emple.nom_emple))
            {
                erroresValidacion += "Nombre de empleado es un dato requerido ";
            }
            if (string.IsNullOrEmpty(emple.ape_emple))
            {
                erroresValidacion += "Apellido de empleado es un dato requerido ";
            }
            if (emple.sueldo <= 0)
            {
                erroresValidacion += "Sueldo es un dato requerido ";
            }
            if (string.IsNullOrEmpty((emple.fecha_alta).ToString()))
            {
                erroresValidacion += "Verifique la fecha ";
            }
            if (string.IsNullOrEmpty(emple.id_departamento.ToString()))
            {
                erroresValidacion += "Verifique el departamento";
            }
            if (!string.IsNullOrEmpty(erroresValidacion))
            {
                throw new Exception(erroresValidacion);
            }

            //grabar registro
            using (PymesEntities db = new PymesEntities())
            {
                try
                {
                    if (emple.id_emple > 0)
                    {
                        db.Entry(emple).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        db.Empleado.Add(emple);
                        db.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    if (ex.ToString().Contains("UK_Nombre"))
                    {
                        throw new ApplicationException("Ya existe otro Trabajador con ese Nombre");
                    }

                    else
                    {
                        throw;
                    }
                }
            }
        }
Esempio n. 4
0
 public static Empleado BuscarPorId(int sId)
 {
     using (PymesEntities db = new PymesEntities())
     {
         return(db.Empleado.Find(sId));
     }
 }
Esempio n. 5
0
 public static Departamento BuscarPorId(int Id)
 {
     using (PymesEntities db = new PymesEntities())
     {
         return(db.Departamento.Find(Id));
     }
 }
 public static void ActivarDesactivar(int IdArticulo)
 {
     using (PymesEntities db = new PymesEntities())
     {
         //ref Entity Framework; ejecutar codigo sql directo
         db.Database.ExecuteSqlCommand("Update Articulos set Activo = case when ISNULL(activo,1)=1 then 0 else 1 end  where IdArticulo = @IdArticulo",
                                       new SqlParameter("@IdArticulo", IdArticulo)
                                       );
     }
 }
Esempio n. 7
0
 public static void DeleteDpto(int Id)
 {
     using (PymesEntities db = new PymesEntities())
     {
         //ref Entity Framework; ejecutar codigo sql directo
         db.Database.ExecuteSqlCommand("Delete from Departamento where id_dpto = @Id",
                                       new SqlParameter("@Id", Id)
                                       );
     }
 }
        public static void Grabar(Articulos DtoSel)
        {
            // validar campos
            string erroresValidacion = "";

            if (string.IsNullOrEmpty(DtoSel.Nombre))
            {
                erroresValidacion += "Nombre es un dato requerido; ";
            }
            if (DtoSel.Precio == null || DtoSel.Precio == 0)
            {
                erroresValidacion += "Precio es un dato requerido; ";
            }
            if (!string.IsNullOrEmpty(erroresValidacion))
            {
                throw new Exception(erroresValidacion);
            }

            // grabar registro
            using (PymesEntities db = new PymesEntities())
            {
                try
                {
                    if (DtoSel.IdArticulo != 0)
                    {
                        db.Entry(DtoSel).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        db.Articulos.Add(DtoSel);
                        db.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    if (ex.ToString().Contains("UK_Articulos_Nombre"))
                    {
                        throw new ApplicationException("Ya existe otro Artículo con ese Nombre");
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Esempio n. 9
0
        //************************************************************************************



        public static void Grabar(Departamento dpto)
        {
            //validar campos
            string erroresValidacion = "";

            if (string.IsNullOrEmpty(dpto.nombre_dpto))
            {
                erroresValidacion += "Descripcion de dpto es un dato requerido ";
            }
            //if (dpto.Precio == null || dpto.Precio == 0)
            //    erroresValidacion += "Precio es un dato requerido; ";
            if (!string.IsNullOrEmpty(erroresValidacion))
            {
                throw new Exception(erroresValidacion);
            }

            //grabar registro
            using (PymesEntities db = new PymesEntities())
            {
                try
                {
                    if (dpto.id_dpto > 0)
                    {
                        db.Entry(dpto).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        db.Departamento.Add(dpto);
                        db.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    if (ex.ToString().Contains("UK_Nombre_Dpto"))
                    {
                        throw new ApplicationException("Ya existe otro Departamento con ese Nombre");
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }