private void button2_Click(object sender, EventArgs s) { MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult result = MessageBox.Show("Are you sure you want to delete this student?", "Confirm Delete", buttons); if (result == DialogResult.Yes) { try { int delete = listBox1.SelectedIndex; var employee = stu[delete]; using (var db = new CodeFirstContext()) { db.Students.Attach(employee); db.Students.Remove(employee); db.SaveChanges(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Unable to Delete - Contact your administrator"); } } listBox1.DataSource = Student.displayStudents(); }
private void button1_Click_1(object sender, EventArgs s) { // THIS IS THE ADD BUTTON try { using (var db = new CodeFirstContext()) { Student objStudent = new Student(); objStudent.Name = textBox2.Text; objStudent.tuition = int.Parse(textBox3.Text); objStudent.Major = textBox4.Text; db.Students.Add(objStudent); db.SaveChanges(); } MessageBox.Show("New student has been added to the database.", "Success!"); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Oops - Something went wrong."); } }
private void button1_Click_1(object sender, EventArgs e) { // THIS IS THE ADD BUTTON try { using (var db = new CodeFirstContext()) { Employee objEmployee = new Employee(); objEmployee.Name = textBox2.Text; objEmployee.Salary = int.Parse(textBox3.Text); objEmployee.JobTitle = textBox4.Text; db.Employees.Add(objEmployee); db.SaveChanges(); } MessageBox.Show("New employee has been added to the database.", "Success!"); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Oops - Something went wrong."); } }