Example #1
0
        private void button3_Click(object sender, EventArgs e)
        {
            //update
            string Name      = textBox2.Text.ToString();
            string GenderTxt = cmbGender.Text.ToString();
            Gender gender    = Gender.female;

            if (GenderTxt.ToLower() == "male")
            {
                gender = Gender.male;
            }
            int    Age      = int.Parse(textBox4.Text);
            string Email    = textBox5.Text.ToString();
            string Password = textBox6.Text.ToString();
            string Phone    = textBox7.Text.ToString();

            if (textBox7.Text.Length > 11)
            {
                MessageBox.Show("Please Entre valid  Phone");
            }
            bool   IsAdmin    = checkBox1.Checked;
            string NationalID = textBox9.Text;

            if (textBox9.Text.Length > 14)
            {
                MessageBox.Show("Please Entre valid NationalID");
            }
            //read all Data
            Employee D_Emp = (from s in Program._dbContext.Employees
                              where s.ID == ID
                              select s).FirstOrDefault();



            //Update Data

            D_Emp.Name       = Name;
            D_Emp.Gender     = gender;
            D_Emp.Age        = Age;
            D_Emp.Email      = Email;
            D_Emp.Password   = Password;
            D_Emp.Phone      = Phone;
            D_Emp.IsAdmin    = IsAdmin;
            D_Emp.NationalID = NationalID;

            Program._dbContext.SaveChanges();

            //show message

            MessageBox.Show("Updated Success ");

            textBox2.Text     = "";
            cmbGender.Text    = "";
            textBox4.Text     = "";
            textBox5.Text     = "";
            textBox6.Text     = "";
            textBox7.Text     = "";
            checkBox1.Checked = false;
            textBox9.Text     = "";

            // clear datagridview and refresh
            dataGridView1.Rows.Clear();
            dataGridView1.Refresh();

            //read all Data

            var Emp = (from s in Program._dbContext.Employees
                       select s).ToList();


            foreach (var item in Emp)
            {
                dataGridView1.Rows.Add(new String[] { item.ID.ToString(), item.Name, item.Gender.ToString(), item.Age.ToString(), item.Email, item.Password, item.Phone, item.IsAdmin.ToString(), item.NationalID.ToString() });
            }
        }
Example #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            Program._dbContext = new Data_Context();
            Database.SetInitializer <Data_Context>(new MigrateDatabaseToLatestVersion <Data_Context, Migrations.Configuration>());

            string Name      = textBox2.Text.ToString();
            string Gendertxt = cmbGender.Text.ToString();
            Gender gender    = Gender.female;

            if (Gendertxt.ToLower() == "male")
            {
                gender = Gender.male;
            }

            int    Age        = int.Parse(textBox4.Text);
            string Email      = textBox5.Text.ToString();
            string Password   = textBox6.Text.ToString();
            string Phone      = textBox7.Text.ToString();
            bool   IsAdmin    = checkBox1.Checked;
            string NationalID = textBox9.Text;



            Employee emp = new Employee()
            {
                Name       = Name,
                Gender     = gender,
                Age        = Age,
                Email      = Email,
                Password   = Password,
                Phone      = Phone,
                IsAdmin    = IsAdmin,
                NationalID = NationalID
            };


            Program._dbContext.Employees.Add(emp);
            Program._dbContext.SaveChanges();

            //show message

            MessageBox.Show("Added Success ");


            //Clear Text

            textBox2.Text     = "";
            cmbGender.Text    = "";
            textBox4.Text     = "";
            textBox5.Text     = "";
            textBox6.Text     = "";
            textBox7.Text     = "";
            checkBox1.Checked = false;
            textBox9.Text     = "";


            // clear datagridview and refresh
            dataGridView1.Rows.Clear();
            dataGridView1.Refresh();


            //read all Data

            var Emp = (from s in Program._dbContext.Employees
                       select s).ToList();



            foreach (var item in Emp)
            {
                dataGridView1.Rows.Add(new String[] { item.ID.ToString(), item.Name, item.Gender.ToString(), item.Age.ToString(), item.Email, item.Password, item.Phone, item.IsAdmin.ToString(), item.NationalID.ToString() });
            }
        }