protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { userName = (string)(Session["userName"]); EditModeLabel.Visible = false; // Assign session information to variables int clickedRecipe = (int)(Session["clickedRecipe"]); isOnFavorites = (bool)(Session["isOnFavorites"]); // Set text boxes to read only so they can't be modified unless they select update btn IngredientsBox.ReadOnly = true; InstructionsBox.ReadOnly = true; LabelBox.ReadOnly = true; SaveBtn.Visible = false; CancelBtn.Visible = false; // If else statements to display recipe from the correct database if (isOnFavorites) { // Use favorite manager to display recipe from favorites list FavoriteRecipeMgr favMgr = new FavoriteRecipeMgr(); list = favMgr.retrieveRecipes(userName); // Hide Add to favorite button since user is currently viewing favorite recipe AddToFavButton.Visible = false; // Show Update button to allow users to modify their favorite recipes UpdateBtn.Visible = true; // Change the text of the delete button so user knows which database they're viewing DeleteButton.Text = "Delete From Favorites"; } else { // Use recipe manager to display recipe from regular recipe list RecipeMgr recipeMgr = new RecipeMgr(); list = recipeMgr.retrieveRecipes(); // Show favorite button since recipe can be added to favorites AddToFavButton.Visible = true; // Hide update button since users are not allowed to edit preloaded recipes UpdateBtn.Visible = false; // Change text of delete button DeleteButton.Text = "Delete From Recipes"; } // Retrieve desired recipe and display recipe information in text boxes recipeDisplayed = list[clickedRecipe]; LabelBox.Text = list[clickedRecipe].RecipeName; IngredientsBox.Text = list[clickedRecipe].RecipeIngredients; InstructionsBox.Text = list[clickedRecipe].RecipeInstructions; string strBase64 = Convert.ToBase64String(list[clickedRecipe].RecipeImage); RecipeImage.ImageUrl = "data:Image/jpg;base64," + strBase64; } }
protected void AddToFavButton_Click(object sender, EventArgs e) { FavoriteRecipeMgr favMgr = new FavoriteRecipeMgr(); RecipeMgr recipeMgr = new RecipeMgr(); isOnFavorites = (bool)(Session["isOnFavorites"]); int clickedRecipe = (int)(Session["clickedRecipe"]); userName = (string)(Session["userName"]); list = recipeMgr.retrieveRecipes(); recipeDisplayed = list[clickedRecipe]; // Try to store recipe to favorite list and display success message try { favMgr.addRecipe(recipeDisplayed, userName); Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Recipe Successfully added to Favorite List!!!');</script>"); } // Catch exception for debugging purposes and display error message catch (Exception ex) { Debug.Write(ex.StackTrace); Debug.WriteLine(ex.ToString()); Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Error! Unable to save recipe to Favorite List!!!');</script>"); } }
protected void DeleteButton_Click(object sender, EventArgs e) { isOnFavorites = (bool)(Session["isOnFavorites"]); int clickedRecipe = (int)(Session["clickedRecipe"]); userName = (string)(Session["userName"]); // If else statement to delete recipe from correct database if (isOnFavorites) { FavoriteRecipeMgr favMgr = new FavoriteRecipeMgr(); list = favMgr.retrieveRecipes(userName); recipeDisplayed = list[clickedRecipe]; // Try to delete recipe from favorites and redirect to favorites form try { favMgr.deleteRecipe(recipeDisplayed.RecipeId, userName); bool recipeDeleted = true; Session.Add("recipeDeleted", recipeDeleted); Server.Transfer("FavoriteListForm.aspx"); } // Catch exception for debugging and display error message catch (Exception ex) { Debug.Write(ex.StackTrace); Debug.WriteLine(ex.ToString()); Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Error! Unable to delete recipe from Favorite List!!!');</script>"); } } else { RecipeMgr recipeMgr = new RecipeMgr(); list = recipeMgr.retrieveRecipes(); recipeDisplayed = list[clickedRecipe]; // Try to delete recipe from regular recipe list and redirect to recipe list form try { recipeMgr.deleteRecipe(recipeDisplayed.RecipeId); bool recipeDeleted = true; Session.Add("recipeDeleted", recipeDeleted); Server.Transfer("RecipeListForm.aspx"); } // Catch exception for debugging and display error message catch (Exception ex) { Debug.Write(ex.StackTrace); Debug.WriteLine(ex.ToString()); Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Error! Unable to delete recipe from Recipe List!!!');</script>"); } } }
protected void SaveButton_Click(object sender, EventArgs e) { try { // Assign text values to recipe and try to save it to database Recipe recipeToAdd = new Recipe(NameBox.Text, IngredientsBox.Text, InstructionsBox.Text); if (!ImageUpload.Equals(null)) { HttpPostedFile postedFile = ImageUpload.PostedFile; string fileName = Path.GetFileName(postedFile.FileName); string fileExtenstion = Path.GetExtension(fileName); int fileSize = postedFile.ContentLength; if (fileExtenstion.ToLower() == ".jpg" || fileExtenstion.ToLower() == ".bmp" || fileExtenstion.ToLower() == ".gif" || fileExtenstion.ToLower() == ".png") { Stream stream = postedFile.InputStream; BinaryReader binReader = new BinaryReader(stream); byte[] bytes = binReader.ReadBytes((int)stream.Length); recipeToAdd.RecipeSize = fileSize; recipeToAdd.RecipeImage = bytes; } else { throw new ApplicationException(); } } else { ImageConverter imgConverter = new ImageConverter(); byte[] bytes = (byte[])imgConverter.ConvertTo( Properties.Resources.MyRecipes, typeof(byte[])); recipeToAdd.RecipeSize = 0; recipeToAdd.RecipeImage = bytes; } RecipeMgr recipeMgr = new RecipeMgr(); recipeMgr.addRecipe(recipeToAdd); bool recipeAdded = true; Session.Add("recipeAdded", recipeAdded); Server.Transfer("RecipeListForm.aspx"); } catch (ApplicationException ex) { // Display what type of file can be used for recipe image Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Error! Only images .jpg, .png, .bmp, or " + ".png can be uploaded!!');</script>"); // For Debugging Debug.Write(ex.StackTrace); Debug.WriteLine(ex.ToString()); } catch (Exception ex) { // Display alert message that recipe could not be added Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Error! Unable to add recipe to list!');</script>"); // For Debugging Debug.Write(ex.StackTrace); Debug.WriteLine(ex.ToString()); } }
protected void Page_Load(object sender, EventArgs e) { string userName = User.Identity.Name; if (userName == "") { userName = "******"; } Session.Add("userName", userName); try { // Preload reg recipe list database to be displayed on grid view RecipeWS.RecipeWebService ws = new RecipeWS.RecipeWebService(); RecipeWS.WSRecipe[] wsList = ws.GetWebServiceRecipes(); int count = wsList.Length; for (int i = 0; i < count; i++) { Recipe recipe = new Recipe(); recipe.RecipeName = wsList[i].RecipeName; recipe.RecipeIngredients = wsList[i].RecipeIngredients; recipe.RecipeInstructions = wsList[i].RecipeInstructions; recipe.RecipeSize = wsList[i].RecipeSize; recipe.RecipeImage = wsList[i].RecipeImage; list.Add(recipe); } } catch { Debug.WriteLine("Unable to load data from Service1Client!"); Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Error! Unable to retrieve recipe list from host!');</script>"); } RecipeMgr recipeMgr = new RecipeMgr(); RecipeLists loadData = new RecipeLists(); // Create the recipe list database with preloaded recipes recipeMgr.storeRecipes(list); // Try to retrieve bool recipeAdded variable if available try { recipeAdded = (bool)(Session["recipeAdded"]); } catch (Exception ex) { Debug.Write(ex.StackTrace); Debug.WriteLine("Form loaded with no recipeAdded variable available."); } // Try to retrieve recipeDeleted bool variable if available try { recipeDeleted = (bool)(Session["recipeDeleted"]); } catch (Exception ex) { Debug.Write(ex.StackTrace); Debug.WriteLine("Form loaded with no recipeDeleted variable available."); } // Display success message if recipe successfully added to list from AddRecipeForm // Change the session bool variables back to false if (recipeAdded) { Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Success! Recipe Added to List!');</script>"); Session["recipeAdded"] = false; } else if (recipeDeleted) { Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Recipe successfully deleted from list!');</script>"); Session["recipeDeleted"] = false; } }