protected void btnAdd_Click1(object sender, EventArgs e)
        {
            Int32 StudentID = 0;

            if (!String.IsNullOrEmpty(Request.QueryString["StudentID"]))
            {
                StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);
            }
            //db connection
            var conn = new ContosoEntities();

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

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

            //save new data to DB
            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 students page
            Response.Redirect("students.aspx");
        }
Exemple #2
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //check if we have an id to decide if we are adding or editing
            Int32 DepartmentID = 0;

            if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"]))
            {
                DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);
            }
            //db connection
            var conn = new ContosoEntities();

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

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

            //save new data to DB
            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 departments page
            Response.Redirect("departments.aspx");
        }
Exemple #3
0
        protected void getStudents()
        {
            //connect to db
            var conn = new ContosoEntities();

            var Students = from s in conn.Students
                           select s;

            grdStudents.DataSource = Students.ToList();
            grdStudents.DataBind();
        }
Exemple #4
0
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // function to delete department
            // determine the row clicked
            Int32 gridIndex = e.RowIndex;
            //find the department id value in selected row
            Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[gridIndex].Value);
            //connect the DB
            var conn = new ContosoEntities();

            //delete the department
            Student d = new Student();

            d.StudentID = StudentID;
            conn.Students.Attach(d);
            conn.Students.Remove(d);
            conn.SaveChanges();
            //refresh the grid view
            getStudents();
        }
Exemple #5
0
 protected void Page_Load(object sender, EventArgs e)
 {
     if (IsPostBack == false)
     {
         //check the url for an id so we know if we are  adding or editing
         if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"]))
         {
             //get id from the url
             Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);
             //connect
             var conn = new ContosoEntities();
             //lookup the selected department
             var Dep = (from d in conn.Departments
                        where d.DepartmentID == DepartmentID
                        select d).FirstOrDefault();
             //populate the form
             txtName.Text   = Dep.Name;
             txtBudget.Text = Dep.Budget.ToString();
         }
     }
 }
 protected void Page_Load(object sender, EventArgs e)
 {
     if (IsPostBack == false)
     {
         //check the url for an id so we know if we are  adding or editing
         if (!String.IsNullOrEmpty(Request.QueryString["StudentID"]))
         {
             //get id from the url
             Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);
             //connect
             var conn = new ContosoEntities();
             //lookup the selected student
             var Stu = (from s in conn.Students
                        where s.StudentID == StudentID
                        select s).FirstOrDefault();
             //populate the form
             txtLastName.Text  = Stu.LastName;
             txtFirstName.Text = Stu.FirstMidName;
             txtEDate.Text     = Stu.EnrollmentDate.ToString();
         }
     }
 }