protected void getWorkoutLog()
        {
            //Get userID for viewing based off ID
             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 an error is thrown
            try
            {
                //Connect with EF
                using (HealthLogEntities db = new HealthLogEntities())
                {
                    //Select the coloumns we need for the grid, and default sort
                    String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();
                    var Logs = from c in db.workoutLogs
                               where c.userID == userIdentity
                               select new { c.workoutLogID, c.userID, c.muscleGroup, c.excercise, c.wSets, c.wDate };
                    if (Logs != null)
                    {
                        //Bind the data to the gridview
                        showWorkout.DataSource = Logs.AsQueryable().OrderBy(SortString).ToList();
                        showWorkout.DataBind();
                    }

                }
            }
                //Catch any errors and redirect to the error page
             catch(Exception r)
            {
                Response.Redirect("/error.aspx");
            }
        }
 protected void getSetsLog()
 {
     //set variable for the ID
      Int32 workoutLogID = 0;
      //Check for query string in the URL
      if (Request.QueryString["workoutLogID"] != null)
      {
          //get the id from the url
          workoutLogID = Convert.ToInt32(Request.QueryString["workoutLogID"]);
          //Try block incase any errors are thrown in EF
          try
          {
              //Connect to EF
              using (HealthLogEntities db = new HealthLogEntities())
              {
                  //get the current sets for the workoutLogID from EF
                  var w = from objC in db.setsLogs
                          where objC.setID == workoutLogID
                          select new { objC.setID, objC.ID, objC.setReps, objC.setWeight };
                  //Bind the data to the second gridview if not null
                  if (w != null)
                  {
                      //Bind
                      showSetsLog.DataSource = w.ToList();
                      showSetsLog.DataBind();
                  }
              }
          }
              //Catch any errors thrown and redirect to error page
          catch(Exception s)
          {
              Response.Redirect("/error.aspx");
          }
      }
 }
 protected void getFoodLog()
 {
     //Get the userID for looking up
     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
     {
         //connect with EF to the SQL server
         using (HealthLogEntities db = new HealthLogEntities())
         {
             //Set sortstring
             String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();
             //Select the logs from foodlogs
             var Logs = from c in db.foodLogs
                        where c.userID == userIdentity
                        select new { c.fLogID, c.protein, c.carbs, c.fat, c.calories, c.foodDate, c.userID };
             //If not null, display the logs
             if (Logs != null)
             {
                 showFood.DataSource = Logs.AsQueryable().OrderBy(SortString).ToList();
                 showFood.DataBind();
             }
         }
     }
         //Catch any errors therown and redirect
     catch(Exception r)
     {
         Response.Redirect("/error.aspx");
     }
 }
        protected void getFoodLog()
        {
            //populate form with existing foodlog record
            Int32 fLogID = Convert.ToInt32(Request.QueryString["fLogID"]);

            //Try block incase there are any errors
            try {
            //connect to db via EF
            using (HealthLogEntities db = new HealthLogEntities())
            {
                //populate a student instance with the fLogID from the URL parameter
                foodLog s = (from objS in db.foodLogs
                             where objS.fLogID == fLogID
                             select objS).FirstOrDefault();

                //map the student properties to the form controls if we found a match
                if (s != null)
                {
                    //If editting, set the fields up
                    txtProtein.Text = Convert.ToString(s.protein);
                    txtFat.Text = Convert.ToString(s.fat);
                    txtCarbs.Text = Convert.ToString(s.carbs);
                    txtCalories.Text = Convert.ToString(s.calories);
                    var x = Convert.ToString(s.foodDate);
                    var q =  Convert.ToDateTime(x).ToShortDateString();
                    txtDate.Text = q;
                }
            }
            }
                //Catch any errors thrown and redirect
            catch(Exception y)
            {
                Response.Redirect("/error.aspx");
            }
        }
        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 getWorkoutLog()
        {
            //populate form with existing workout record
            Int32 workoutLogID = Convert.ToInt32(Request.QueryString["workoutLogID"]);
            //Try data connection, incase of errors
            try
            {
                //connect to db via EF
                using (HealthLogEntities db = new HealthLogEntities())
                {
                    //populate a workout instance with the workoutLogID from the URL parameter
                    workoutLog s = (from objS in db.workoutLogs
                                    where objS.workoutLogID == workoutLogID
                                    select objS).FirstOrDefault();
                    //populate a set instance with the workoutLogID from the URL parameter
                    setsLog q = (from objC in db.setsLogs
                                 where objC.setID == s.workoutLogID
                                 select objC).FirstOrDefault();
                    //map the workout properties to the form controls if we found a match
                    if (s != null && q != null)
                    {
                        txtExcercise.Text = s.excercise;
                        ddlMuscleGroup.SelectedValue = s.muscleGroup;
                        txtDate.Text = Convert.ToString(s.wDate);
                        ddlSets.SelectedValue = Convert.ToString(s.wSets);

                    }
                }
            }
            catch(Exception R)
            {
                //If an error is thrown redirect to the error page
                Response.Redirect("/error.aspx");
            }
        }
        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 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 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");
            }
        }