//NOT WORKING, TRIED USING STUDENT ID AND ENROLLMENT ID, KEEPS THROWING ERROR WHEN //PRESSING DELETE protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { Int32 EnrollmentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["EnrollmentID"]); using (DefaultConnectionEF db = new DefaultConnectionEF()) { 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(); } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
protected void GetDepartment() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get the department id Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //get department info var d = (from dep in conn.Departments where dep.DepartmentID == DepartmentID select dep).FirstOrDefault(); //populate the form txtName.Text = d.Name; txtBudget.Text = d.Budget.ToString(); var objC = (from c in conn.Courses where c.DepartmentID == DepartmentID select new { c.Title, c.DepartmentID, c.CourseID, c.Credits }); grdDepCourses.DataSource = objC.ToList(); grdDepCourses.DataBind(); //show the course panel pnlCourses.Visible = true; } } catch (Exception e) { Response.Redirect("~/error.aspx"); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get the selected Course Int32 DepartmentID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["DepartmentID"]); try { using (DefaultConnectionEF db = new DefaultConnectionEF()) { Course objE = (from en in db.Courses where en.DepartmentID == DepartmentID select en).FirstOrDefault(); db.Courses.Remove(objE); db.SaveChanges(); //repopulate the page GetDepartment(); } } catch (Exception) { Response.Redirect("/error.aspx"); } }
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"); } }
protected void btnAdd_Click(object sender, EventArgs e) { try { using (DefaultConnectionEF db = new DefaultConnectionEF()) { //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(); } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
protected void GetCourse() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get the course id Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); //get student info var c = (from crs in conn.Courses where crs.CourseID == CourseID select crs).FirstOrDefault(); //populate the form txtTitle.Text = c.Title; txtCredits.Text = c.Credits.ToString(); ddlDepartment.SelectedValue = c.DepartmentID.ToString(); } } catch (Exception e) { Response.Redirect("~/error.aspx"); } }
protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get selected department ID Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]); var s = (from stu in conn.Students where stu.StudentID == StudentID select stu).FirstOrDefault(); //save conn.Students.Remove(s); conn.SaveChanges(); //redirect to updated departments page GetStudents(); } } catch (Exception e2) { Response.Redirect("/error.aspx"); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get the selected department id Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"]); var c = (from crs in conn.Courses where crs.CourseID == CourseID select crs).FirstOrDefault(); //delete conn.Courses.Remove(c); conn.SaveChanges(); //update the grid GetCourses(); } } catch (Exception ex) { Response.Redirect("~/error.aspx"); } }
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 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(); } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get the selected EnrollmentID Int32 EnrollmentID = Convert.ToInt32(grdCourses.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(); //process the deletion db.Enrollments.Remove(objE); db.SaveChanges(); //repopulate the page GetStudent(); } } catch { Response.Redirect("/error.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { //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"); } }
protected void ddlStudent_SelectedIndexChanged(object sender, EventArgs e) { using (DefaultConnectionEF db = new DefaultConnectionEF()) { } }
protected void GetHeading() { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get id from url parameter and store in a variable Int32 HeadingID = Convert.ToInt32(Request.QueryString["Heading_ID"]); var c = (from head in conn.Headings where head.Heading_ID == HeadingID select head).FirstOrDefault(); //populate the form from our department object txtHeading.Text = c.Heading1; var categoriesUnder = c.Categories_Under.ToString(); var allCategories = (from head in conn.Categories select head.Category_ID).ToList(); var catLength = allCategories.Count(); int lastID = allCategories[catLength - 1]; for (int i = 0; i < lastID; i++) { if (categoriesUnder.Contains((i + 1).ToString())) { chklstCategories.Items[i].Selected = 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 GetDepartments() { try { //connect using our connection string from web.config and EF context class using (DefaultConnectionEF db = new DefaultConnectionEF()) { //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(); } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
protected void btnSave_OnClick(object sender, EventArgs e) { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //instantiate a new deparment object in memory Category c = new Category(); //decide if updating or adding, then save if (Request.QueryString.Count > 0) { Int32 CategoryID = Convert.ToInt32(Request.QueryString["Category_ID"]); c = (from category in conn.Categories where category.Category_ID == CategoryID select category).FirstOrDefault(); } //fill the properties of our object from the form inputs c.Category1 = txtCategory.Text; if (Request.QueryString.Count == 0) { conn.Categories.Add(c); } conn.SaveChanges(); //redirect to updated departments page Response.Redirect("/lafargeUser/categories.aspx"); } }
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 GetDepartments() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get the student id Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); //get student info var d = (from dep in conn.Departments orderby dep.Name select dep); //populate the form ddlDepartment.DataSource = d.ToList(); ddlDepartment.DataBind(); } } catch (Exception e) { Response.Redirect("~/error.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { //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"); } }
protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e) { try { 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); } } catch { Response.Redirect("/error.aspx"); } }
protected void GetStudent() { //populate form with existing student record Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); try { //connect to db via EF using (DefaultConnectionEF db = new DefaultConnectionEF()) { //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"); } //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; } } catch { Response.Redirect("/error.aspx"); } }
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"]); try { //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(); } } catch { Response.Redirect("/error.aspx"); } //refresh the grid GetStudents(); }
protected void btnSave_Click(object sender, EventArgs e) { //save or update data using (DefaultConnectionEF db = new DefaultConnectionEF()) { Designation design = new Designation(); if (!String.IsNullOrEmpty(Request.QueryString["DesignationId"])) { Int32 DesignationId = Convert.ToInt32(Request.QueryString["DesignationId"]); design = (from c in db.Designations where c.DesignationId == DesignationId select c).FirstOrDefault(); } //fetch the information to edit design.DesignationName = txtName.Text; design.DepartmentId= Convert.ToInt32(ddlDepartment.SelectedValue); if (String.IsNullOrEmpty(Request.QueryString["DesignationId"])) { //insert db.Designations.Add(design); } //save and redirect user to display page db.SaveChanges(); Response.Redirect("designation.aspx"); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get the Department Id Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"]); var c = (from course in conn.Courses where course.CourseID == CourseID select course).FirstOrDefault(); //process the delete conn.Courses.Remove(c); conn.SaveChanges(); //update the grid GetCourses(); } } catch (System.IO.IOException e2) { Server.Transfer("/error.aspx", true); } }
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 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 Enrollment objE = new Enrollment(); objE.StudentID = StudentID; objE.CourseID = CourseID; //save db.Enrollments.Add(objE); db.SaveChanges(); //refresh GetStudent(); } } catch { Response.Redirect("/error.aspx"); } }
private void fillBtnPnl() { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { var SelectedPlant = Convert.ToInt32(Session["selectedPlant"]); var SelectedCategory = Convert.ToInt32(Session["selectedCategory"]); //create a list the holds the plants table var b = (from equip in conn.Equipments where equip.Category_ID == SelectedCategory & equip.Plant_ID == SelectedPlant select equip).ToList(); for (int i = 0; i < b.Count; i++) { HyperLink equipmentButton = new HyperLink(); equipmentButton.Text = b[i].Name; equipmentButton.Attributes.Add("data-role", "button"); equipmentButton.Attributes.Add("data-inline", "true"); equipmentButton.NavigateUrl = "survey.aspx?selectedEquipment=" + b[i].Unit_Number; pnlButtons.Controls.Add(equipmentButton); } //set the datasource to the created list and bind it to the dropdown } }
protected void btnAdd_Click(object sender, EventArgs e) { try { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { Int32 StudentID = Convert.ToInt32(ddlStudent.SelectedValue); Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); Enrollment objE = new Enrollment(); objE.StudentID = StudentID; objE.CourseID = CourseID; conn.Enrollments.Add(objE); conn.SaveChanges(); //refresh GetStudents(); } } catch (Exception exception) { Response.Redirect("~/error.aspx"); } }
protected void SaveButton_Click(object sender, EventArgs e) { int DepartmentID = 0; Department departments = new Department(); using (DefaultConnectionEF db = new DefaultConnectionEF()) { if (Request.QueryString.Count > 0) { DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); departments = (from records in db.Departments where records.DepartmentID == DepartmentID select records).FirstOrDefault(); } departments.Name = DepartmentTextBox.Text; departments.Budget = Convert.ToDecimal(BudgetTextBox.Text); if (DepartmentID == 0) { db.Departments.Add(departments); } db.SaveChanges(); Response.Redirect("~/Departments.aspx"); } }
protected void GetStudents() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get the student id Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //get student info var s = (from stu in conn.Students where stu.StudentID == StudentID select stu).FirstOrDefault(); if (s != null) { //populate the form txtFirstName.Text = s.FirstMidName; txtLastName.Text = s.LastName; txtEnrollDate.Text = s.EnrollmentDate.ToString("yyyy-MM-dd"); } 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(); ddlDepartment.ClearSelection(); ddlCourse.ClearSelection(); //fill departments dropdown var deps = (from dep in conn.Departments orderby dep.Name select dep); //populate the form 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; } } catch (Exception e) { Response.Redirect("~/error.aspx"); } }
protected void btnSave_OnClick(object sender, EventArgs e) { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //instantiate a new deparment object in memory Heading c = new Heading(); //decide if updating or adding, then save if (Request.QueryString.Count > 0) { Int32 HeadingID = Convert.ToInt32(Request.QueryString["Heading_ID"]); c = (from head in conn.Headings where head.Heading_ID == HeadingID select head).FirstOrDefault(); } //fill the properties of our object from the form inputs c.Heading1 = txtHeading.Text; // Create the list to store. string selectedCategoryValues = null; // Loop through each item. foreach (ListItem item in chklstCategories.Items) { if (item.Selected) { // If the item is selected, add the value to the list. selectedCategoryValues = selectedCategoryValues + item.Value; } else { // Item is not selected, do something else. } } try { c.Categories_Under = selectedCategoryValues.ToString(); } catch (NullReferenceException) { c.Categories_Under = ""; } if (Request.QueryString.Count == 0) { conn.Headings.Add(c); } conn.SaveChanges(); //redirect to updated departments page Response.Redirect("/lafargeUser/headings.aspx"); } }
protected void convertIDtoValue(int EquipmentID) { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { var equipName = (from q in conn.Equipments where q.Unit_Number == EquipmentID select q).FirstOrDefault(); txtEqID.Text = equipName.Name; } }
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); } }
protected void btnSave_Click(object sender, EventArgs e) { //save or update data using (DefaultConnectionEF db = new DefaultConnectionEF()) { Employee emp = new Employee(); if (!String.IsNullOrEmpty(Request.QueryString["EmployeeId"])) { Int32 EmployeeId = Convert.ToInt32(Request.QueryString["EmployeeId"]); emp = (from c in db.Employees where c.EmployeeId == EmployeeId select c).FirstOrDefault(); } //fetch data from input form to fill out textbox emp.FirstName = txtFirstname.Text; emp.LastName = txtLastname.Text; emp.Email = txtEmail.Text; emp.Gender = ddlGender.SelectedValue; emp.DateofBirth = Convert.ToDateTime(txtDob.Text); emp.DepartmentId = Convert.ToInt32(ddlDepartment.SelectedValue); emp.DesignationId = Convert.ToInt32(ddlDesignation.SelectedValue); if (String.IsNullOrEmpty(Request.QueryString["EmployeeId"])) { var userStore = new UserStore<IdentityUser>(); var manager = new UserManager<IdentityUser>(userStore); var user = new IdentityUser() { UserName = txtUsername.Text }; IdentityResult result = manager.Create(user, txtPassword.Text); if (result.Succeeded) { emp.UserName = user.Id; } else { lblStatus.Text = result.Errors.FirstOrDefault(); lblStatus.CssClass = "label label-danger"; } //insert db.Employees.Add(emp); } //save and redirect user to display page db.SaveChanges(); Response.Redirect("employee.aspx"); } }
protected void GetCourse() { try { Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); //populate the existing course for editing using (DefaultConnectionEF db = new DefaultConnectionEF()) { 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; } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
protected void GetDepartment() { using (DefaultConnectionEF db = new DefaultConnectionEF()) { var deps = (from d in db.Departments orderby d.DepartmentName select d); ddlDepartment.DataSource = deps.ToList(); ddlDepartment.DataBind(); } }
protected void GetEmployee() { using (DefaultConnectionEF db = new DefaultConnectionEF()) { var emp = (from c in db.Employees select new { c.EmployeeId, c.AspNetUser.UserName, c.FirstName, c.LastName, c.Email, c.Gender, c.DateofBirth, c.Department.DepartmentName ,c.Designation.DesignationName}); //append the current direction to the Sort Column String Sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); grdEmployee.DataSource = emp.AsQueryable().OrderBy(Sort).ToList(); grdEmployee.DataBind(); } }
protected void GetDepartment() { using (DefaultConnectionEF db = new DefaultConnectionEF()) { var dep = (from c in db.Departments select new { c.DepartmentId, c.DepartmentName}); //append the current direction to the Sort Column String Sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); grdDepartment.DataSource = dep.AsQueryable().OrderBy(Sort).ToList(); grdDepartment.DataBind(); } }
protected void GetDepartment() { //fetch department information to edit using (DefaultConnectionEF db = new DefaultConnectionEF()) { Int32 DepartmentId = Convert.ToInt32(Request.QueryString["DepartmentId"]); Department dept = (from c in db.Departments where c.DepartmentId == DepartmentId select c).FirstOrDefault(); //fills the textbox txtName.Text = dept.DepartmentName; } }
protected void grdEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 EmployeeId = Convert.ToInt32(grdEmployee.DataKeys[e.RowIndex].Values["EmployeeId"].ToString()); using (DefaultConnectionEF db = new DefaultConnectionEF()) { Employee emp = (from c in db.Employees where c.EmployeeId == EmployeeId select c).FirstOrDefault(); db.Employees.Remove(emp); db.SaveChanges(); } GetEmployee(); }
protected void GetDepartment() { using (DefaultConnectionEF db = new DefaultConnectionEF()) { var deps = (from d in db.Departments orderby d.DepartmentName select d); ddlDepartment.DataSource = deps.ToList(); ddlDepartment.DataBind(); ListItem newItem = new ListItem("-Select-", "0"); ddlDepartment.Items.Insert(0, newItem); } }
protected void GetDesignation() { //fetch designation information to edit using (DefaultConnectionEF db = new DefaultConnectionEF()) { Int32 DesignationId = Convert.ToInt32(Request.QueryString["DesignationId"]); Designation design = (from c in db.Designations where c.DesignationId == DesignationId select c).FirstOrDefault(); //populate the form txtName.Text = design.DesignationName; ddlDepartment.SelectedValue = design.DepartmentId.ToString(); } }
protected void grdDepartment_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 DepartmentId = Convert.ToInt32(grdDepartment.DataKeys[e.RowIndex].Values["DepartmentId"].ToString()); using (DefaultConnectionEF db = new DefaultConnectionEF()) { Department objC = (from c in db.Departments where c.DepartmentId == DepartmentId select c).FirstOrDefault(); db.Departments.Remove(objC); db.SaveChanges(); } GetDepartment(); }
protected void btnSave_Click(object sender, EventArgs e) { try { //use EF to connect to SQL Server using (DefaultConnectionEF db = new DefaultConnectionEF()) { //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"); } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
protected void GetLeave() { //fetch the information to edit using (DefaultConnectionEF db = new DefaultConnectionEF()) { Int32 LeaveId = Convert.ToInt32(Request.QueryString["LeaveId"]); Employee_Leave leave = (from c in db.Employee_Leave where c.LeaveId == LeaveId select c).FirstOrDefault(); //fetch data from db txtFromDate.Text = String.Format("{0:yyyy-MM-dd}", leave.FromDate); txtToDate.Text = String.Format("{0:yyyy-MM-dd}", leave.ToDate); txtMessage.Text = leave.LeaveMessage; ddlStatus.SelectedValue = leave.LeaveStatus; } }
protected void btnAdd_Click(object sender, EventArgs e) { //save or update using (DefaultConnectionEF db = new DefaultConnectionEF()) { Employee_Leave leave = new Employee_Leave(); if (!String.IsNullOrEmpty(Request.QueryString["LeaveId"])) { Int32 LeaveId = Convert.ToInt32(Request.QueryString["LeaveId"]); leave = (from c in db.Employee_Leave where c.LeaveId == LeaveId select c).FirstOrDefault(); } //fetch infromation from input form leave.FromDate = Convert.ToDateTime(txtFromDate.Text); ; leave.ToDate = Convert.ToDateTime(txtToDate.Text); leave.LeaveMessage = txtMessage.Text; leave.LeaveStatus = ddlStatus.SelectedValue; if (String.IsNullOrEmpty(Request.QueryString["LeaveId"])) { leave.LeaveStatus = "Submitted"; if (HttpContext.Current.User.Identity.IsAuthenticated) { leave.UserName = HttpContext.Current.User.Identity.GetUserId(); } //insert db.Employee_Leave.Add(leave); } //save and redirect user to diaply page db.SaveChanges(); Response.Redirect("leave.aspx"); } }
protected void GetCourses() { try { using (DefaultConnectionEF db = new DefaultConnectionEF()) { //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(); } } catch (System.IO.IOException) { 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 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 (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
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 (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
protected void GetDepartment() { try { //get id from url parameter and store in a variable Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //connect using (DefaultConnectionEF db = new DefaultConnectionEF()) { //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; } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
protected void GetStudent() { try { //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 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; } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e) { try { using (DefaultConnectionEF db = new DefaultConnectionEF()) { //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); } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
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"]); try { //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(); } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } //refresh the grid GetStudents(); }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"].ToString()); 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 (System.IO.IOException) { Server.Transfer("/error.aspx", true); } GetCourses(); }
protected void GetDepartments() { try { Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); using (DefaultConnectionEF db = new DefaultConnectionEF()) { var deps = (from d in db.Departments orderby d.Name select d); ddlDepartment.DataSource = deps.ToList(); ddlDepartment.DataBind(); } } catch (System.IO.IOException) { Server.Transfer("/error.aspx", true); } }
protected void GetEmployee() { //fetch the employee information to edit using (DefaultConnectionEF db = new DefaultConnectionEF()) { Int32 EmployeeId = Convert.ToInt32(Request.QueryString["EmployeeId"]); Employee emp = (from c in db.Employees where c.EmployeeId == EmployeeId select c).FirstOrDefault(); //fetch data from db to fill out textbox txtFirstname.Text = emp.FirstName; txtLastname.Text = emp.LastName; txtEmail.Text = emp.Email; ddlGender.SelectedValue = emp.Gender; txtDob.Text = String.Format("{0:yyyy-MM-dd}", emp.DateofBirth); ddlDepartment.SelectedValue = emp.DepartmentId.ToString(); GetDesignation(); ddlDesignation.SelectedValue = emp.DesignationId.ToString(); } }
protected void GetDesignation() { using (DefaultConnectionEF db = new DefaultConnectionEF()) { //save the selected DepartmentID Int32 DepartmentId = Convert.ToInt32(ddlDepartment.SelectedValue); var design = from c in db.Designations where c.DepartmentId == DepartmentId orderby c.DesignationName select c; //bind to the designation dropdown ddlDesignation.DataSource = design.ToList(); ddlDesignation.DataBind(); ListItem newItem = new ListItem("-Select-", "0"); ddlDesignation.Items.Insert(0, newItem); } }