Example #1
0
        //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);
            }
        }
Example #2
0
        //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);
            }
        }