Ejemplo n.º 1
0
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //identify student id to manipulate
            Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]);

            //Select current id based upon selected row
            using (DefaultConnection db = new DefaultConnection()){
                Student stdt = (from s in db.Students
                            where s.StudentID == StudentID
                            select s).FirstOrDefault();
                db.Students.Remove(stdt);
                db.SaveChanges();

                //refresh grid
                getStudents();
            }
        }
Ejemplo n.º 2
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //connect to database
            using (DefaultConnection db = new DefaultConnection())
            {
                //create new student in memory
                Student stdt = new Student();
                Int32 StudentID = 0;

                 DateTime EnDate = DateTime.Now;

                //handle updating a record
                if (!String.IsNullOrEmpty(Request.QueryString["StudentID"]))
                {
                    StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                    stdt = (from s in db.Students
                            where s.StudentID == StudentID
                            select s).FirstOrDefault();

                    //set eroll date to equal existing result
                    EnDate = stdt.EnrollmentDate;

                }

                //fill in student details
                stdt.FirstMidName = txtLName.Text;
                stdt.LastName = txtFName.Text;
                //add current date time as time of registering
                stdt.EnrollmentDate = EnDate;

                if (StudentID == 0)
                {
                    //create new row in database
                    db.Students.Add(stdt);
                }

                db.SaveChanges();

                //redirect back to students page
                Response.Redirect("students.aspx");

            }
        }
Ejemplo n.º 3
0
        protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //identify department id to be deleted from the row the user selected
            Int32 DepartmentID = Convert.ToInt32(grdDepartments.DataKeys[e.RowIndex].Values["DepartmentID"]);

            //connect to database
            using (DefaultConnection db = new DefaultConnection())
            {
                //SElect current department based upon id
                Department dep = (from d in db.Departments
                                  where d.DepartmentID == DepartmentID
                                  select d).FirstOrDefault();
                db.Departments.Remove(dep);
                db.SaveChanges();

                //refresh the grid
                GetDepartments();

            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                //create a new department in memory
                Department dep = new Department();

                Int32 DepartmentID = 0;

                //handle updating a recod
                if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"]))
                {
                    DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

                    dep = (from d in db.Departments
                               where d.DepartmentID == DepartmentID
                               select d).FirstOrDefault();
                }

                //fill the properties of the department
                dep.Name = txtName.Text;
                dep.Budget = Convert.ToDecimal(txtbudget.Text);

                if (DepartmentID == 0)
                {
                    //save the new department
                    db.Departments.Add(dep);
                }

                db.SaveChanges();

                //redirect to departments list page
                Response.Redirect("departments.aspx");
            }
        }