protected void AddProductButton_Click(object sender, EventArgs e) { Boolean fileOK = false; String path = Server.MapPath("~/Images/"); if (ProductImage.HasFile) { String fileExtension = System.IO.Path.GetExtension(ProductImage.FileName).ToLower(); String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" }; for (int i = 0; i < allowedExtensions.Length; i++) { if (fileExtension == allowedExtensions[i]) { fileOK = true; } } } if (fileOK) { try { // Save to Images folder. ProductImage.PostedFile.SaveAs(path + ProductImage.FileName); // Save to Images/Thumbs folder. ProductImage.PostedFile.SaveAs(path + "Thumbs/" + ProductImage.FileName); } catch (Exception ex) { LabelAddStatus.Text = ex.Message; } // Add product data to DB. AddProducts products = new AddProducts(); bool addSuccess = products.AddProduct(AddProductName.Text, AddProductDescription.Text, AddProductPrice.Text, DropDownAddRestaurant.SelectedValue, ProductImage.FileName); if (addSuccess) { // Reload the page. string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count()); Response.Redirect(pageUrl + "?ProductAction=add"); } else { LabelAddStatus.Text = "Unable to add new product to database."; } } else { LabelAddStatus.Text = "Unable to accept file type."; } }
public void AddProductToMenu() { AddProducts adder = new AddProducts(); ProductContext _db = new ProductContext(); string n = "cheese"; string d = "some cheesy cheese"; string p = "3.50"; string i = "/Images/10101.jpg"; string r = "1"; adder.AddProduct(n, d, p, r, i); IQueryable<Product> query = _db.Products; IQueryable<Product> refined = from Product prod in query where prod.ProductName.Equals(n) select prod; Product inserted = refined.First(); Assert.AreEqual(n, inserted.ProductName); Assert.AreEqual(d, inserted.Description); Assert.AreEqual(p, String.Format("{0:0.00}", inserted.UnitPrice)); Assert.AreEqual(i, inserted.ImagePath); }