Exemple #1
0
        public static int InsertProducto(Producto obj)
        {
            if (obj == null)
            {
                throw new ArgumentException("El objeto a insertar no puede ser nulo");
            }

            if (string.IsNullOrEmpty(obj.Nombre))
            {
                throw new ArgumentException("El nombre no puede ser nulo o vacio");
            }

            if (obj.Precio <= 0)
            {
                throw new ArgumentException("El precio no puede ser menor o igual que ceo");
            }

            if (obj.Stock < 0)
            {
                throw new ArgumentException("El stock no puede ser negativo");
            }

            int?id = null;

            ProductoDSTableAdapters.ProductoTableAdapter adapter = new ProductoDSTableAdapters.ProductoTableAdapter();
            adapter.Insert(obj.Nombre, obj.Precio, obj.Stock, ref id);

            if (id == null || id.Value <= 0)
            {
                throw new Exception("La llave primaria no se generó correctamente");
            }

            return(id.Value);
        }