コード例 #1
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");
        }
コード例 #2
0
        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");
        }