private void BtnAddUser_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(TxtFirstName.Text) && !string.IsNullOrEmpty(TxtLastName.Text) && !string.IsNullOrEmpty(TxtPassword.Text) && (dateEnrollmentDate.Checked || dateHireDate.Checked) && comboBoxDisc.Text != null) { using (var db = new SchoolEntities()) { DateTime?dt = null; var hireDate = (dateHireDate.Enabled == true) ? dateHireDate.Value : dt; var enrollDate = (dateEnrollmentDate.Enabled == true) ? dateEnrollmentDate.Value : dt; var person = new Person() { LastName = TxtLastName.Text, FirstName = TxtFirstName.Text, HireDate = hireDate, EnrollmentDate = enrollDate, Discriminator = comboBoxDisc.SelectedItem.ToString(), Password = TxtPassword.Text }; db.Person.Add(person); db.SaveChanges(); dataGridView1.DataSource = db.Person.ToList(); dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true; dataGridView1.FirstDisplayedCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0]; } TxtFirstName.ResetText(); TxtLastName.ResetText(); TxtPassword.ResetText(); dateHireDate.ResetText(); dateEnrollmentDate.ResetText(); comboBoxDisc.SelectedItem = null; } else { MessageBox.Show("You havent entered every needed information.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }