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 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"); } }