protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // function delete department from gridview
            // determine the row the user clicked
            Int32 gridIndex = e.RowIndex;

            // find department id in selected row
            Int32 DepartmentID = Convert.ToInt32(grdDepartments.DataKeys[gridIndex].Value);

            // connect to database
            var conn = new ContosoEntities();

            // delete the selected department

            /* var objDep = (from d in conn.Departments
             *              where d.DepartmentID == DepartmentID
             *              select d).First(); */

            Department d = new Department();

            d.DepartmentID = DepartmentID;
            conn.Departments.Attach(d);
            conn.Departments.Remove(d);
            conn.SaveChanges();

            // refresh the gridview
            getDepartments();
        }
Exemplo n.º 2
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            // check if we are adding a student or editing one
            Int32 StudentID = 0;

            if (!String.IsNullOrEmpty(Request.QueryString["StudentID"]))
            {
                StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);
            }

            // connect
            var conn = new ContosoEntities();

            // use the student class to create a new student object
            Student s = new Student();

            // fill the properties of the new department object
            s.LastName       = txtLastName.Text;
            s.FirstMidName   = txtFirstName.Text;
            s.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text);

            // save the new object to the database
            if (StudentID == 0)
            {
                conn.Students.Add(s);
            }
            else
            {
                s.StudentID = StudentID;
                conn.Students.Attach(s);
                conn.Entry(s).State = System.Data.Entity.EntityState.Modified;
            }

            conn.SaveChanges();

            // redirect to the departments page
            Response.Redirect("students.aspx");
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            // check if we are adding of editing
            Int32 DepartmentID = 0;

            if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"]))
            {
                DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);
            }

            // connect
            var conn = new ContosoEntities();

            // use the department class to create a new department object
            Department d = new Department();

            // fill the properties of the new department object
            d.Name   = txtName.Text;
            d.Budget = Convert.ToDecimal(txtBudget.Text);

            // save the object to the database
            if (DepartmentID == 0)
            {
                conn.Departments.Add(d);
            }
            else
            {
                d.DepartmentID = DepartmentID;
                conn.Departments.Attach(d);
                conn.Entry(d).State = System.Data.Entity.EntityState.Modified;
            }

            conn.SaveChanges();

            // redirect to the departments page
            Response.Redirect("departments.aspx");
        }
Exemplo n.º 4
0
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // create a function to delete student from gridview
            // determine the row the user clicked
            Int32 gridIndex = e.RowIndex;

            // find student ID in selected row
            Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[gridIndex].Value);

            // connect to database
            var conn = new ContosoEntities();

            // delete the selected student

            Student s = new Student();

            s.StudentID = StudentID;
            conn.Students.Attach(s);
            conn.Students.Remove(s);
            conn.SaveChanges();

            // refresh the gridview
            getStudents();
        }