protected void GetDepartment() { //connect using (ContosoEntities conn = new ContosoEntities()) { //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 dep in conn.Departments join c in conn.Courses on dep.DepartmentID equals c.DepartmentID where dep.DepartmentID == DepartmentID select new { c.CourseID, c.Title, dep.Name}); grdCourses.DataSource = objE.ToList(); grdCourses.DataBind(); pnlCourses.Visible = true; } }
protected void btnSave_Click(object sender, EventArgs e) { //connect using (ContosoEntities conn = new ContosoEntities()) { Student s = new Student(); if (Request.QueryString["StudentID"] != null) { Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); s = (from stu in conn.Students where stu.StudentID == StudentID select stu).FirstOrDefault(); } s.FirstMidName = txtFirstName.Text; s.LastName = txtLastName.Text; s.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text); if (Request.QueryString.Count == 0) { conn.Students.Add(s); } conn.SaveChanges(); Response.Redirect("students.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { //connect using (ContosoEntities conn = new ContosoEntities()) { //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"); } }
protected void btnSave_Click(object sender, EventArgs e) { //connect using (ContosoEntities conn = new ContosoEntities()) { //instantiate a new deparment object in memory Cours c = new Cours(); //decide if updating or adding, then save if (!String.IsNullOrEmpty(Request.QueryString["CourseID"])) { Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); c = (from cour in conn.Courses where cour.CourseID == CourseID select cour).FirstOrDefault(); } //fill the properties of our object from the form inputs c.Title = txtTitle.Text; c.Credits = Convert.ToInt32(txtCredits.Text); c.DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue); if (String.IsNullOrEmpty(Request.QueryString["CourseID"])) { conn.Courses.Add(c); } conn.SaveChanges(); //redirect to updated departments page Response.Redirect("courses.aspx"); } }
protected void GetStudents() { //connect using our connection string from web.config and EF context class using (ContosoEntities conn = new ContosoEntities()) { var studs = from s in conn.Students select new { s.StudentID, s.FirstMidName, s.LastName }; String sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); grdStudents.DataSource = studs.AsQueryable().OrderBy(sort).ToList(); grdStudents.DataBind(); } }
protected void GetStudent() { //connect using (ContosoEntities db = new ContosoEntities()) { Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //populate a student instance with the StudentID from the URL parameter Student 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; txtFirstName.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; } }
protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e) { using (ContosoEntities conn = new ContosoEntities()) { Int32 DepartmentID = Convert.ToInt32(ddlDepartment.SelectedValue); var objC = from c in conn.Courses where c.DepartmentID == DepartmentID orderby c.Title select c; ddlCourse.DataSource = objC.ToList(); ddlCourse.DataBind(); ListItem newItem = new ListItem("-Select-", "0"); ddlCourse.Items.Insert(0, newItem); } }
protected void GetDepartments() { //connect using our connection string from web.config and EF context class using (ContosoEntities conn = new ContosoEntities()) { //use link to query the Departments model var deps = from d in conn.Departments select new {d.DepartmentID, d.Name, d.Budget}; //bind the query result to the gridview String sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); grdDepartments.DataSource = deps.AsQueryable().OrderBy(sort).ToList(); grdDepartments.DataBind(); } }
protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { //connect using (ContosoEntities conn = new ContosoEntities()) { Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]); var s = (from stu in conn.Students where stu.StudentID == StudentID select stu).FirstOrDefault(); conn.Students.Remove(s); conn.SaveChanges(); GetStudents(); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"]); using (ContosoEntities db = new ContosoEntities()) { Cours objE = (from c in db.Courses where c.CourseID == CourseID select c).FirstOrDefault(); //process the deletion db.Courses.Remove(objE); db.SaveChanges(); //repopulate the page GetDepartment(); } }
protected void btnAdd_Click(object sender, EventArgs e) { using (ContosoEntities conn = new ContosoEntities()) { Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); Int32 StudentID = Convert.ToInt32(ddlStudent.SelectedValue); Enrollment objE = new Enrollment(); objE.StudentID = StudentID; objE.CourseID = CourseID; conn.Enrollments.Add(objE); conn.SaveChanges(); GetCourse(); } }
protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e) { //connect using (ContosoEntities conn = new ContosoEntities()) { //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(); } }
protected void GetCourse() { //connect using (ContosoEntities conn = new ContosoEntities()) { //get id from url parameter and store in a variable Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); var c = (from cour in conn.Courses where cour.CourseID == CourseID select cour).FirstOrDefault(); //populate the form from our object txtTitle.Text = c.Title; txtCredits.Text = c.Credits.ToString(); ddlDepartments.SelectedValue = Convert.ToString(c.DepartmentID); var objS = (from s in conn.Students join enr in conn.Enrollments on s.StudentID equals enr.StudentID join co in conn.Courses on enr.CourseID equals co.CourseID where enr.CourseID == CourseID select new {enr.EnrollmentID, s.FirstMidName, s.LastName }); ddlStudent.ClearSelection(); var stus = from s in conn.Students orderby s.LastName select new { s.StudentID, Name = string.Concat(s.LastName, ", ", s.FirstMidName)}; ddlStudent.DataSource = stus.ToList(); ddlStudent.DataBind(); ListItem newItem = new ListItem("-Select-", "0"); ddlStudent.Items.Insert(0, newItem); pnlStudents.Visible = true; } }
protected void GetDepartments() { //connect using our connection string from web.config and EF context class using (ContosoEntities conn = new ContosoEntities()) { //use link to query the Departments model var deps = from d in conn.Departments select new { d.DepartmentID, d.Name }; //bind the query result to the ddl ddlDepartments.DataSource = deps.ToList(); ddlDepartments.DataBind(); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get the selected EnrollmentID Int32 EnrollmentID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["EnrollmentID"]); using (ContosoEntities db = new ContosoEntities()) { Enrollment 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(); } }