Example #1
0
        /// <summary>
        /// Saves changes to DB
        /// </summary>
        /// <returns>true if a new record was created; else false</returns>
        private bool saveChanges()
        {
            currentSelection = currentSelection is null ? new ProductNutrition() : currentSelection;
            bool newCreated = currentSelection.Value < 0;;

            currentSelection.NutritionId   = (cmbNutrType.SelectedItem as Nutrition).Id;
            currentSelection.ProductItemId = (cmbProduct.SelectedItem as ProductItem).Id;
            currentSelection.Value         = double.Parse(txtValue.Text);
            currentSelection.ValueType     = valueTypes[cmbValType.SelectedItem as string];

            if (newCreated)
            {
                //The current product-nutrition combination does not exist in the database
                //Create new combination of product-nutrition
                currentSelection.Create();
                MessageBox.Show("Sucessfully added nutrition");
            }
            else
            {
                //The current product-nutrition combination exists in the database
                //Update product-nutrition values for the current selection
                currentSelection.Update();
                MessageBox.Show("Sucessfully updated nutrition");
            }

            return(newCreated);
        }
Example #2
0
        /// <summary>
        /// Load nutrition data for current selection of product and nutrition type
        /// </summary>
        private void loadExistingData()
        {
            if (cmbNutrType.SelectedItem != null && cmbProduct.SelectedItem != null)
            {
                short nutritionID = (cmbNutrType.SelectedItem as Nutrition).Id;
                int   prodItemID  = (cmbProduct.SelectedItem as ProductItem).Id;
                currentSelection = ProductNutrition.Retrieve(nutritionID, prodItemID);

                if (currentSelection is null)
                {
                    txtValue.Text = "";
                }
                else
                {
                    txtValue.Text           = currentSelection.Value.ToString();
                    cmbValType.SelectedItem = valueTypes.First <KeyValuePair <string, char> >(
                        (elem) => { return(elem.Value == currentSelection.ValueType); }
                        ).Key;
                }
            }
        }
Example #3
0
        /// <summary>
        /// Move onto the final step of saving
        /// changes in the form to the DB
        /// </summary>
        private void proceedToSave()
        {
            bool newCreated = true;

            try
            {
                newCreated = saveChanges();
            }
            catch (SqlException se)
            {
                currentSelection = newCreated ? null : currentSelection;
                if (se.Message.Contains("duplicate key"))
                {
                    MessageBox.Show("Product already has a value for this nutrition type");
                }
            }
            catch
            {
                currentSelection = newCreated ? null : currentSelection;
                MessageBox.Show("Something went wrong");
            }
        }