コード例 #1
0
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try { 
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //get selected department ID
                    Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"]);

                    var c = (from course in conn.Courses
                             where course.CourseID == CourseID
                             select course).FirstOrDefault();
                    //save
                    conn.Courses.Remove(c);
                    conn.SaveChanges();

                    //redirect to updated departments page
                    GetCourses();
                }
            }
            catch (Exception e2)
            {
                Response.Redirect("/error.aspx");
            }
        }
コード例 #2
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //instantsiate a new department object in memory
                    Department d = new Department();

                    //decide if updating or adding then save
                    if (Request.QueryString.Count > 0)
                    {
                        Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);
                        d = (from dep in conn.Departments
                             where dep.DepartmentID == DepartmentID
                             select dep).FirstOrDefault();
                    }

                    //fill the properties of our object from the form inputs
                    d.Name = txtName.Text;
                    d.Budget = Convert.ToDecimal(txtBudget.Text);

                    if (Request.QueryString.Count == 0)
                    {
                        conn.Departments.Add(d);
                    }
                    conn.SaveChanges();

                    //redirect to updated departments page
                    Response.Redirect("departments.aspx");
                }
            }
            catch (Exception e2)
            {
                Response.Redirect("/error.aspx");
            }
        }
コード例 #3
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try { 
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //instantsiate a new course object in memory
                    Course objC = new Course();

                    //decide if updating or adding then save
                    if (!String.IsNullOrEmpty(Request.QueryString["CourseID"]))
                    {
                        Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);
                        objC = (from c in conn.Courses
                                where c.CourseID == CourseID
                                select c).FirstOrDefault();
                    }

                    //fill the properties of our object from the form inputs
                    objC.Title = txtTitle.Text;
                    objC.Credits = Convert.ToInt32(txtCredits.Text);
                    objC.DepartmentID = Convert.ToInt32(ddlDepartment.SelectedValue);

                    if (String.IsNullOrEmpty(Request.QueryString["CourseID"])) {
                        conn.Courses.Add(objC);
                    }

                    conn.SaveChanges();

                    //redirect to updated departments page
                    Response.Redirect("courses.aspx");
                }
            }
            catch (Exception e3)
            {
                Response.Redirect("/error.aspx");
            }
        }