コード例 #1
0
        /**
         *<summary>
         * This method gets the student data from the DB
         * </summary>summary>
         *
         * @Method GetStudents
         **/
        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 the result to the GridView
                StudentsGridView.DataSource = Students.AsQueryable().OrderBy(SortString).ToList();
                StudentsGridView.DataBind();
            }
        }
コード例 #2
0
        protected void GetStudent()
        {
            //populate the form with existing data from the database
            int StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

            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");
                }
            }
        }
コード例 #3
0
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            //connect to EF
            using (ContosoConnection db = new ContosoConnection())
            {
                //use student model to create a new student object and save a new record
                Student newStudent = new Student();

                int StudentID = 0;

                //url contains a student id
                if (Request.QueryString.Count > 0)
                {
                    //get id from 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 data to the newStudent record
                newStudent.LastName = LastNameTextBox.Text.ToString();
                newStudent.FirstMidName = FirstNameTextBox.Text.ToString();
                newStudent.EnrollmentDate = Convert.ToDateTime(EnrollmentDateTextBox.Text);

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

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

                //redirect back to updated students page
                Response.Redirect("~/Contoso/Students.aspx");
            }
        }
コード例 #4
0
        /// <summary>
        /// This event handler deletes a student from the db using EF
        /// </summary>
        /// 
        /// <param name="sender">{object} sender</param>
        /// <param name="e">{GridViewDeleteEventArgs} e</param>
        /// 
        /// @method StudentsGridView_RowDeleting
        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"]);

            try
            {
                //use EF to find the selected student in the DB and remove it
                using (ContosoConnection db = new ContosoConnection())
                {
                    //create object of 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 changes to db
                    db.SaveChanges();

                    //refresh the grid
                    this.GetStudents();
                }
            }
            catch (Exception err)
            {
                //error message

            }
        }