コード例 #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 GetStudents()
        {
            try { 
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    var students = from s in conn.Students
                                   select s;

                    grdStudents.DataSource = students.ToList();
                    grdStudents.DataBind();
                }
            }
            catch (Exception e)
            {
                Response.Redirect("/error.aspx");
            }
        }
コード例 #3
0
        protected void GetDepartments()
        {
            try { 
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {

                    //use link to query the Departments model
                    var deps = from d in conn.Departments
                               select d;

                    //bind the query result to the gridview
                    grdDepartments.DataSource = deps.ToList();
                    grdDepartments.DataBind();
                }
            }
            catch (Exception e2)
            {
                Response.Redirect("/error.aspx");
            }
        }
コード例 #4
0
        protected void GetDepartment()
        {
            try {
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //get id from url parameter and store in a variable
                    Int32 DepertmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

                    var deps = (from d in conn.Departments
                             orderby d.Name
                             select d);

                    ddlDepartment.DataSource = deps.ToList();
                    ddlDepartment.DataBind();
                }
            }
            catch (Exception e2)
            {
                Response.Redirect("/error.aspx");
            }
        }
コード例 #5
0
        protected void GetDepartment()
        {
            try { 
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //get id from url parameter and store in a variable
                    Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

                    var d = (from dep in conn.Departments
                             where dep.DepartmentID == DepartmentID
                             select dep).FirstOrDefault();

                    //populate the form from our department object
                    txtName.Text = d.Name;
                    txtBudget.Text = d.Budget.ToString();
                }
            }
            catch (Exception e)
            {
                Response.Redirect("/error.aspx");
            }
        }
コード例 #6
0
        protected void GetCourses()
        {
            try
            {
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {

                    //use link to query the Departments model
                    var course = (from c in conn.Courses
                                  select new { c.CourseID, c.Title, c.Credits, c.Department.Name });

                    //bind the query result to the gridview
                    String sort = Session["SortColumn"] + " " + Session["SortDirection"].ToString();
                    grdCourses.DataSource = course.AsQueryable().OrderBy(sort).ToList();
                    grdCourses.DataBind();
                }
            }
            catch (Exception e)
            {
                Response.Redirect("/error.aspx");
            }
        }
コード例 #7
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");
            }
        }
コード例 #8
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");
            }
        }
コード例 #9
0
        protected void GetCourse()
        {
            try
            {
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //get id from url parameter and store in a variable
                    Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);

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

                    //populate the form from our department object
                    txtTitle.Text = course.Title;
                    txtCredits.Text = course.Credits.ToString();
                }
            }
            catch (Exception e)
            {
                Response.Redirect("/error.aspx");
            }
        }