Ejemplo n.º 1
0
        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)
            { 
            
            }
        }
Ejemplo n.º 2
0
        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();
                }
            }
        }
Ejemplo n.º 3
0
        private void btnDel_Copy_Click(object sender, RoutedEventArgs e)
        {
            
            object item = dg1.SelectedItem;
            String ID = (dg1.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
            System.Windows.MessageBox.Show(ID);
            byte[] imageArr;
            using(var ctx=new MyContext()) 
            {
                var pic = ctx.Employees.Find(ID);
                imageArr = pic.Photo;
            }

            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.CreateOptions = BitmapCreateOptions.None;
            bi.CacheOption = BitmapCacheOption.Default;
            bi.StreamSource = new MemoryStream(imageArr);
            bi.EndInit();
            img1.Source = bi;

            //Image img = new Image();  //Image control of wpf

            //img.Source = bi;

        }
Ejemplo n.º 4
0
        private void resetGrid()
        {
            dg1.IsReadOnly = true;
            
            try
            {
                List<Employee> list = new List<Employee>();
                using (var ctx = new MyContext())
                {
                    var Emp = from em in ctx.Employees
                              select em;
                    foreach (var item in Emp)
                    {
                        list.Add(item);
                    }
                    dg1.ItemsSource = ctx.Employees.ToList();

                }
            }
            catch (Exception ex)
            {

            }
        }