private void DeleteStudent_Click(object sender, EventArgs e)
        {
            int index     = StudentsDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected);
            int studentId = Convert.ToInt32(StudentsDataGridView.Rows[index].Cells["StudentId"].Value);

            using (StudentsManagerContext db = new StudentsManagerContext())
            {
                Student student = db.Students.Find(studentId);
                db.Students.Remove(student);
                db.SaveChanges();
            }

            MessageBox.Show("Student record is deleted from the system.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);


            // Reload data into DataGridView ...
            LoadDataIntoDataGridView();
        }
        private void SaveRecordButton_Click(object sender, EventArgs e)
        {
            Student student = new Student();

            student.StudentId = StudentId;
            student.FullName  = FullNameTextBox.Text;
            student.Email     = EmailTextBox.Text;
            student.Mobile    = MobileTextBox.Text;
            student.Phone     = PhoneTextBox.Text;
            student.Gender    = GetGender();
            student.CSharp    = CSharpCheckBox.Checked;
            student.VBNET     = VBNETCheckBox.Checked;
            student.HTML      = HtmlCheckBox.Checked;
            student.Photoshop = PhotoshopCheckBox.Checked;
            student.Notes     = NotesTextBox.Text;

            if (!this.IsUpdate)
            {
                using (StudentsManagerContext db = new StudentsManagerContext())
                {
                    db.Students.Add(student);
                    db.SaveChanges();
                }

                MessageBox.Show("Student record is added to the system.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                using (StudentsManagerContext db = new StudentsManagerContext())
                {
                    db.Entry(student).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();

                    MessageBox.Show("Student record is updated.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            this.Close();
        }
Esempio n. 3
0
 public void Add(TEntity obj)
 {
     _context.Set <TEntity>().Add(obj);
     _context.SaveChanges();
 }