Пример #1
0
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            //use EF to connect to the server
            using (ContosoConnection db = new ContosoConnection())
            {
                //populate teh form with existing data from the database
                int GameID = Convert.ToInt32(Request.QueryString["GameID"]);

                //populate a student object instance with the StudentID from the URL Parameter
                Game updatedGame = (from game in db.Games
                                    where game.GameID == GameID
                                    select game).FirstOrDefault();

                //map the student properties to the form controls
                if (updatedGame != null)
                {
                    updatedGame.GameName              = GameNameTextBox.Text;
                    updatedGame.ShortDescription      = ShortDescripTextBox.Text;
                    updatedGame.TotalPointsScored     = Convert.ToInt32(TotalScoreTextBox.Text);
                    updatedGame.NumberOfSpectators    = Convert.ToInt32(SpectatorsTextBox.Text);
                    updatedGame.DesignatedWinningTeam = DesignatedWinningTeamTextBox.Text;
                }

                //run an insert command
                db.SaveChanges();
                //redirect back to the students page
                Response.Redirect("~/Details/GameTracker.aspx");
            }
        }
Пример #2
0
        /**
         * <summary>
         * This event handler deletes a student from the databse using EF
         * </summary>
         * @method StudentsGridView_RowDeleting
         * @param {object} sender
         * @param {GridViewDeleteEventArgs}
         * @returns {void}
         * */
        protected void StudentsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //store which row was clicked
            int selectedRow = e.RowIndex;

            //get the selected StudentID using the grids datakey collection
            int StudentID = Convert.ToInt32(StudentsGridView.DataKeys[selectedRow].Values["StudentID"]);

            //use ef to find the selelcted student and delete it
            using (ContosoConnection db = new ContosoConnection())
            {
                //create object of the student class and store the query string inside of it
                Student deletedStudent = (from studentRecords in db.Students
                                          where studentRecords.StudentID == StudentID
                                          select studentRecords).FirstOrDefault();

                //remove the selected student from the db
                db.Students.Remove(deletedStudent);

                //save db changes
                db.SaveChanges();

                //refresh gridview
                this.GetStudents();
            }
        }
Пример #3
0
        //update team's informations
        //update game's total point scored and designated winning team
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            //use EF to connect to the server
            using (ContosoConnection db = new ContosoConnection())
            {
                //populate teh form with existing data from the database
                int TeamID = Convert.ToInt32(Request.QueryString["TeamID"]);

                //populate a student object instance with the StudentID from the URL Parameter
                Team updatedTeam = (from team in db.Teams
                                    where team.TeamID == TeamID
                                    select team).FirstOrDefault();
                //map the student properties to the form controls
                if (updatedTeam != null)
                {
                    int  GameID           = updatedTeam.GameID;
                    int  teamScore        = Convert.ToInt32(TotalScoreTextBox.Text);
                    Team anotherTeamScore = (from team in db.Teams
                                             where team.GameID == GameID && team.TeamID != TeamID
                                             select team).FirstOrDefault();

                    Game updatedGame = (from game in db.Games
                                        where game.GameID == GameID
                                        select game).FirstOrDefault();
                    if (updatedTeam.TotalPointsScored > anotherTeamScore.TotalPointsScored)
                    {
                        updatedGame.DesignatedWinningTeam = TeamNameTextBox.Text;
                    }
                    else if (updatedTeam.TotalPointsScored < anotherTeamScore.TotalPointsScored)
                    {
                        updatedGame.DesignatedWinningTeam = anotherTeamScore.TeamName;
                    }
                    else
                    {
                        updatedGame.DesignatedWinningTeam = "Draw";
                    }

                    updatedGame.TotalPointsScored = (teamScore + anotherTeamScore.TotalPointsScored);

                    updatedTeam.TeamName          = TeamNameTextBox.Text;
                    updatedTeam.ShortDescription  = ShortDescripTextBox.Text;
                    updatedTeam.TotalPointsScored = Convert.ToInt32(TotalScoreTextBox.Text);
                }

                //run an insert command
                db.SaveChanges();
                //redirect back to the students page
                Response.Redirect("~/Details/GameTracker.aspx");
            }
        }
Пример #4
0
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            // Use EF to connect to the server
            using (ContosoConnection db = new ContosoConnection())
            {
                // use the Student model to create a new student object and
                // save a new record
                Student newStudent = new Student();

                int StudentID = 0;

                if (Request.QueryString.Count > 0) // our URL has a StudentID in it
                {
                    // get the id from the URL
                    StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                    // get the current student from EF DB
                    newStudent = (from student in db.Students
                                  where student.StudentID == StudentID
                                  select student).FirstOrDefault();
                }

                // add form data to the new student record
                newStudent.LastName       = LastNameTextBox.Text;
                newStudent.FirstMidName   = FirstNameTextBox.Text;
                newStudent.EnrollmentDate = Convert.ToDateTime(EnrollmentDateTextBox.Text);

                // use LINQ to ADO.NET to add / insert new student into the database

                if (StudentID == 0)
                {
                    db.Students.Add(newStudent);
                }


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

                // Redirect back to the updated students page
                Response.Redirect("~/Contoso/Students.aspx");
            }
        }
Пример #5
0
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            // Use EF to connect to the server
            using (ContosoConnection db = new ContosoConnection())
            {
                // use the Departments model to create a new department object and
                // save a new record
                Department newDepartment = new Department();

                int DepartmentID = 0;

                if (Request.QueryString.Count > 0) // our URL has a DepartmentID in it
                {
                    // get the id from the URL
                    DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

                    // get the current student from EF DB
                    newDepartment = (from department in db.Departments
                                     where department.DepartmentID == DepartmentID
                                     select department).FirstOrDefault();
                }

                // add form data to the new department record
                newDepartment.Name   = DepartmentNameTextBox.Text;
                newDepartment.Budget = Convert.ToDecimal(BudgetTextBox.Text);

                // use LINQ to ADO.NET to add / insert new department into the database

                if (DepartmentID == 0)
                {
                    db.Departments.Add(newDepartment);
                }


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

                // Redirect back to the updated departments page
                Response.Redirect("~/Contoso/Departments.aspx");
            }
        }
Пример #6
0
        protected void StudentsGridView_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 StudentID = Convert.ToInt32(StudentsGridView.DataKeys[selectedRow].Values["StudentID"]);

            // use EF to find the selected student from DB and remove it
            using (ContosoConnection db = new ContosoConnection())
            {
                Student deletedStudent = (from studentRecords in db.Students
                                          where studentRecords.StudentID == StudentID
                                          select studentRecords).FirstOrDefault();

                // perform the removal in the DB
                db.Students.Remove(deletedStudent);
                db.SaveChanges();

                // refresh the grid
                this.GetStudents();
            }
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            //Use Entity Framework to connect to the server
            using (ContosoConnection db = new ContosoConnection())
            {
                //Use the Student model to create a new student object and
                //Save a new record
                Student newStudent = new Student();

                int StudentID = 0;

                if (Request.QueryString.Count > 0) //Our URL has a StudentID in it
                {
                    //Get the StudentID from the URL
                    StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                    //Get the current student from the Entity Framework Database
                    newStudent = (from student in db.Students
                                  where student.StudentID == StudentID
                                  select student).FirstOrDefault();
                }

                //Add data to the new student record
                newStudent.LastName = LastNameTextBox.Text;
                newStudent.FirstMidName = FirstNameTextBox.Text;
                newStudent.EnrollmentDate = Convert.ToDateTime(EnrollmentDateTextBox.Text);

                //Use LINQ/ADO.NET to Add/Insert new student into the database
                if (StudentID == 0)
                {
                    db.Students.Add(newStudent);
                }

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

                //Redirect back to the updated students page
                Response.Redirect("~/Contoso/Students.aspx");
            }
        }
        /**
         * <summary>
         * This event handler deletes a student from the database using entity framework
         * </summary>
         *
         * @method StudentGridView_RowDeleting
         * @param {object} sender
         * @param {GridviewDeleteEventArgs} e
         * @returns {void}
         *
         */
        protected void StudentsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //Store which row was selected
            int selectedRow = e.RowIndex;

            //Get the selected StudentID using the grids DataKey collection
            int StudentID = Convert.ToInt32(StudentsGridView.DataKeys[selectedRow].Values["StudentID"]);

            //Use Entity Framework to find the selected student in the DB and remove it
            using (ContosoConnection db = new ContosoConnection())
            {
                //Create object of the student class and store the query string inside of it
                Student deletedStudent = (from studentRecords in db.Students
                                          where studentRecords.StudentID == StudentID
                                          select studentRecords).FirstOrDefault();

                //Remove the selected student from the database
                db.Students.Remove(deletedStudent);

                //Save changes to database
                db.SaveChanges();

                //Refresh the gridview
                this.GetStudents();
            }
        }