Exemplo n.º 1
0
        protected void GetDepartment()
        {
            //connect
            using (DefaultConnectionEF conn = new DefaultConnectionEF())
            {
                //get id from url parameter and store in a variable
                Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

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

                //enrollments - this code goes in the same method that populates the student form but below the existing code that's already in GetStudent()              
                var objE = (from dep in conn.Departments
                            join c in conn.Courses on dep.DepartmentID equals c.DepartmentID
                            where dep.DepartmentID == DepartmentID
                            select new { dep.DepartmentID, c.Title });

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

                //populate the form from our department object
                txtName.Text = deps.Name;
                txtBudget.Text = deps.Budget.ToString();
                //show the course panel
                pnlCourses.Visible = true;
            }
        }
Exemplo n.º 2
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnectionEF conn = new DefaultConnectionEF())
            {
                //instantiate a new deparment object in memory
                Departments d = new Departments();

                //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");
            }
        }
Exemplo n.º 3
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //do insert or update
            using (DefaultConnectionEF db = new DefaultConnectionEF())
            {
                Courses objC = new Courses();

                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");
            }
        }
Exemplo n.º 4
0
        protected void GetCourse()
        {
            //populate the existing course for editing
            using (DefaultConnectionEF db = new DefaultConnectionEF())
            {
                Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);

                Courses objC = (from c in db.Courses
                               where c.CourseID == CourseID
                               select c).FirstOrDefault();
                //enrollments - this code goes in the same method that populates the student form but below the existing code that's already in GetStudent()              
                var objE = (from en in db.Enrollments
                            join c in db.Courses on en.CourseID equals c.CourseID
                            join s in db.Students on en.StudentID equals s.StudentID
                            where en.CourseID == CourseID
                            select new { s.StudentID,s.LastName,s.FirstMidName });

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

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

                //fill departments to dropdown
                var stu = from s in db.Students
                           orderby s.FirstMidName
                           select s;

                ddlStudent.DataSource = stu.ToList();
                ddlStudent.DataBind();

            }
        }
Exemplo n.º 5
0
        protected void GetStudent()
        {
            //populate form with existing student record
            Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

            //connect to db via EF
            using (DefaultConnectionEF db = new DefaultConnectionEF())
            {
                //populate a student instance with the StudentID from the URL parameter
                Students s = (from objS in db.Students
                             where objS.StudentID == StudentID
                             select objS).FirstOrDefault();

                //map the student properties to the form controls if we found a match
                if (s != null)
                {
                    txtLastName.Text = s.LastName;
                    txtFirstMidName.Text = s.FirstMidName;
                    txtEnrollmentDate.Text = s.EnrollmentDate.ToString("yyyy-MM-dd");
                }

                //enrollments - this code goes in the same method that populates the student form but below the existing code that's already in GetStudent()              
                var objE = (from en in db.Enrollments
                            join c in db.Courses on en.CourseID equals c.CourseID
                            join d in db.Departments on c.DepartmentID equals d.DepartmentID
                            where en.StudentID == StudentID
                            select new { en.EnrollmentID, en.Grade, c.Title, d.Name});
              
                grdCourses.DataSource = objE.ToList();
                grdCourses.DataBind();


                //clear dropdowns
                ddlDepartment.ClearSelection();
                ddlCourse.ClearSelection();

                //fill departments to dropdown
                var deps = from d in db.Departments
                           orderby d.Name
                           select d;

                ddlDepartment.DataSource = deps.ToList();
                ddlDepartment.DataBind();

                //add default options to the 2 dropdowns
                ListItem newItem = new ListItem("-Select-", "0");
                ddlDepartment.Items.Insert(0, newItem);
                ddlCourse.Items.Insert(0, newItem);

                //show the course panel
                pnlCourses.Visible = true;
            }

        }
Exemplo n.º 6
0
        protected void GetDepartments()
        {
            using (DefaultConnectionEF db = new DefaultConnectionEF())
            {
                var deps = (from d in db.Departments
                            orderby d.Name
                            select d);

                ddlDepartment.DataSource = deps.ToList();
                ddlDepartment.DataBind();
            }
        }
Exemplo n.º 7
0
        protected void GetCourses()
        {
            using (DefaultConnectionEF db = new DefaultConnectionEF())
            {
                var courses = (from c in db.Courses
                               select new { c.CourseID, c.Title, c.Credits, c.Departments.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();
            }
        }
Exemplo n.º 8
0
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"].ToString());

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

                db.Courses.Remove(objC);
                db.SaveChanges();
            }

            GetCourses();
        }
Exemplo n.º 9
0
        protected void GetStudents()
        {
            //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;
                var Students = (from s in db.Students
                               select new { s.StudentID, s.FirstMidName, s.LastName, s.Enrollments,s.EnrollmentDate });
                String Sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();

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

            }
        }
Exemplo n.º 10
0
        protected void GetDepartments()
        {

            //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 new {d.DepartmentID, d.Budget,d.Name });

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


                //bind the query result to the gridview
                grdDepartments.DataSource = deps.ToList();
                grdDepartments.DataBind();
            }
        }
Exemplo n.º 11
0
        protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //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();
                System.Diagnostics.Debug.WriteLine("hi:" + e.ToString());
                //process the delete
                conn.Departments.Remove(d);
                conn.SaveChanges();

                //update the grid
                GetDepartments();
            }
        }
Exemplo n.º 12
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //use EF to connect to SQL Server
            using (DefaultConnectionEF db = new DefaultConnectionEF())
            {

                //use the Student model to save the new record
                Students s = new Students();
                Int32 StudentID = 0;

                //check the querystring for an id so we can determine add / update
                if (Request.QueryString["StudentID"] != null)
                {
                    //get the id from the url
                    StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                    //get the current student from EF
                    s = (from objS in db.Students
                         where objS.StudentID == StudentID
                         select objS).FirstOrDefault();
                }

                s.LastName = txtLastName.Text;
                s.FirstMidName = txtFirstMidName.Text;
                s.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text);

                //call add only if we have no student ID
                if (StudentID == 0)
                {
                    db.Students.Add(s);
                }

                //run the update or insert
                db.SaveChanges();

                //redirect to the updated students page
                Response.Redirect("students.aspx");
            }
        }
Exemplo n.º 13
0
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //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())
            {

                Students 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();
        }
Exemplo n.º 14
0
        protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DefaultConnectionEF db = new DefaultConnectionEF())
            {
                //Store the selected DepartmentID
                Int32 DepartmentID = Convert.ToInt32(ddlDepartment.SelectedValue);

                var objC = from c in db.Courses
                           where c.DepartmentID == DepartmentID
                           orderby c.Title
                           select c;

                //bind to the course dropdown
                ddlCourse.DataSource = objC.ToList();
                ddlCourse.DataBind();

                //add default options to the 2 dropdowns
                ListItem newItem = new ListItem("-Select-", "0");
                ddlCourse.Items.Insert(0, newItem);
            }
        }
Exemplo n.º 15
0
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            using (DefaultConnectionEF db = new DefaultConnectionEF())
            {
                //get the values needed
                Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);
                Int32 CourseID = Convert.ToInt32(ddlCourse.SelectedValue);

                //populate the new enrollment object
                Enrollments objE = new Enrollments();
                objE.StudentID = StudentID;
                objE.CourseID = CourseID;
                objE.Grade = 0;
                //save
                db.Enrollments.Add(objE);
                db.SaveChanges();

                //refresh
                GetStudent();
            }
        }
Exemplo n.º 16
0
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get the selected EnrollmentID
            Int32 EnrollmentID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["EnrollmentID"]);

            using (DefaultConnectionEF db = new DefaultConnectionEF())
            {
                Enrollments objE = (from en in db.Enrollments
                                   where en.EnrollmentID == EnrollmentID
                                   select en).FirstOrDefault();

                //process the deletion
                db.Enrollments.Remove(objE);
                db.SaveChanges();

                //repopulate the page
                GetStudent();
            }
        }