コード例 #1
0
        //public Product Details(int? id)
        //{

        //    var documentType = DBContext.Products
        //        .FirstOrDefault(m => m.productId == id);


        //    return documentType;
        //}

        public void DeleteProduct(int productId)
        {
            Product oProduct = DBContext.Products.Find(productId);

            DBContext.Products.Remove(oProduct);
            try
            {
                DBContext.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                                                       validationErrors.Entry.Entity.ToString(),
                                                       validationError.ErrorMessage);
                        // raise a new exception nesting
                        // the current instance as InnerException
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }

            Response.Write("<script>javascript:alert('Product Deleted successfully');</script>");
        }
コード例 #2
0
        public void AddToCart(int id)
        {
            // Retrieve the product from the database.
            ShoppingCartId = GetCartId();

            var cartItem = _db.CartItems.SingleOrDefault(
                c => c.CartId == ShoppingCartId &&
                c.ProductId == id);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists.
                cartItem = new CartItem
                {
                    ItemId    = Guid.NewGuid().ToString(),
                    ProductId = id,
                    CartId    = ShoppingCartId,
                    Product   = _db.Products.SingleOrDefault(
                        p => p.productId == id),
                    Quantity    = 1,
                    Username    = HttpContext.Current.User.Identity.Name,
                    DateCreated = DateTime.Now
                };

                _db.CartItems.Add(cartItem);
            }
            else
            {
                // If the item does exist in the cart,
                // then add one to the quantity.
                cartItem.Quantity++;
            }
            _db.SaveChanges();
        }
コード例 #3
0
        protected void AddProductButton_Click(object sender, EventArgs e)
        {
            string str = FileUpload1.FileName;

            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/site/images/" + str));
            string Image = str;
            //string name = TextBox1.Text;
            // Add product data to DB.
            Product newProduct = new Product
            {
                productName = AddProductName.Text,
                //productDescription = AddProductIdentifier.Text,
                unitPrice = int.Parse(AddUnitPrice.Text),
                imageURL  = Image
            };

            //newProduct.Date_Of_Birth = AddDOB.Text;
            DBContext.Products.Add(newProduct);
            DBContext.SaveChanges();
            Response.Write("<script>javascript:alert('Product Added  successfully');</script>");

            AddProductName.Text = "";
            AddUnitPrice.Text   = "";
            //AddImageURL.Text = "";
        }
コード例 #4
0
        public bool AddProduct(string ProductName, string ProductDesc, string ProductPrice, string ProductImagePath)
        {
            var myProduct = new Product();

            myProduct.productName        = ProductName;
            myProduct.productDescription = ProductDesc;
            myProduct.unitPrice          = Convert.ToDecimal(ProductPrice);
            myProduct.imageURL           = ProductImagePath;
            //myProduct.CategoryID = Convert.ToInt32(ProductCategory);

            using (TechEntities _db = new TechEntities())
            {
                // Add product to DB.
                _db.Products.Add(myProduct);
                _db.SaveChanges();
            }
            // Success.
            return(true);
        }
コード例 #5
0
        public HttpResponseMessage AddUser(UserViewModel users)
        {
            Response response = new Response();

            try
            {
                using (var dbContext = new TechEntities())
                {
                    var Req = dbContext.Users.FirstOrDefault(x => x.UserName == users.UserName);
                    if (Req == null)
                    {
                        var objuser = new User
                        {
                            UserName    = users.UserName,
                            UserId      = users.UserId,
                            GroupId     = users.GroupId,
                            UserStatus  = users.UserStatus,
                            CreatedDate = DateTime.Now
                        };
                        dbContext.Users.Add(objuser);
                        dbContext.SaveChanges();
                        var NewRecord = dbContext.Users.FirstOrDefault(x => x.UserName == users.UserName);
                        response.success = true;
                        response.result  = NewRecord;
                        response.message = "User added successfully ";
                    }
                    else
                    {
                        response.success = false;
                        response.message = "User already exist";
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                response.success = false;
                response.message = "User adding failed, Please contact your administrator";
            }
            return(Request.CreateResponse(HttpStatusCode.OK, response));
        }
コード例 #6
0
        public HttpResponseMessage AddGroup(GroupViewModel groups)
        {
            HttpResponseMessage httpresponse = new HttpResponseMessage();
            Response            response     = new Response();

            try
            {
                using (var dbContext = new TechEntities())
                {
                    var Req = dbContext.Groups.FirstOrDefault(x => x.GroupName == groups.GroupName);
                    if (Req == null)
                    {
                        var objGroup = new Group {
                            GroupId     = groups.GroupId,
                            GroupName   = groups.GroupName,
                            GroupStatus = groups.GroupStatus
                        };
                        dbContext.Groups.Add(objGroup);
                        dbContext.SaveChanges();
                        var NewRecord = dbContext.Groups.FirstOrDefault(x => x.GroupName == groups.GroupName);
                        response.success = true;
                        response.result  = NewRecord;
                        response.message = "Group added successfully ";
                    }
                    else
                    {
                        response.success = false;
                        response.message = "Group already exist";
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                response.success = false;
                response.message = "Group adding failed, Please contact your administrator";
            }
            return(httpresponse);
        }