protected void Insert(object sender, EventArgs e)
    {
        using (DIYPTEntities db = new DIYPTEntities())
        {
            db.Database.Connection.Open();
            {
                int ExerciseUnitSetId = 0;
                //int ExerciseUnitId = Convert.ToInt32(Session["UnitSetId"]);
                int ExerciseUnitId = Convert.ToInt32(txtExerciseUnitId.Text);

                var tt = (from c in db.PrizeExerciseUnits
                          where c.Id == ExerciseUnitId
                          select c).Count();

                if (Convert.ToInt32(tt) == 0)
                {
                    string errorText = "Unit Id doesnot exist!Please add new unit first.";
                    Response.Write(
                        @"<SCRIPT LANGUAGE=""JavaScript"">alert('" + errorText + "')</SCRIPT>");
                }
                else
                {
                    ExerciseUnitSetId = Convert.ToInt32(Session["UnitSetId"]);
                    if (ExerciseUnitSetId == 0)
                    {
                        int units = (from c in db.PrizeExerciseUnitSets
                                     orderby c.UnitSetId
                                     select c.UnitSetId).DefaultIfEmpty(0).Max();

                        ExerciseUnitSetId = units + 1;

                        //add to unitset for days table one recond.
                        int pwid  = Convert.ToInt32(Session["PWID"]);
                        int day   = Convert.ToInt32(Session["DAY"]);
                        int DGNid = Convert.ToInt32(Session["DayGroupNameId"]);

                        PrizeExerciseUnitSetForDay item = (from c in db.PrizeExerciseUnitSetForDays
                                                           where c.ExercisePlanWeekId == pwid && c.ExerciseDay == day && c.DayGroupNameId == DGNid
                                                           select c).FirstOrDefault();

                        item.ExerciseUnitSetId = ExerciseUnitSetId;
                        db.SaveChanges();
                        Session["UnitSetId"] = ExerciseUnitSetId;
                    }


                    PrizeExerciseUnitSet addplan = new PrizeExerciseUnitSet();
                    addplan.Sequence       = txtSequence.Text;
                    addplan.ExerciseUnitId = Convert.ToInt32(txtExerciseUnitId.Text);
                    addplan.UnitSetId      = ExerciseUnitSetId;
                    db.PrizeExerciseUnitSets.Add(addplan);
                    db.SaveChanges();
                }
                this.BindGrid();
            }

            //   transaction.Complete();
            db.Database.Connection.Close();
        }
    }
    protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);

        using (DIYPTEntities db = new DIYPTEntities())
        {
            db.Database.Connection.Open();
            PrizeExerciseUnitSet item = (from c in db.PrizeExerciseUnitSets
                                         where c.Id == Id
                                         select c).FirstOrDefault();
            db.PrizeExerciseUnitSets.Remove(item);

            db.SaveChanges();
            db.Database.Connection.Close();
        }
        GridView1.EditIndex = -1;
        this.BindGrid();
    }
    protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row            = GridView1.Rows[e.RowIndex];
        int         Id             = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
        string      Sequence       = (row.FindControl("ttSequence") as TextBox).Text;
        int         ExerciseUnitId = Convert.ToInt32((row.FindControl("ttExerciseUnitId") as TextBox).Text);


        using (DIYPTEntities db = new DIYPTEntities())
        {
            db.Database.Connection.Open();

            var tt = (from c in db.PrizeExerciseUnits
                      where c.Id == ExerciseUnitId
                      select c).Count();

            if (Convert.ToInt32(tt) == 0)
            {
                string errorText = "Unit Id doesnot exist!Please add new unit first.";
                Response.Write(
                    @"<SCRIPT LANGUAGE=""JavaScript"">alert('" + errorText + "')</SCRIPT>");
            }
            else
            {
                PrizeExerciseUnitSet item = (from c in db.PrizeExerciseUnitSets
                                             where c.Id == Id
                                             select c).FirstOrDefault();

                item.Sequence       = Sequence;
                item.ExerciseUnitId = ExerciseUnitId;
                db.SaveChanges();
            }



            db.Database.Connection.Close();
        }
        GridView1.EditIndex = -1;
        this.BindGrid();
    }