protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e) { //identify the DepartmentID to be deleted from the row the user selected Int32 DepartmentID = Convert.ToInt32(grdDepartments.DataKeys[e.RowIndex].Values["DepartmentID"]); //connect using (DefaultConnection db = new DefaultConnection()) { Department dep = (from d in db.Departments where d.DepartmentID == DepartmentID select d).FirstOrDefault(); //delete db.Departments.Remove(dep); db.SaveChanges(); //refresh grid GetDepartments(); } }
protected void btnSave_Click(object sender, EventArgs e) { //connect using (DefaultConnection db = new DefaultConnection()) { //create a new course and fill the properties Course objC = new Course(); objC.Title = txtTitle.Text; objC.Credits = Convert.ToInt32(txtCredits.Text); objC.DepartmentID = Convert.ToInt32(ddlDepartment.SelectedValue); //save db.Courses.Add(objC); db.SaveChanges(); //redirect Response.Redirect("courses.aspx"); } }
protected void btnRegister_Click(object sender, EventArgs e) { //connect using (DefaultConnection db = new DefaultConnection()) { //create a new instructor Instructor objI = new Instructor(); //fill the properties from the form inputs objI.FirstName = txtFirstName.Text; objI.LastName = txtLastName.Text; objI.Username = txtUsername.Text; objI.DepartmentID = Convert.ToInt32(ddlDepartment.SelectedValue); //salt and hash the plain text password String password = txtPassword.Text; String salt = CreateSalt(8); String pass_and_salt = password + salt; // Create a new instance of the hash crypto service provider. HashAlgorithm hashAlg = new SHA256CryptoServiceProvider(); // Convert the data to hash to an array of Bytes. byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt); // Compute the Hash. This returns an array of Bytes. byte[] bytHash = hashAlg.ComputeHash(bytValue); // Optionally, represent the hash value as a base64-encoded string, // For example, if you need to display the value or transmit it over a network. string base64 = Convert.ToBase64String(bytHash); objI.Password = base64; objI.Salt = salt; //save db.Instructors.Add(objI); db.SaveChanges(); } }
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 btnSave_Click(object sender, EventArgs e) { //connect using (DefaultConnection db = new DefaultConnection()) { //create a new department in memory Department dep = new Department(); Int32 DepartmentID = 0; //check for a url if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"])) { //get the id from the url DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //look up the department dep = (from d in db.Departments where d.DepartmentID == DepartmentID select d).FirstOrDefault(); } //fill the properties of the new department dep.Name = txtName.Text; dep.Budget = Convert.ToDecimal(txtBudget.Text); //add if we have no id in the url if (DepartmentID == 0) { db.Departments.Add(dep); } //save the new department db.SaveChanges(); //redirect to the departments list page Response.Redirect("departments.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"]); //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(); }