//get the current workout selected protected void GetWorkout() { try { //populate form with existing workout record Int32 WorkoutID = Convert.ToInt32(Request.QueryString["WorkoutID"]); //connect to db via EF using (DefaultConnectionEF db = new DefaultConnectionEF()) { //populate a workout instance with the WorkoutID from the URL parameter Workout w = (from a in db.Workouts where a.WorkoutID == WorkoutID select a).FirstOrDefault(); //map the workout properties to the form controls if we found a match if (w != null) { txtName.Text = w.Name; txtBodyPart.Text = w.BodyPart; txtIntensity.Text = w.Intensity; txtReps.Text = w.Reps.ToString(); txtSetts.Text = w.Setts.ToString(); txtWorkoutDate.Text = w.WorkoutDate.ToString("yyyy-MM-dd"); } } } catch(System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
//Save the workout edit/ add protected void btnSave_Click(object sender, EventArgs e) { try { //use EF to connect to SQL Server using (DefaultConnectionEF db = new DefaultConnectionEF()) { //use the Workout model to save the new record Workout w = new Workout(); Int32 WorkoutID = 0; //check the querystring for an id so we can determine add / update if (Request.QueryString["WorkoutID"] != null) { //get the id from the url WorkoutID = Convert.ToInt32(Request.QueryString["WorkoutID"]); //get the current workout from EF w = (from a in db.Workouts where a.WorkoutID == WorkoutID select a).FirstOrDefault(); } //Convert and add text to fields w.Name = txtName.Text; w.BodyPart = txtBodyPart.Text; w.Reps = Convert.ToInt32(txtReps.Text); w.Setts = Convert.ToInt32(txtSetts.Text); w.Intensity = txtIntensity.Text; w.WorkoutDate = Convert.ToDateTime(txtWorkoutDate.Text); //call add only if we have no workout ID if (WorkoutID == 0) { db.Workouts.Add(w); } //run the update or insert db.SaveChanges(); //redirect to the updated students page Response.Redirect("/admin/workouts.aspx"); } } catch { Server.Transfer("/error.aspx", true); } }
//Connect to the database and get the workouts protected void GetWorkouts() { try { Int32 ExerciseID = Convert.ToInt32(Request.QueryString["CourseID"]); //connect to EF using (DefaultConnectionEF db = new DefaultConnectionEF()) { //query the workouts table using EF and LINQ var workouts = (from w in db.Workouts select new { w.WorkoutID, w.Name, w.BodyPart, w.Intensity, w.Reps, w.Setts, w.WorkoutDate }); //var Workouts = from w in db.Workouts // select w; String Sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //var objE = (from w in db.Links // join d in db.Workouts on w.WorkoutID equals d.WorkoutID //join a in db.Exercises on w.ExerciseID equals a.ExerciseID //where w.ExerciseID == ExerciseID //select new { d.WorkoutID, d.Intensity, a.Reps, a.Setts, d.WorkoutDate }); //bind the result to the gridview //grdWorkouts.DataSource = Workouts.ToList(); grdWorkouts.DataSource = workouts.AsQueryable().OrderBy(Sort).ToList(); grdWorkouts.DataBind(); //grdTestWorkouts.DataSource = objE.ToList(); //grdTestWorkouts.DataBind(); } }catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
//Delete a row that the user selects protected void grdWorkouts_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { //store which row was clicked Int32 selectedRow = e.RowIndex; //get the selected WorkoutID using the grid's Data Key collection Int32 WorkoutID = Convert.ToInt32(grdWorkouts.DataKeys[selectedRow].Values["WorkoutID"]); //use EF to remove the selected workouts from the db using (DefaultConnectionEF db = new DefaultConnectionEF()) { Workout a = (from aw in db.Workouts where aw.WorkoutID == WorkoutID select aw).FirstOrDefault(); //do the delete db.Workouts.Remove(a); db.SaveChanges(); } //refresh the grid GetWorkouts(); } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }