protected void btnAddIngredient_Click(object sender, EventArgs e)
        {
            Int32 RecipeID;

            try
            {

                if (!String.IsNullOrEmpty(Request.QueryString["recipe_id"]))
                {
                    //get the recipe id
                    RecipeID = Convert.ToInt32(Request.QueryString["recipe_id"]);
                }
                else
                {
                    //connect
                    using (DefaultConnectionEF conn = new DefaultConnectionEF())
                    {
                        //instantiate a new recipe object in memory
                        Recipe r = new Recipe();

                        //fill the properties of our object from the form inputs
                        r.recipe_name = txtRecipeName.Text;
                        r.directions = txtRecipeDirections.Text;

                        //add the new object and save changes
                        conn.Recipes.Add(r);
                        conn.SaveChanges();

                        //get recipe info
                        var Recipe = (from rec in conn.Recipes
                                        where rec.recipe_name == r.recipe_name && rec.directions == r.directions
                                        select new { rec.recipe_id}).FirstOrDefault();

                        RecipeID = Recipe.recipe_id;
                    }
                }

                Response.Redirect("~/Admin/ingredient.aspx?recipe_id=" + RecipeID);
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (DefaultConnectionEF conn = new DefaultConnectionEF())
            {
                //instantiate a new student object in memory
                Ingredient i = new Ingredient();
                Measurement m = new Measurement();
                Int32 RecipeID = Convert.ToInt32(Request.QueryString["recipe_id"]);

                //decide if updating or adding, then save
                if (!String.IsNullOrEmpty(Request.QueryString["ingredient_id"]))
                {
                    Int32 IngredientID = Convert.ToInt32(Request.QueryString["ingredient_id"]);

                    i = (from ing in conn.Ingredients
                            join mes in conn.Measurements on ing.ingredient_id equals mes.ingredient_id
                            where ing.ingredient_id == IngredientID
                            select ing).FirstOrDefault();

                    //fill the properties of our object from the form inputs
                    i.Measurements.FirstOrDefault().measurement = txtMeasurement.Text;
                    i.Measurements.FirstOrDefault().unit = ddlUnit.SelectedValue;
                }

                i.ingredient_name = txtIngredientName.Text;

                if (String.IsNullOrEmpty(Request.QueryString["ingredient_id"]))
                {
                    m.measurement = txtMeasurement.Text;
                    m.unit = ddlUnit.SelectedValue;
                    m.ingredient_id = i.ingredient_id;
                    m.recipe_id = RecipeID;

                    conn.Ingredients.Add(i);
                    conn.Measurements.Add(m);
                }
                conn.SaveChanges();

                //redirect to updated departments page
                Response.Redirect("~/Admin/recipe.aspx?recipe_id=" + RecipeID);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(Request.QueryString["recipe_id"]))
                {
                    lblIngredientMessage.Text = "At Least One Ingredent Must be Added";
                    lblIngredientMessage.Visible = true;
                }
                else
                {
                    using (DefaultConnectionEF conn = new DefaultConnectionEF())
                    {
                        Recipe r = new Recipe();
                        Int32 RecipeID = Convert.ToInt32(Request.QueryString["recipe_id"]);

                        r = (from rec in conn.Recipes
                             where rec.recipe_id == RecipeID
                             select rec).FirstOrDefault();

                        //fill the properties of our object from the form inputs
                        r.recipe_name = txtRecipeName.Text;
                        r.directions = txtRecipeDirections.Text;

                        conn.SaveChanges();

                        lblIngredientMessage.Visible = false;
                        Response.Redirect("/Admin/recipes.aspx");
                    }
                }
            }
            catch (Exception exception)
            {
                lblError.Text = exception.Message;
            }
        }
        protected void grdIngredients_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get the selected ingredient id
            Int32 IngredientID = Convert.ToInt32(grdIngredients.DataKeys[e.RowIndex].Values["ingredient_id"]);
            //get the recipe id
            Int32 RecipeID = Convert.ToInt32(Request.QueryString["recipe_id"]);

            try
            {
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {

                    Measurement objM = (from mes in conn.Measurements
                                       where mes.recipe_id == RecipeID && mes.ingredient_id == IngredientID
                                       select mes).FirstOrDefault();

                    conn.Measurements.Remove(objM);
                    conn.SaveChanges();

                    GetRecipe();

                }
            }
            catch (Exception exc)
            {
                Response.Redirect("~/error.aspx");
            }
        }
        protected void grdRecipes_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //get the selected recipe_id
                    Int32 Recipe_id = Convert.ToInt32(grdRecipes.DataKeys[e.RowIndex].Values["recipe_id"]);

                    var recipe = (from r in conn.Recipes
                                  where r.recipe_id == Recipe_id
                                  select r).FirstOrDefault();

                    var measure = (from mes in conn.Measurements
                                   where mes.recipe_id == Recipe_id
                                   select mes);

                    //delete
                    conn.Measurements.RemoveRange(measure);
                    conn.Recipes.Remove(recipe);
                    conn.SaveChanges();

                    //update the grid
                    GetRecipes();

                }
            }
            catch (Exception ex)
            {
                Response.Redirect("~/error.aspx");
            }
        }