protected void fv_ItemUpdating(object sender, FormViewUpdateEventArgs e) { if (this.fv.CurrentMode == FormViewMode.Edit) { var employeesListControl = this.fv.FindControl("employeesList") as CheckBoxList; if (employeesListControl != null) { var ctx = new PubsEntities(); var jobID = Convert.ToInt16(this.fv.DataKey["job_id"]); var currentJob = ctx.jobs.First(x => x.job_id == jobID); foreach (var item in employeesListControl.Items.OfType<ListItem>()) { var employee = ctx.employees.First(x => x.emp_id == item.Value); if (item.Selected) { employee.job_id = jobID; } } ctx.SaveChanges(); } } }
public void DeleteJob(Int16 ID) { var ctx = new PubsEntities(); var job = ctx.jobs.First(x => x.job_id == ID); ctx.jobs.Remove(job); ctx.SaveChanges(); }
public void UpdateJob(Int16 ID, string Description, byte Minimum, byte Maximum) { var ctx = new PubsEntities(); var job = new job(); job.job_id = ID; job.job_desc = Description; job.min_lvl = Minimum; job.max_lvl = Maximum; ctx.jobs.Attach(job); ctx.Entry(job).State = System.Data.EntityState.Modified; ctx.ChangeTracker.DetectChanges(); ctx.SaveChanges(); }
protected void addNew_Click(object sender, EventArgs e) { var j = new job { job_desc = this.description.Text, max_lvl = Convert.ToByte(this.maximum.Text), min_lvl = Convert.ToByte(this.minimum.Text) }; var ctx = new PubsEntities(); ctx.jobs.Add(j); ctx.SaveChanges(); this.status.Text = "Job added"; this.gv.DataBind(); }
protected void myCustomGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) { var row = this.myCustomGridView.Rows[e.RowIndex]; var cbl = row.FindControl("employeesList") as CheckBoxList; var employees = cbl.Items.OfType<ListItem>().Where(x => x.Selected); var ctx = new PubsEntities(); var jobID = (Int16)e.Keys[0]; foreach (var item in employees) { var employee = ctx.employees.First(x => x.emp_id.Equals(item.Value)); if (!employee.job_id.Equals(jobID)) { employee.job_id = jobID; } } ctx.ChangeTracker.DetectChanges(); ctx.SaveChanges(); }
protected void dv_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { if (this.dv.CurrentMode == DetailsViewMode.Edit) { var employeesListControl = this.dv.FindControl("employeesList") as ListBox; if (employeesListControl != null) { var ctx = new PubsEntities(); var jobID = Convert.ToInt16(this.dv.DataKey[0]); var currentJob = ctx.jobs.Include("employees").FirstOrDefault(x => x.job_id == jobID); if (currentJob != null) { foreach (var item in employeesListControl.Items.OfType<ListItem>()) { var emp = ctx.employees.First(x => x.emp_id == item.Value); if (item.Selected) { emp.job_id = jobID; } else { if (emp.job_id == jobID) { // here i would normally remove it } } } ctx.SaveChanges(); } } } }