protected void ButtonUpdateProduct_Click(object sender, EventArgs e) { ClearAlerts(); try { Product.productName = TextBoxProductName.Text; Product.description = TextBoxProductDescription.Text; Product.categoryID = int.Parse(ddlCategories.SelectedValue); Product.price = decimal.Parse(TextBoxPrice.Text); Product.categoryID = int.Parse(ddlCategories.SelectedValue.ToString()); Product.onHand = int.Parse(TextBoxOnHand.Text); Product.onOrder = int.Parse(TextBoxOnOrder.Text); if (FileUploadImage.HasFile) { var oldFile = Product.File; if (oldFile != null) { theGateContext.Files.Attach(oldFile); theGateContext.Files.Remove(oldFile); } Stream fs = FileUploadImage.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); Byte[] fileBytes = br.ReadBytes((Int32)fs.Length); var newFile = new TheGateWebSite.Model.File() { ContentType = FileUploadImage.PostedFile.ContentType, Size = FileUploadImage.PostedFile.ContentLength, Data = fileBytes, name = FileUploadImage.PostedFile.FileName }; theGateContext.Files.Add(newFile); Product.imageID = newFile.fileID; PanelImageFile.Visible = true; ImageFile.ImageUrl = "~/Handlers/Image.ashx?ID=" + Product.imageID.ToString(); } theGateContext.SaveChanges(); ShowSuccess("Product successfully updated."); } catch (Exception ex) { ShowError("Error updating product: " + ex.Message); } }
protected void ButtonAddProduct_Click(object sender, EventArgs e) { try { var newProduct = new Product() { productName = TextBoxProductName.Text, description = TextBoxProductDescription.Text, categoryID = int.Parse(ddlCategories.SelectedValue), price = decimal.Parse(TextBoxPrice.Text) }; if (FileUploadImage.HasFile) { Stream fs = FileUploadImage.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); Byte[] fileBytes = br.ReadBytes((Int32)fs.Length); var newFile = new TheGateWebSite.Model.File() { ContentType = FileUploadImage.PostedFile.ContentType, Size = FileUploadImage.PostedFile.ContentLength, Data = fileBytes, name = FileUploadImage.PostedFile.FileName }; theGateContext.Files.Add(newFile); newProduct.imageID = newFile.fileID; } theGateContext.Products.Add(newProduct); theGateContext.SaveChanges(); TextBoxProductName.Text = string.Empty; TextBoxProductDescription.Text = string.Empty; TextBoxPrice.Text = string.Empty; ddlCategories.SelectedIndex = 0; LoadProducts(); } catch (Exception ex) { PanelAddAlertError.Visible = true; LabelAddAlert.Text = "Error adding new product: " + ex.Message; } }