Exemplo n.º 1
0
        public void add(Recipe recipe, string userName)
        {
            List <Recipe> list       = new List <Recipe>();
            RecipeLists   sortedList = new RecipeLists();

            // Retrieve all recipes in database if any and add new recipe to list
            list = retrieveAllRecipes(userName);
            list.Add(recipe);

            // Sort all the recipes including newly added recipe in alphabetical order
            list = sortedList.sortRecipeList(list);

            // Try to drop the old table and create recipe list database
            try
            {
                dropTable(userName);
                create(list, userName);
                var page = HttpContext.Current.CurrentHandler as Page;
                ScriptManager.RegisterStartupScript(page, page.GetType(), "Scripts", "<script>alert('Recipe Successfully Added to Favorite List!!!');</script>", false);
            }

            // Catch sqlite exception to see if database error occurs
            catch (SqlException e3)
            {
                Debug.WriteLine("Unable to add recipe!", e3.ToString());
                Debug.Write(e3.StackTrace);
            }

            // Catch any other exception for debugging
            catch (Exception e3)
            {
                Debug.WriteLine(e3.ToString());
                Debug.Write(e3.StackTrace);
            }
        }
Exemplo n.º 2
0
        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;
            }
        }