protected void GetDepartment() { //get id from url parameter and store in a variable Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //connect using (DefaultConnection db = new DefaultConnection()) { //populate a department instance with the DeparmtnetID from the URL parameter Department d = (from dep in db.Departments where dep.DepartmentID == DepartmentID select dep).FirstOrDefault(); //populate the form from our department object txtName.Text = d.Name; txtBudget.Text = d.Budget.ToString(); //courses table var objE = (from c in db.Courses join dp in db.Departments on c.DepartmentID equals dp.DepartmentID where c.DepartmentID == DepartmentID select new { c.CourseID, c.Title, c.Credits}); grdCourses.DataSource = objE.ToList(); grdCourses.DataBind(); //show the course panel pnlCourses.Visible = true; } }
protected void btnSave_Click(object sender, EventArgs e) { //do insert or update using (DefaultConnection db = new DefaultConnection()) { 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"); } }
protected void ddlStudent_SelectedIndexChanged(object sender, EventArgs e) { using (DefaultConnection db = new DefaultConnection()) { } }
protected void btnSave_Click(object sender, EventArgs e) { //connect using (DefaultConnection conn = new DefaultConnection()) { //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 GetCourses() { using (DefaultConnection db = new DefaultConnection()) { //gets all the couses in memory 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(); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"].ToString()); using (DefaultConnection db = new DefaultConnection()) { Course objC = (from c in db.Courses where c.CourseID == CourseID select c).FirstOrDefault(); db.Courses.Remove(objC); db.SaveChanges(); } GetCourses(); }
protected void GetCourse() { Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); //populate the existing course for editing using (DefaultConnection db = new DefaultConnection()) { 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(); //students table var objE = (from c in db.Enrollments join dp in db.Students on c.StudentID equals dp.StudentID join d in db.Courses on c.CourseID equals d.CourseID where c.CourseID == CourseID select new {c.EnrollmentID, c.StudentID, dp.LastName, dp.FirstMidName, dp.EnrollmentDate}); grdStudents.DataSource = objE.ToList(); grdStudents.DataBind(); //clear dropdowns ddlStudent.ClearSelection(); ddlCourse.ClearSelection(); //fill studentdropdown var dd = from d in db.Students //orderby d.FirstMidName select d; ddlStudent.DataSource = dd.ToList(); ddlStudent.DataBind(); //add default options to the 2 dropdownsE ListItem newItem = new ListItem("-Select-", "0"); ddlStudent.Items.Insert(0, newItem); ddlCourse.Items.Insert(0, newItem); //show the course panel pnlStudents.Visible = true; } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"]); using (DefaultConnection db = new DefaultConnection()) { Course objE = (from en in db.Courses where en.CourseID == CourseID select en).FirstOrDefault(); //processs the deletetion db.Courses.Remove(objE); db.SaveChanges(); //reopoulate the page GetDepartment(); } }
protected void GetStudents() { //connect to EF using (DefaultConnection db = new DefaultConnection()) { //query the students table using EF and LINQ var students = (from s in db.Students select new {s.StudentID, s.LastName, s.FirstMidName, s.EnrollmentDate}); //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.ToList(); grdStudents.DataSource = students.AsQueryable().OrderBy(Sort).ToList(); grdStudents.DataBind(); } }
protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e) { using (DefaultConnection db = new DefaultConnection()) { //store the selcted 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); } }
protected void GetDepartments() { //connect using our connection string from web.config and EF context class using (DefaultConnection db = new DefaultConnection()) { //use link to query the Departments model var departments = (from d in db.Departments select new { d.DepartmentID, d.Name, d.Budget }); //var deps = from d in conn.Departments //select d; //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 = departments.ToList(); grdDepartments.DataSource = departments.AsQueryable().OrderBy(Sort).ToList(); grdDepartments.DataBind(); } }
protected void btnSave_Click(object sender, EventArgs e) { //use EF to connect to SQL Server using (DefaultConnection db = new DefaultConnection()) { //use the Student model to save the new record Student s = new Student(); 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"); } }
protected void btnAdd_Click(object sender, EventArgs e) { using (DefaultConnection db = new DefaultConnection()) { //get tje values needed Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); Int32 CourseID = Convert.ToInt32(ddlCourse.SelectedValue); //populate the new enrollment object Enrollment objE = new Enrollment(); objE.StudentID = StudentID; objE.CourseID = CourseID; //save db.Enrollments.Add(objE); db.SaveChanges(); //refresh GetStudent(); } }
protected void GetStudent() { //populate form with existing student record Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //connect to db via EF using (DefaultConnection db = new DefaultConnection()) { //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; txtFirstMidName.Text = s.FirstMidName; txtEnrollmentDate.Text = s.EnrollmentDate.ToString("yyyy-MM-dd"); } 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; } }
//NOT WORKING, TRIED USING STUDENT ID AND ENROLLMENT ID, KEEPS THROWING ERROR WHEN //PRESSING DELETE protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 EnrollmentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["EnrollmentID"]); using (DefaultConnection db = new DefaultConnection()) { Enrollment objE = (from en in db.Enrollments where en.EnrollmentID == EnrollmentID select en).FirstOrDefault(); //processs the deletetion db.Enrollments.Remove(objE); db.SaveChanges(); //reopoulate the page GetCourse(); } }
protected void GetDepartments() { Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); using (DefaultConnection db = new DefaultConnection()) { var deps = (from d in db.Departments orderby d.Name select d); ddlDepartment.DataSource = deps.ToList(); ddlDepartment.DataBind(); } }
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 (DefaultConnection db = new DefaultConnection()) { 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(); }
protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e) { //connect using (DefaultConnection conn = new DefaultConnection()) { //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(); }