Esempio n. 1
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (DefaultConnection db = new DefaultConnection())
            {
                Student stud = new Student();

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

                    stud = (from d in db.Students where d.StudentID == StudentID select d).FirstOrDefault();
                }

                stud.LastName = txtLName.Text;
                stud.FirstMidName = txtFName.Text;
                stud.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text);

                if (String.IsNullOrEmpty(Request.QueryString["StudentID"]))
                {
                    db.Students.Add(stud);
                }
                db.Students.Add(stud);
                db.SaveChanges();

                Response.Redirect("students.aspx");
            }
        }
Esempio n. 2
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                // create a new student in memory
                Student stud = new Student();

                //check for a url
                if (!String.IsNullOrEmpty(Request.QueryString["StudentID"]))
                {
                    //get id from url
                    Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                    //look up the student
                    stud = (from s in db.Students
                            where s.StudentID == StudentID
                            select s).FirstOrDefault();
                }

                // fill properties of new student
                stud.LastName = txtLastName.Text;
                stud.FirstMidName = txtFirstName.Text;
                stud.EnrollmentDate = Convert.ToDateTime(txtDateEnrolled.Text);

                //add if we have no id in the url
                if (String.IsNullOrEmpty(Request.QueryString["StudentID"]))
                {
                    stud.EnrollmentDate = System.DateTime.Now;
                    db.Students.Add(stud);
                }
                db.SaveChanges();

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