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 Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // check the url for an ID
                if (!String.IsNullOrEmpty(Request.QueryString["StudentID"]))
                {
                    // get the ID from the url
                    Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

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

                    // find the selected student
                    var objStudent = (from s in conn.Students
                                      where s.StudentID == StudentID
                                      select s).FirstOrDefault();

                    // populate the form
                    txtLastName.Text       = objStudent.LastName;
                    txtFirstName.Text      = objStudent.FirstMidName;
                    txtEnrollmentDate.Text = objStudent.EnrollmentDate.ToString();
                }
            }
        }
        protected void getDepartments()
        {
            // connect to database
            var conn = new ContosoEntities();

            // run the query using LINQ
            var Departments = from d in conn.Departments
                              select d;

            // display query result in grideview
            grdDepartments.DataSource = Departments.ToList();
            grdDepartments.DataBind();
        }
Exemplo n.º 4
0
        protected void getStudents()
        {
            // connect to database
            var conn = new ContosoEntities();

            // run the query using LINQ
            var Students = from s in conn.Students
                           select s;

            // display query result in grideview
            grdStudents.DataSource = Students.ToList();
            grdStudents.DataBind();
        }
Exemplo n.º 5
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");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // check the url for an ID
                if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"]))
                {
                    // get Id from the url
                    Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

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

                    // find selected department
                    var objDep = (from d in conn.Departments
                                  where d.DepartmentID == DepartmentID
                                  select d).FirstOrDefault();

                    // populate the form
                    txtName.Text   = objDep.Name;
                    txtBudget.Text = objDep.Budget.ToString();
                }
            }
        }
Exemplo n.º 8
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();
        }