protected void btnDelete_Click(object sender, EventArgs e) { int SelectedEmpID = string.IsNullOrEmpty(ddlEmployee.SelectedValue) ? -1 : int.Parse(ddlEmployee.SelectedValue); LearnEFEntities db = new LearnEFEntities(); var empQuery = from emp in db.Employees where emp.EmpId == SelectedEmpID select emp; if (!empQuery.Any()) { return; } //create a new object using the value of EmpId Employee objEmp = new Employee() { EmpId = SelectedEmpID }; //attach object in the entity set db.Employees.Attach(objEmp); //mark the object for deletion db.Employees.Remove(objEmp); //save changes db.SaveChanges(); ReloadData(); txtHREmpID.Text = ""; txtFirstName.Text = ""; txtLastName.Text = ""; txtAddress.Text = ""; txtCity.Text = ""; }
protected void btUpdate_Click(object sender, EventArgs e) { int SelectedEmpID = string.IsNullOrEmpty(ddlEmployee.SelectedValue) ? -1 : int.Parse(ddlEmployee.SelectedValue); //Read the record from the database. LearnEFEntities db = new LearnEFEntities(); //following query will fetch a record based upon the EmpID passed through local variable empId var empQuery = from emp in db.Employees where emp.EmpId == SelectedEmpID select emp; Employee objEmp; if (!empQuery.Any()) { objEmp = new Employee(); db.Employees.Add(objEmp); } else { objEmp = empQuery.Single(); } //set the new values of the columns (properties), based upon the values entered using the text boxes objEmp.HREmpId = txtHREmpID.Text; objEmp.FirstName = txtFirstName.Text; objEmp.LastName = txtLastName.Text; objEmp.Address = txtAddress.Text; objEmp.City = txtCity.Text; //save your changes into the database db.SaveChanges(); ReloadData(); ddlEmployee.SelectedValue = objEmp.EmpId.ToString(); }
protected void btnDelete_Click(object sender, EventArgs e) { Int32 empId = Convert.ToInt32(btnSave.CommandArgument); if (empId != 0) { LearnEFEntities db = new LearnEFEntities(); Employee objEmp = new Employee() { EmpId = empId }; db.Employees.Attach(objEmp); db.Employees.DeleteObject(objEmp); db.SaveChanges(); //reset the form and grid ClearEmpForm(); PopulateEmpList(); } }
protected void btnSave_Click(object sender, EventArgs e) { Int32 empId = Convert.ToInt32(btnSave.CommandArgument); if (empId == 0) { //INSERT LearnEFEntities db = new LearnEFEntities(); Employee objEmp = new Employee(); objEmp.HREmpId = txtHREmpId.Text; objEmp.FirstName = txtFirstName.Text; objEmp.LastName = txtLastName.Text; objEmp.Address = txtAddress.Text; objEmp.City = txtCity.Text; db.Employees.AddObject(objEmp); db.SaveChanges(); } else { //UPDATE LearnEFEntities db = new LearnEFEntities(); var empQuery = from emp in db.Employees where emp.EmpId == empId select emp; Employee objEmp = empQuery.Single(); objEmp.HREmpId = txtHREmpId.Text; objEmp.FirstName = txtFirstName.Text; objEmp.LastName = txtLastName.Text; objEmp.Address = txtAddress.Text; objEmp.City = txtCity.Text; db.SaveChanges(); } ClearEmpForm(); PopulateEmpList(); }
protected void btnSave_Click(object sender, EventArgs e) { Int32 empId = Convert.ToInt32(btnSave.CommandArgument); if (empId == 0) { //INSERT LearnEFEntities db = new LearnEFEntities(); Employee objEmp = new Employee(); objEmp.HREmpId = txtHREmpId.Text; objEmp.FirstName = txtFirstName.Text; objEmp.LastName = txtLastName.Text; objEmp.Address = txtAddress.Text; objEmp.City = txtCity.Text; db.Employees.AddObject(objEmp); db.SaveChanges(); } else { //UPDATE LearnEFEntities db = new LearnEFEntities(); var empQuery = from emp in db.Employees where emp.EmpId == empId select emp; Employee objEmp = empQuery.Single(); objEmp.HREmpId = txtHREmpId.Text; objEmp.FirstName = txtFirstName.Text; objEmp.LastName = txtLastName.Text; objEmp.Address = txtAddress.Text; objEmp.City = txtCity.Text; db.SaveChanges(); } ClearEmpForm(); PopulateEmpList(); }
protected void btnDelete_Click(object sender, EventArgs e) { Int32 empId = Convert.ToInt32(btnSave.CommandArgument); if (empId != 0) { LearnEFEntities db = new LearnEFEntities(); Employee objEmp = new Employee() { EmpId = empId }; db.Employees.Attach(objEmp); db.Employees.DeleteObject(objEmp); db.SaveChanges(); //reset the form and grid ClearEmpForm(); PopulateEmpList(); } }