// Add a new item to the collection
        public void AddNewItem(
            string id,
            string name,
            string pack,
            decimal price,
            bool active
            )
        {
            // New instance of beverage
            Beverage newBeverageToAdd = new Beverage();

            // Assign properties to the parts of the beverage
            newBeverageToAdd.id     = id;
            newBeverageToAdd.name   = name;
            newBeverageToAdd.pack   = pack;
            newBeverageToAdd.price  = price;
            newBeverageToAdd.active = active;

            // Try catch to make sure id of beverage doesn't already exist
            try
            {
                // Add new beverage to the collection
                _beverageContext.Beverages.Add(newBeverageToAdd);

                // Save changes to database
                _beverageContext.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                // Remove the new beverage from the Database since we can't save it.
                _beverageContext.Beverages.Remove(newBeverageToAdd);
                // Write to Console that there was an error.
                Console.WriteLine("Can't add the record. Already have one with the same ID.");
            }
            catch (Exception e)
            {
                // Remove the new beverage from the Database since we can't save it.
                _beverageContext.Beverages.Remove(newBeverageToAdd);
                // Write to Console that there was an error.
                Console.WriteLine("Can't add the record. Unknown error occured");
            }
        }
Example #2
0
 // Add a new item to the collection
 public void AddNewItem(Beverage beverage)
 {
     try
     {
         _beverageContext.Beverages.Add(beverage); // Takes the beverage passed in and adds it to the database
         _beverageContext.SaveChanges();           // Saves the changes which will show the added beverage
     }
     catch (DbUpdateException)                     // If the user tries to add a beverage that has an ID that matches one already found
     {
         _beverageContext.Beverages.Remove(beverage);
         Console.WriteLine();
         Console.WriteLine("Beverage must have a unique ID");
     }
     catch (Exception) // Catches any other error
     {
         _beverageContext.Beverages.Remove(beverage);
         Console.WriteLine();
         Console.WriteLine("Can't add the beverage due to an unknown error");
     }
 }
        // Add a new item to the database
        public void AddNewBeverage(
            string id,
            string name,
            string pack,
            decimal price,
            bool active,
            Beverage newBeverageToAdd
            )
        {
            // Use a try catch to ensure that they can't add a beverage with an id that already exists
            try
            {
                // Add the new beverage to the Beverage Collection
                _beverageContext.Beverages.Add(newBeverageToAdd);

                // Saving the changes to the database
                _beverageContext.SaveChanges();

                // Display Succession
                DisplayAddBeverageItemSuccess();
            }
            catch (Exception e) // catch (DbUpdateException e)
            {
                // Remove the new beverage form the Beverages Collection since we can't save it.
                // Dont want it to hang around the next time to saveChanges
                _beverageContext.Beverages.Remove(newBeverageToAdd);

                // Error message
                DisplayBeverageAlreadyExistsError();
            }
            //catch (Exception e)
            //{
            //    // Remove the new beverage form the Beverages Collection since we can't save it.
            //    // Dont want it to hang around the next time to saveChanges
            //    _beverageContext.Beverages.Remove(newBeverageToAdd);
            //    // Error message
            //    DisplayItemAlreadyExistsError2();
            //    //userInterface.DisplayItemAlreadyExistsError2();
            //}
        }
Example #4
0
        // Add a new item to the collection
        public void AddNewItem(
            string id,
            string name,
            string pack,
            string price,
            string active
            )
        {
            // Make a new beverage
            Beverage newBeverage = new Beverage();

            // Assign the properties that are passed in to the new item
            newBeverage.id     = id;
            newBeverage.name   = name;
            newBeverage.pack   = pack;
            newBeverage.price  = decimal.Parse(price);
            newBeverage.active = (active == "True");

            // Add the new beverage to the collection of beverages
            beverages.Add(newBeverage);

            // Save the changes to the Entities
            beverageContext.SaveChanges();
        }