Ejemplo n.º 1
0
        protected void BuildCollection()
        {
            PTCData db = null;

            try
            {
                db             = new PTCData();
                DataCollection = db.Products.ToList();

                if (DataCollection != null && DataCollection.Count > 0)
                {
                    if (!string.IsNullOrEmpty(SearchEntity.ProductName))
                    {
                        //DataCollection = DataCollection.FindAll(
                        //    p => p.ProductName.
                        //    StartsWith(SearchEntity.ProductName,
                        //        StringComparison.InvariantCultureIgnoreCase));

                        DataCollection = DataCollection.FindAll(
                            p => p.ProductName.IndexOf(
                                SearchEntity.ProductName, StringComparison.OrdinalIgnoreCase) >= 0);
                    }
                }
            }
            catch (Exception ex)
            {
                Publish(ex, "Error while loading products.");
            }
        }
Ejemplo n.º 2
0
        public void Search()
        {
            PTCData db = null;

            ResetException();
            try
            {
                db = new PTCData();

                // Perform Search
                Products = db.Products.Where(p =>
                                             (SearchEntity.CategoryId == 0 ? true :
                                              p.Category.CategoryId == SearchEntity.CategoryId) &&
                                             (string.IsNullOrEmpty(SearchEntity.ProductName) ? true :
                                              p.ProductName.StartsWith(SearchEntity.ProductName))).
                           OrderBy(p => p.ProductName).ToList();

                SetUIState(PDSAPageModeEnum.List);
            }
            catch (Exception ex)
            {
                LastException = ex;
                Message       = "ERROR Searching for Products";
            }
        }
Ejemplo n.º 3
0
        public void Save()
        {
            PTCData db = null;

            Messages.Clear();
            ResetException();
            try
            {
                db = new PTCData();

                // Ensure the correct category is set
                if (Entity.CategoryId == null)
                {
                    Entity.Category = db.Categories.Find(Entity.Category.CategoryId);
                }
                else
                {
                    Entity.Category = db.Categories.Find(Entity.CategoryId);
                }
                Entity.CategoryId = Entity.Category.CategoryId;

                // Either Update or Insert product
                if (PageMode == PDSAPageModeEnum.Edit)
                {
                    db.Entry(Entity).State = EntityState.Modified;
                    db.SaveChanges();
                }
                else if (PageMode == PDSAPageModeEnum.Add)
                {
                    db.Products.Add(Entity);
                    db.SaveChanges();
                }

                // Get all the data again in case anything changed
                Get();
            }
            catch (DbEntityValidationException ex)
            {
                IsValid = false;
                // Validation errors
                foreach (var errors in ex.EntityValidationErrors)
                {
                    foreach (var item in errors.ValidationErrors)
                    {
                        Messages.AddModelError(item.PropertyName, item.ErrorMessage);
                    }
                }

                // Set page state
                SetUIState(PageMode);
            }
            catch (Exception ex)
            {
                LastException = ex;
                Message       = "ERROR Saving Product";
            }
        }
        public void LoadCategories()
        {
            PTCData db = null;

            ResetException();
            try {
                db = new PTCData();

                // Load categories
                Categories.AddRange(db.Categories);
            }
            catch (Exception ex) {
                LastException = ex;
                Message       = "ERROR Loading Categories";
            }
        }
        public Product Get(int productId)
        {
            PTCData db = null;

            ResetException();
            try {
                db = new PTCData();

                Entity = db.Products.Find(productId);
            }
            catch (Exception ex) {
                LastException = ex;
                Message       = "ERROR Getting Product with ID: " + productId.ToString();
            }

            return(Entity);
        }
        public void Get()
        {
            PTCData db = null;

            ResetException();
            try {
                db = new PTCData();

                Products = db.Products.OrderBy(p => p.ProductName).ToList();

                SetUIState(PDSAPageModeEnum.List);
            }
            catch (Exception ex) {
                LastException = ex;
                Message       = "ERROR Loading Products";
            }
        }
Ejemplo n.º 7
0
        protected virtual void GetEntity()
        {
            PTCData db = null;

            try
            {
                db = new PTCData();
                // Get the entity
                if (!string.IsNullOrEmpty(EventArgument))
                {
                    Entity =
                        db.Products.Find(Convert.ToInt32(EventArgument));
                }
            }
            catch (Exception ex)
            {
                Publish(ex, "Error Retrieving Product With ID=" +
                        EventArgument);
            }
        }
Ejemplo n.º 8
0
        public virtual void Delete()
        {
            PTCData db = null;

            try
            {
                db = new PTCData();
                if (!string.IsNullOrEmpty(EventArgument))
                {
                    Entity = db.Products.Find(Convert.ToInt32(EventArgument));
                    db.Products.Remove(Entity);
                    db.SaveChanges();
                    PageMode = PDSAPageModeEnum.List;
                }
            }
            catch (Exception ex)
            {
                Publish(ex, "Error Deleting Product With ID=" +
                        Entity.ProductName);
            }
        }
        public void Delete(int productId)
        {
            PTCData db = null;

            ResetException();
            try {
                db = new PTCData();

                Product product = db.Products.Find(productId);

                // Attempt to delete
                db.Products.Remove(product);
                db.SaveChanges();

                // Reload all Products
                Get();
            }
            catch (Exception ex) {
                LastException = ex;
                Message       = "ERROR Deleting Product with ID: " + productId.ToString();
            }
        }
Ejemplo n.º 10
0
        protected void Update()
        {
            PTCData db = null;

            try
            {
                db = new PTCData();
                // Do editing here
                db.Entry(Entity).State = EntityState.Modified;
                db.SaveChanges();
                PageMode = PDSAPageModeEnum.List;
            }
            catch (DbEntityValidationException ex)
            {
                IsValid = false;
                ValidationErrorsToMessages(ex);
            }
            catch (Exception ex)
            {
                Publish(ex, "Error Updating Product With ID=" +
                        Entity.ProductId.ToString());
            }
        }
Ejemplo n.º 11
0
        protected void Insert()
        {
            PTCData db = null;

            try
            {
                db = new PTCData();
                // Do editing here
                db.Products.Add(Entity);
                db.SaveChanges();
                PageMode = PDSAPageModeEnum.List;
            }
            catch (DbEntityValidationException ex)
            {
                IsValid = false;
                ValidationErrorsToMessages(ex);
            }
            catch (Exception ex)
            {
                Publish(ex, "Error Inserting New Product: '" +
                        Entity.ProductName + "'");
            }
        }