protected void GetStudents() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //use link to query the Students model var studs = from s in conn.Students select s; //If there is a sort parameter then override the query if (Request.QueryString.Count > 0) { studs = from s in conn.Students orderby Request.QueryString["orderby"] select s; } //bind to the gridview grdStudents.DataSource = studs.ToList(); grdStudents.DataBind(); } } catch (System.IO.IOException e) { Server.Transfer("/error.aspx", true); } }
protected void btnAdd_Click(object sender, EventArgs e) { try { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //Get the values Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); Int32 CourseID = Convert.ToInt32(ddlCourse.SelectedValue); //Instantiate the enrollment object Enrollment objE = new Enrollment(); //Populate, save, and refresh objE.StudentID = StudentID; objE.CourseID = CourseID; conn.Enrollments.Add(objE); conn.SaveChanges(); GetStudent(); } } catch (System.IO.IOException e5) { Server.Transfer("/error.aspx", true); } }
protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get the Department Id Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]); var s = (from stud in conn.Students where stud.StudentID == StudentID select stud).FirstOrDefault(); //process the delete conn.Students.Remove(s); conn.SaveChanges(); //update the grid GetStudents(); } } catch (System.IO.IOException e2) { Server.Transfer("/error.aspx", true); } }
protected void btnSave_Click(object sender, EventArgs e) { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //instantiate a new deparment object in memory Student s = new Student(); //decide if updating and then re-query the updated Students if (Request.QueryString.Count > 0) { Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); s = (from stud in conn.Students where stud.StudentID == StudentID select stud).FirstOrDefault(); } //fill in the values s.FirstMidName = txtFirstMidName.Text; s.LastName = txtLastName.Text; s.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text); //Only add a new record if it's not updating an existing one if (Request.QueryString.Count == 0) { conn.Students.Add(s); } conn.SaveChanges(); //redirect to the Students page Response.Redirect("students.aspx"); } } catch (System.IO.IOException e2) { Server.Transfer("/error.aspx", true); } }
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 and then re-query the updated departments 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 in the values d.Name = txtName.Text; d.Budget = Convert.ToDecimal(txtBudget.Text); //Only add a new record if it's not updating an existing one if (Request.QueryString.Count == 0) { conn.Departments.Add(d); } conn.SaveChanges(); //redirect to the departments page Response.Redirect("departments.aspx"); } } catch (System.IO.IOException e2) { Server.Transfer("/error.aspx", true); } }
protected void GetDepartments() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //use link to query the Departments model var deps = from d in conn.Departments select d; //bind to the gridview grdDepartments.DataSource = deps.ToList(); grdDepartments.DataBind(); } } catch (System.IO.IOException e) { Server.Transfer("/error.aspx", true); } }
protected void GetDepartments() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { var deps = (from d in conn.Departments orderby d.Name select d); //bind to the gridview ddlDepartments.DataSource = deps.ToList(); ddlDepartments.DataBind(); } } catch (System.IO.IOException e) { Server.Transfer("/error.aspx", true); } }
protected void GetCourses() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //use link to query the Courses model var course = from c in conn.Courses select new { c.CourseID, c.Title, c.Credits, c.Department.Name }; //append the current direction of the sort collumn String Sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); grdCourses.DataSource = course.AsQueryable().OrderBy(Sort).ToList(); grdCourses.DataBind(); } } catch (System.IO.IOException e) { Server.Transfer("/error.aspx", true); } }
protected void btnSave_Click(object sender, EventArgs e) { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { Courses objC = new Courses(); 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(); } //Populate the course from the input form objC.Title = txtTitle.Text; objC.Credits = Convert.ToInt32(txtCredits.Text); objC.DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue); if (String.IsNullOrEmpty(Request.QueryString["CourseID"])) { //add conn.Courses.Add(objC); } conn.SaveChanges(); Response.Redirect("courses.aspx"); } } catch (System.IO.IOException e3) { Server.Transfer("/error.aspx", true); } }
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(); } } catch (System.IO.IOException e) { Server.Transfer("/error.aspx", true); } }
protected void GetCourse() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); //use link to query the Courses model Courses objC = (from c in conn.Courses where c.CourseID == CourseID select c).FirstOrDefault(); //bind to the gridview txtTitle.Text = objC.Title; txtCredits.Text = objC.Credits.ToString(); } } catch (System.IO.IOException e2) { Server.Transfer("/error.aspx", true); } }
protected void ddlDepartments_SelectedIndexChanged(object sender, EventArgs e) { try { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //store the selected department ID Int32 DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue); var course = (from c in conn.Courses where c.DepartmentID == DepartmentID orderby c.Title select c); //bind to the ddl ddlCourse.DataSource = course.ToList(); ddlCourse.DataBind(); //Create a default option ListItem defaultItem = new ListItem("--Select--", "0"); ddlCourse.Items.Insert(0, defaultItem); } } catch (System.IO.IOException e4) { Server.Transfer("/error.aspx", true); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { //Get the desired enrollment ID Int32 EnrollmentID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["EnrollmentID"]); try { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { Enrollment objE = (from en in conn.Enrollments where en.EnrollmentID == EnrollmentID select en).FirstOrDefault(); conn.Enrollments.Remove(objE); conn.SaveChanges(); GetStudent(); } } catch (System.IO.IOException e3) { Server.Transfer("/error.aspx", true); } }
protected void GetStudent() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get id from url parameter and store in a variable Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); var s = (from stud in conn.Students where stud.StudentID == StudentID select stud).FirstOrDefault(); //populate the form from our Student object txtFirstMidName.Text = s.FirstMidName; txtLastName.Text = s.LastName; txtEnrollmentDate.Text = s.EnrollmentDate.ToString("yyyy-MM-dd"); var studentId = Convert.ToInt32(Request.QueryString["StudentID"]); var objE = (from en in conn.Enrollments join c in conn.Courses on en.CourseID equals c.CourseID join d in conn.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 the dropdown ddlDepartments.ClearSelection(); ddlCourse.ClearSelection(); var deps = (from d in conn.Departments orderby d.Name select d); //bind to the ddl ddlDepartments.DataSource = deps.ToList(); ddlDepartments.DataBind(); //Create a default option ListItem defaultItem = new ListItem("--Select--", "0"); ddlDepartments.Items.Insert(0, defaultItem); ddlCourse.Items.Insert(0, defaultItem); pnlCourses.Visible = true; } } catch (System.IO.IOException e) { Server.Transfer("/error.aspx", true); } }