protected void AddCategory_Click(object sender, EventArgs e) { try { Category item = new Category(); item.CategoryName = CategoryName.Text; if (!string.IsNullOrEmpty(Description.Text)) { item.Description = Description.Text; } item.Picture = ImageUploadHelpers.GetUploadedPicture(CategoryImageUpload); item.PictureMimeType = ImageUploadHelpers.GetMimeType(CategoryImageUpload); InventoryPurchasingController controller = new InventoryPurchasingController(); int addedCategoryID = controller.AddCategory(item); // Update the form and give feedback to the user PopulateCategoryDropdown(); CurrentCategories.SelectedValue = addedCategoryID.ToString(); CategoryID.Text = addedCategoryID.ToString(); if (item.Picture != null) { string base64String = Convert.ToBase64String(item.Picture); Picture.ImageUrl = string.Format("data:{0};base64,{1}", item.PictureMimeType, base64String); } else { Picture.ImageUrl = "~/Images/NoImage_172x120.gif"; } MessageLabel.Text = "New category added"; } catch (Exception ex) { MessageLabel.Text = ex.Message; MessagePanel.CssClass = "alert alert-danger alert-dismissible"; MessagePanel.Visible = true; } }
protected void UpdateCategory_Click(object sender, EventArgs e) { int theCategoryId; if (IsValid) { if (int.TryParse(CategoryID.Text, out theCategoryId)) { try { byte[] uploadedPicture = ImageUploadHelpers.GetUploadedPicture(CategoryImageUpload); string mimeType = ImageUploadHelpers.GetMimeType(CategoryImageUpload); if (uploadedPicture != null && DeletePicture.Checked) { MessageLabel.Text = "Unclear input.<br />" + "You selected 'Check to delete picture' and uploaded an image."; } else { // 1) Get the picture to be used in the update InventoryPurchasingController controller = new InventoryPurchasingController(); if (DeletePicture.Checked) { uploadedPicture = null; mimeType = null; } else if (uploadedPicture == null) { // default to the existing picture Category existing = controller.LookupCategory(theCategoryId); uploadedPicture = existing.Picture; mimeType = existing.PictureMimeType; } // 2) Create the Category object Category item = new Category(); item.CategoryID = theCategoryId; item.CategoryName = CategoryName.Text; if (!string.IsNullOrEmpty(Description.Text)) { item.Description = Description.Text; } item.Picture = uploadedPicture; item.PictureMimeType = mimeType; // 3) Update the database controller.UpdateCategory(item); PopulateCategoryDropdown(); CurrentCategories.SelectedValue = item.CategoryID.ToString(); if (item.Picture != null) { string base64String = Convert.ToBase64String(item.Picture); Picture.ImageUrl = string.Format("data:{0};base64,{1}", item.PictureMimeType, base64String); } else { Picture.ImageUrl = "~/Images/NoImage_172x120.gif"; } MessageLabel.Text = "Category was updated."; } } catch (Exception ex) { MessageLabel.Text = ex.Message; MessagePanel.CssClass = "alert alert-danger alert-dismissible"; MessagePanel.Visible = true; } } else { MessageLabel.Text = "Lookup and existing category before attempting an update."; } } }