/** * <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(); } }
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"); } }
protected void GetGames() { using (ContosoConnection db = new ContosoConnection()) { var Games = (from allGames in db.Games select allGames); GameTrackersGridView.DataSource = Games.ToList(); GameTrackersGridView.DataBind(); } }
/** * <summary> * This method gets the student data from the database * </summary> * @method GetStudents * @return {void} * */ protected void GetStudents() { //connect to EF using (ContosoConnection db = new ContosoConnection()) { string SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //query the students table using EF and LINQ var Students = (from allStudents in db.Students select allStudents); //bind results to gridview StudentsGridView.DataSource = Students.AsQueryable().OrderBy(SortString).ToList(); StudentsGridView.DataBind(); } }
//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"); } }
protected void GetTeam() { //populate teh form with existing data from the database int GameID = Convert.ToInt32(Request.QueryString["GameID"]); using (ContosoConnection db = new ContosoConnection()) { var Teams = (from team in db.Teams where team.GameID == GameID select team); TeamsGridView.DataSource = Teams.ToList(); TeamsGridView.DataBind(); } }
protected void GetDepartments() { string sortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); // connect to EF using (ContosoConnection db = new ContosoConnection()) { // query the departments Table using EF and LINQ var Departments = (from allDepartments in db.Departments select allDepartments); // bind the result to the GridView DepartmentsGridView.DataSource = Departments.AsQueryable().OrderBy(sortString).ToList(); DepartmentsGridView.DataBind(); } }
/** * <summary> * This method gets the student data from the database * </summary> * * @method GetStudents * @returns {void} */ protected void GetStudents() { //Connect to Entity Framework using (ContosoConnection db = new ContosoConnection()) { string SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //Query the Students Table using Entity Framework and LINQ var Students = (from allStudents in db.Students select allStudents); //Bind the results to the GridView StudentsGridView.DataSource = Students.AsQueryable().OrderBy(SortString).ToList(); StudentsGridView.DataBind(); } }
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"); } }
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"); } }
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"); } }
protected void GetDepartment() { // populate the form with existing department data from the db int DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); // connect to the EF DB using (ContosoConnection db = new ContosoConnection()) { // populate a department instance with the DepartmentID from the URL parameter Department updatedDepartment = (from department in db.Departments where department.DepartmentID == DepartmentID select department).FirstOrDefault(); // map the department properties to the form controls if (updatedDepartment != null) { NameTextBox.Text = updatedDepartment.Name; BudgetTextBox.Text = updatedDepartment.Budget.ToString(); } } }
protected void GetStudent() { // populate teh form with existing data from the database int StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); // connect to the EF DB using (ContosoConnection db = new ContosoConnection()) { // populate a student object instance with the StudentID from the URL Parameter Student updatedStudent = (from student in db.Students where student.StudentID == StudentID select student).FirstOrDefault(); // map the student properties to the form controls if (updatedStudent != null) { LastNameTextBox.Text = updatedStudent.LastName; FirstNameTextBox.Text = updatedStudent.FirstMidName; EnrollmentDateTextBox.Text = updatedStudent.EnrollmentDate.ToString("yyyy-MM-dd"); } } }
protected void GetTeam() { //populate teh form with existing data from the database int TeamID = Convert.ToInt32(Request.QueryString["TeamID"]); //connect to the EF DB using (ContosoConnection db = new ContosoConnection()) { //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) { TeamNameTextBox.Text = updatedTeam.TeamName; ShortDescripTextBox.Text = updatedTeam.ShortDescription; TotalScoreTextBox.Text = updatedTeam.TotalPointsScored.ToString(); } } }
protected void GetStudent() { //Populate the form with existing data from the database int StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //Connect to the Entity Framework Database using (ContosoConnection db = new ContosoConnection()) { //Populate a student object instance with the StudentID from the URL Param Student updatedStudent = (from student in db.Students where student.StudentID == StudentID select student).FirstOrDefault(); //Map the student properties to the from controls if (updatedStudent != null) { LastNameTextBox.Text = updatedStudent.LastName; FirstNameTextBox.Text = updatedStudent.FirstMidName; EnrollmentDateTextBox.Text = updatedStudent.EnrollmentDate.ToString("yyyy-MM-dd"); } } }
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 GetGame() { //populate teh form with existing data from the database int GameID = Convert.ToInt32(Request.QueryString["GameID"]); //connect to the EF DB using (ContosoConnection db = new ContosoConnection()) { //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) { GameNameTextBox.Text = updatedGame.GameName; ShortDescripTextBox.Text = updatedGame.ShortDescription; TotalScoreTextBox.Text = updatedGame.TotalPointsScored.ToString(); SpectatorsTextBox.Text = updatedGame.NumberOfSpectators.ToString(); DesignatedWinningTeamTextBox.Text = updatedGame.DesignatedWinningTeam; } } }
/** * <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(); } }