Пример #1
0
        protected void GetStudents()
        {
            try
            {
                //connect to EF
                using (DefaultConnectionEF db = new DefaultConnectionEF())
                {

                    //query the students table using EF and LINQ
                    var Students = from s in db.Students
                                   select s;

                    //append the current direction to the Sort Column
                    String Sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();

                    //bind the result to the gridview
                    grdStudents.DataSource = Students.AsQueryable().OrderBy(Sort).ToList();
                    grdStudents.DataBind();

                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #2
0
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                using (DefaultConnectionEF db = new DefaultConnectionEF())
                {
                    //get the values needed
                    Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);
                    Int32 StudentID = Convert.ToInt32(ddlStudent.SelectedValue);

                    //populate the new enrollment object
                    Enrollment objE = new Enrollment();
                    objE.StudentID = StudentID;
                    objE.CourseID = CourseID;

                    //save
                    db.Enrollments.Add(objE);
                    db.SaveChanges();

                    //refresh
                    GetCourse();
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #3
0
        protected void GetCourses()
        {
            try
            {
                using (DefaultConnectionEF db = new DefaultConnectionEF())
                {
                    var courses = (from c in db.Courses
                                   select new { c.CourseID, c.Title, c.Credits, c.Department.Name });

                    //append the current direction to the Sort Column
                    String Sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();

                    grdCourses.DataSource = courses.AsQueryable().OrderBy(Sort).ToList();
                    grdCourses.DataBind();
                }
            }
            catch(Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #4
0
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"].ToString());

            try{
                using (DefaultConnectionEF db = new DefaultConnectionEF())
                {
                    Course objC = (from c in db.Courses
                                   where c.CourseID == CourseID
                                   select c).FirstOrDefault();

                    db.Courses.Remove(objC);
                    db.SaveChanges();
                }
            }
            catch(Exception)
            {
                Response.Redirect("/error.aspx");
            }

            GetCourses();
        }
Пример #5
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //instantiate a new deparment 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)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #6
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //do insert or update
                using (DefaultConnectionEF db = new DefaultConnectionEF())
                {
                    Course objC = new Course();

                    if (!String.IsNullOrEmpty(Request.QueryString["CourseID"]))
                    {
                        Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);
                        objC = (from c in db.Courses
                                where c.CourseID == CourseID
                                select c).FirstOrDefault();
                    }

                    //populate the course from the input form
                    objC.Title = txtTitle.Text;
                    objC.Credits = Convert.ToInt32(txtCredits.Text);
                    objC.DepartmentID = Convert.ToInt32(ddlDepartment.SelectedValue);

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

                    //save and redirect
                    db.SaveChanges();
                    Response.Redirect("courses.aspx");
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #7
0
        protected void GetDepartments()
        {
            try
            {
                //connect using our connection string from web.config and EF context class
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //use link to query the Departments model
                    var deps = from d in conn.Departments
                               select d;

                    String Sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();

                    //bind the query result to the gridview
                    grdDepartments.DataSource = deps.AsQueryable().OrderBy(Sort).ToList();
                    grdDepartments.DataBind();
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #8
0
        protected void GetDepartment()
        {
            try
            {
                //connect
                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();

                    var objE = (from en in conn.Courses
                                join c in conn.Departments on
                                 en.DepartmentID equals c.DepartmentID
                                where en.DepartmentID == DepartmentID
                                select new { en.Title, en.DepartmentID });

                    grdCourses.DataSource = objE.ToList();
                    grdCourses.DataBind();

                    //show the courses
                    grdCourses.Visible = true;
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #9
0
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                //store which row was clicked
                Int32 selectedRow = e.RowIndex;

                //get the selected StudentID using the grid's Data Key collection
                Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[selectedRow].Values["StudentID"]);

                //use EF to remove the selected student from the db
                using (DefaultConnectionEF db = new DefaultConnectionEF())
                {

                    Student s = (from objS in db.Students
                                 where objS.StudentID == StudentID
                                 select objS).FirstOrDefault();

                    //do the delete
                    db.Students.Remove(s);
                    db.SaveChanges();
                }

                //refresh the grid
                GetStudents();
            }
            catch (Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #10
0
        protected void GetCourse()
        {
            //populate the existing course for editing
            try
            {
                using (DefaultConnectionEF db = new DefaultConnectionEF())
                {
                    Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);

                    Course objC = (from c in db.Courses
                                   where c.CourseID == CourseID
                                   select c).FirstOrDefault();

                    //populate the form
                    txtTitle.Text = objC.Title;
                    txtCredits.Text = objC.Credits.ToString();
                    ddlDepartment.SelectedValue = objC.DepartmentID.ToString();

                    var objE = (from en in db.Enrollments
                                join d in db.Students on en.StudentID equals d.StudentID
                                join e in db.Courses on en.CourseID equals e.CourseID
                                where en.CourseID == CourseID
                                select new { en.EnrollmentID, d.LastName, d.FirstMidName });

                    grdStudents.DataSource = objE.ToList();
                    grdStudents.DataBind();

                    ddlStudent.ClearSelection();

                    var studs = from d in db.Students select d;
                    ddlStudent.DataSource = studs.ToList();
                    ddlStudent.DataBind();

                    ListItem newItem = new ListItem("-Select-", "0");
                    ddlStudent.Items.Insert(0, newItem);

                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #11
0
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get the selected EnrollmentID
            Int32 EnrollmentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["EnrollmentID"]);
            try
            {
                using (DefaultConnectionEF db = new DefaultConnectionEF())
                {
                    Enrollment objE = (from en in db.Enrollments
                                       where en.EnrollmentID == EnrollmentID
                                       select en).FirstOrDefault();

                    db.Enrollments.Remove(objE);
                    db.SaveChanges();

                    //repopulate the page
                    GetCourse();
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #12
0
        protected void GetDepartments()
        {
            try
            {
                using (DefaultConnectionEF db = new DefaultConnectionEF())
                {
                    var deps = (from d in db.Departments
                                orderby d.Name
                                select d);

                    ddlDepartment.DataSource = deps.ToList();
                    ddlDepartment.DataBind();
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }
Пример #13
0
        protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //get the selected DepartmentID
                    Int32 DepartmentID = Convert.ToInt32(grdDepartments.DataKeys[e.RowIndex].Values["DepartmentID"]);

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

                    //process the delete
                    conn.Departments.Remove(d);
                    conn.SaveChanges();

                    //update the grid
                    GetDepartments();
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.aspx");
            }
        }