protected void btnSave_Click(object sender, EventArgs e)
        {
            //Get USERID for the saving foreign keys
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            var userIdentity = authenticationManager.User.Identity.GetUserId();
            //Try block incase any errors are thrown
            try {
                //use EF to connect to SQL Server
                using (HealthLogEntities db = new HealthLogEntities())
                {

                    //use the foodLog model to save the new record
                    foodLog s = new foodLog();

                    Int32 fLogID = 0;

                    //check the querystring for an id so we can determine add / update
                    if (Request.QueryString["fLogID"] != null)
                    {
                        //get the id from the url
                        fLogID = Convert.ToInt32(Request.QueryString["fLogID"]);

                        //get the current student from EF
                        s = (from objS in db.foodLogs
                             where objS.fLogID == fLogID
                             select objS).FirstOrDefault();
                    }

                    //s.XXX = XXX.text for all variables.
                    s.userID = userIdentity;
                    s.fat = Convert.ToInt32(txtFat.Text);
                    s.protein = Convert.ToInt32(txtProtein.Text);
                    s.carbs = Convert.ToInt32(txtCarbs.Text);
                    s.calories = Convert.ToInt32(txtCalories.Text);
                    s.foodDate = Convert.ToDateTime(txtDate.Text);

                    //call add only if we have no student ID
                    if (fLogID == 0)
                    {
                        db.foodLogs.Add(s);
                    }

                    //run the update or insert
                    db.SaveChanges();

                    //redirect to the updated foodlog page
                    Response.Redirect("viewFoodLog.aspx");
                }
            }

            catch (System.Threading.ThreadAbortException lException)
            {

                // do nothing

            }
                //Catch any error and redirect
                catch(Exception z){
                    Response.Redirect("/error.aspx");
                }
        }
 protected void showWorkout_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
     //get workoutLogID
     Int32 workoutLogID = Convert.ToInt32(showWorkout.DataKeys[e.RowIndex].Values["workoutLogID"]);
     //Try block incase EF throws any errors
     try {
         //Use EF to connect to DB
         using (HealthLogEntities db = new HealthLogEntities())
         {
             //get selected workout
             workoutLog objC = (from c in db.workoutLogs
                                where c.workoutLogID == workoutLogID
                                select c).FirstOrDefault();
             //For loop for each set, up to potentially 5
             for (int i = 0; i < 5; i++)
             {
                 //Get each set with the workoutLogID we are deleting
                 setsLog objD = (from d in db.setsLogs where d.setID == objC.workoutLogID select d).FirstOrDefault();
                 if (objD != null)
                 {
                     //Remove each set from that workout
                     db.setsLogs.Remove(objD);
                     db.SaveChanges();
                 }
             }
             //Delete the workout itself
             db.workoutLogs.Remove(objC);
             db.SaveChanges();
         }
     }
         //Catch any errors and redirect
         catch(Exception q){
             Response.Redirect("/error.aspx");
         }
         //refresh grid
         getWorkoutLog();
 }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //Get the users ID for storing in the DB
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            var userIdentity = authenticationManager.User.Identity.GetUserId();
            //Try statement incase an error is thrown

            try
            {
                //use EF to connect to SQL Server
                using (HealthLogEntities db = new HealthLogEntities())
                {

                    //use the Student model to save the new record
                    workoutLog s = new workoutLog();
                    Int32 workoutLogID = 0;
                    //check the querystring for an id so we can determine add / update
                    if (Request.QueryString["workoutLogID"] != null)
                    {
                        //get the id from the url
                        workoutLogID = Convert.ToInt32(Request.QueryString["workoutLogID"]);
                        //get the current workout from EF
                        s = (from objS in db.workoutLogs
                             where objS.workoutLogID == workoutLogID
                             select objS).FirstOrDefault();
                    }
                    //s.XXX = XXX.text for all variables. setting them in the DB
                    s.excercise = txtExcercise.Text;
                    s.muscleGroup = ddlMuscleGroup.SelectedValue;
                    s.userID = userIdentity;
                    s.wDate = Convert.ToDateTime(txtDate.Text);
                    s.wSets = Convert.ToInt32(ddlSets.SelectedValue);

                    //call add only if we have no workout ID
                    if (workoutLogID == 0)
                    {
                        db.workoutLogs.Add(s);
                    }

                    //run the update or insert
                    db.SaveChanges();
                    //New set variable
                    setsLog q = new setsLog();
                    Int32 setID = 0;
                    //check to see if editting with the query string
                    if (Request.QueryString["setID"] != null)
                    {
                        //get the id from the url
                        setID = Convert.ToInt32(Request.QueryString["setID"]);

                        //get the current student from EF
                        q = (from objC in db.setsLogs
                             where objC.setID == s.workoutLogID
                             select objC).FirstOrDefault();
                    }
                    //If there is no set in the url
                    if (setID == 0)
                    {
                        //switch statement to add each set of weight/reps to the database depending on how many they chose
                        switch (Convert.ToInt32(ddlSets.SelectedValue))
                        {
                            case (1):
                                q.setReps = setsControl1.getReps();
                                q.setWeight = setsControl1.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                break;
                            case (2):
                                q.setReps = setsControl1.getReps();
                                q.setWeight = setsControl1.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                q.setReps = setsControl2.getReps();
                                q.setWeight = setsControl2.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                break;
                            case (3):
                                q.setReps = setsControl1.getReps();
                                q.setWeight = setsControl1.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                q.setReps = setsControl2.getReps();
                                q.setWeight = setsControl2.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                q.setReps = setsControl3.getReps();
                                q.setWeight = setsControl3.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                break;
                            case (4):
                                q.setReps = setsControl1.getReps();
                                q.setWeight = setsControl1.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                q.setReps = setsControl2.getReps();
                                q.setWeight = setsControl2.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                q.setReps = setsControl3.getReps();
                                q.setWeight = setsControl3.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                q.setReps = setsControl4.getReps();
                                q.setWeight = setsControl4.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                break;
                            case (5):
                                q.setReps = setsControl1.getReps();
                                q.setWeight = setsControl1.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                q.setReps = setsControl2.getReps();
                                q.setWeight = setsControl2.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                q.setReps = setsControl3.getReps();
                                q.setWeight = setsControl3.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                q.setReps = setsControl4.getReps();
                                q.setWeight = setsControl4.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                q.setReps = setsControl5.getReps();
                                q.setWeight = setsControl5.getWeight();
                                q.setID = s.workoutLogID;
                                //Add the rep(s) and weight(s) to the DB
                                db.setsLogs.Add(q);
                                db.SaveChanges();
                                break;
                        }
                    }
                    //Send the user to the view workout page
                    Response.Redirect("viewWorkout.aspx");
                }
            }
            catch (System.Threading.ThreadAbortException lException)
            {

                // do nothing

            }
                //catch if an error is thrown and redirect to the error page
            catch(Exception T)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void showFood_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get food log id
            Int32 fLogID = Convert.ToInt32(showFood.DataKeys[e.RowIndex].Values["fLogID"]);
            //Try block to catch any errors
            try
            {
                //Connect to the DB with EF
                using (HealthLogEntities db = new HealthLogEntities())
                {
                    //get selected log
                    foodLog objC = (from c in db.foodLogs
                                    where c.fLogID == fLogID
                                    select c).FirstOrDefault();

                    //delete
                    db.foodLogs.Remove(objC);
                    db.SaveChanges();

                    //refresh grid
                    getFoodLog();
                }
            }
                //Catch any errors thrown and redirect to error page
            catch (Exception s)
            {
                Response.Redirect("/error.aspx");
            }
        }