Exemple #1
0
        /**
         * <summary>
         * This event handler deletes from the db using EF
         * </summary>
         *
         * @method GridView_RowDeleting
         * @param {object} sender
         * @param {GridViewDeleteEventArgs} e
         * @returns {void}
         */
        protected void BasketballGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // store which row was clicked
            int selectedRow = e.RowIndex;

            // get the selected StudentID using the Grid's DataKey collection
            int basketballID = Convert.ToInt32(BasketballGridView.DataKeys[selectedRow].Values["basketballID"]);

            // use EF to find the selected student in the DB and remove it
            using (DefaultConnection db = new DefaultConnection())
            {
                // create object of the Student class and store the query string inside of it
                Basketball deletedBasketball = (from basketballRecords in db.Basketball
                                                where basketballRecords.basketballID == basketballID
                                                select basketballRecords).FirstOrDefault();

                // remove the selected rom the db
                db.Students.Remove(deletedBasketball);

                // save my changes back to the database
                db.SaveChanges();

                // refresh the grid
                this.GetBasketball();
            }
        }
        protected void GetBasketball()
        {
            // populate teh form with existing data from the database
            int basketballID = Convert.ToInt32(Request.QueryString["basketballID"]);

            // connect to the EF DB
            using (DefaultConnection db = new DefaultConnection())
            {
                Basketball updatedBasketball = (from basketball in db.Basketball
                                                where basketball.basketballtID == basketballID
                                                select basketball).FirstOrDefault();

                if (updatedBasketball != null)
                {
                    teamName1TextBox.Text = updatedBasketball.teamName1;
                    teamName2TextBox.Text = updatedBasketball.teamName2;
                }
            }
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            // Use EF to connect to the server
            using (DefaultConnection db = new DefaultConnection())
            {
                // save a new record
                Basketball newBasketball = new Basketball();

                int basketballID = 0;

                if (Request.QueryString.Count > 0)
                {
                    // get the id from the URL
                    basketballID = Convert.ToInt32(Request.QueryString["basketballtID"]);

                    newBasketball = (from basktball in db.Basketball
                                     where basketball.basketballID == basketballID
                                     select Basketball).FirstOrDefault();
                }

                newBasketball.teamName1 = teamName1TextBox.Text;
                newBasketball.teamName2 = teamName2TextBox.Text;


                if (basketballID == 0)
                {
                    db.Basketball.Add(newBasketball);
                }


                // save our changes - also updates and inserts
                db.SaveChanges();

                // Redirect back to the updated  page
                Response.Redirect("~/BasketballF.aspx");
            }
        }