private void btnAdd_Click(object sender, RoutedEventArgs e) { try { using(var ctx = new MyContext()) { var num=ctx.Employees.Where(x=> x.empId==txtEmpId.Text).Count(); if (num > 0) { System.Windows.MessageBox.Show("Employee ID already exists"); return; } if (txtEmpId.Text == "" || txtFName.Text == "" || txtLName.Text== "" || txtMName.Text=="") { System.Windows.MessageBox.Show("Please Complete the Information"); return; } Employee emp = new Employee { empId = txtEmpId.Text, FName = txtFName.Text, MI = txtMName.Text, LName = txtLName.Text, Photo = ConvertImageToByteArray(selectedFileName) }; ctx.Employees.Add(emp); ctx.SaveChanges(); System.Windows.MessageBox.Show("Employee added successfully"); img1.Source = null; resetGrid(); } } catch(Exception ex) { } }
private void btnDel_Click(object sender, RoutedEventArgs e) { object item = dg1.SelectedItem; string ID = (dg1.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text; System.Windows.Forms.DialogResult r = System.Windows.Forms.MessageBox.Show("Are you sure you want to delete this record?","Question",MessageBoxButtons.YesNo); if (r == System.Windows.Forms.DialogResult.Yes) { using (var ctx = new MyContext()) { Employee emp = new Employee(); emp = ctx.Employees.Where(x => x.empId == ID).First(); ctx.Employees.Remove(emp); ctx.SaveChanges(); System.Windows.MessageBox.Show("Employee successfully deleted"); resetGrid(); } } }