private void deleteBtn_Click(object sender, EventArgs e) { KevinTB learner; int deletingId = int.Parse(learnerIDTb.Text); //Get learner for the given learnerId using (var dbContext = new KevinDBEntities()) { learner = dbContext.KevinTBs .Where(l => l.learnerID == deletingId) .FirstOrDefault <KevinTB>(); } // Create new context for disconnected scenario using (var dbContext = new KevinDBEntities()) { dbContext.Entry(learner).State = EntityState.Deleted; // Save changes to database dbContext.SaveChanges(); } MessageBox.Show($"{learner.learnerName} has been deleted"); this.loadUnfilteredResults(); this.resetFormControls(); }
private void addBtn_Click(object sender, EventArgs e) { using (var dbContext = new KevinDBEntities()) { try { //create an instance of the Entity object KevinTB learner = new KevinTB { learnerID = int.Parse(learnerIDTb.Text), learnerName = learnerNameTb.Text, enrolledProgram = enrolledProgramTb.Text, favoriteSubject = favSubTb.Text, numberOfLanguages = int.Parse(numLangTb.Text), strongestSkill = strongSkillTb.Text }; //adds the given entity to the context underlying the set dbContext.KevinTBs.Add(learner); dbContext.Entry(learner).State = EntityState.Added; //save dbContext - also save data to the db dbContext.SaveChanges(); MessageBox.Show($"Added new learner to the directory: {learner.learnerName}"); this.loadUnfilteredResults(); this.resetFormControls(); } catch (Exception) { MessageBox.Show($"LearnerID and Number of Languages must be positive integer numbers!"); } } }
private void kevinTBBindingNavigatorSaveItem_Click(object sender, EventArgs e) { Validate(); //validate the input fields kevinTBBindingSource.EndEdit(); //try to save the changes try { dbContext.SaveChanges();//write changes to underlying db } catch (System.Data.Entity.Validation.DbEntityValidationException) { MessageBox.Show("Learner ID, Learner Name and Enrolled Programs must contain values", "Entity Validation Exception"); } this._refreshContacts(); //change back to unfiltered data }
private void updateBtn_Click(object sender, EventArgs e) { KevinTB learner; int updatingId = int.Parse(learnerIDTb.Text); //Get learner for the given learnerId using (var dbContext = new KevinDBEntities()) { learner = dbContext.KevinTBs .Where(l => l.learnerID == updatingId) .FirstOrDefault <KevinTB>(); } // Update learner info in disconnected mode (out of dbContext scope) try { learner.learnerID = int.Parse(learnerIDTb.Text); learner.learnerName = learnerNameTb.Text; learner.enrolledProgram = enrolledProgramTb.Text; learner.favoriteSubject = favSubTb.Text; learner.numberOfLanguages = int.Parse(numLangTb.Text); learner.strongestSkill = strongSkillTb.Text; } catch (Exception) { MessageBox.Show($"LearnerID and Number of Languages must be positive integer numbers!"); } // Save modified entity using new Context using (var dbContext = new KevinDBEntities()) { // Mark entity as modified dbContext.Entry(learner).State = EntityState.Modified; // Save changes to database dbContext.SaveChanges(); } MessageBox.Show($"{learner.learnerName} has been updated"); this.loadUnfilteredResults(); this.resetFormControls(); }