private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                User_Role role = new User_Role();

                role.User_Role_Description = txtdes.Text;
                role.Access_Level_ID       = Convert.ToInt32(cmbAccess.Text);
                db.User_Role.Add(role);
                db.SaveChanges();
                MessageBox.Show("Role added successfully");
            }
            catch (Exception i)
            {
                MessageBox.Show(i.ToString());
            }
        }
Пример #2
0
        private void btnSaveCombo_Click(object sender, EventArgs e)
        {
            User             NewU = new User();
            Employee         NewE = new Employee();
            string           ges  = cmbGender.Text;
            Employee_Gender_ gend = db.Employee_Gender_.FirstOrDefault(c => c.Gender_Description == ges);



            int gender = cmbGender.SelectedIndex;

            NewE.Employee_Name            = txtName.Text;
            NewE.Employee_Surname         = txtSurname.Text;
            NewE.Employee_Identity_Number = txtIdNumber.Text;
            NewE.Gender_ID                  = gend.Gender_ID;
            NewE.Adress                     = txtAddress.Text;
            NewE.Contact_Number             = txtEmpContact.Text;
            NewE.Email_Adress               = txtEmail.Text;
            NewE.Next_Of_Kin_Name           = txtNextOfKin.Text;
            NewE.Next_Of_Kin_Contact_Number = txtNKC.Text;
            NewE.User_ID                    = NewU.User_ID;

            //Generate Login Details


            if (txtName.Text.Length >= 3)
            {
                string userprif = txtName.Text.Substring(0, 3);
                Random rd       = new Random();
                int    number   = rd.Next(100, 999);
                string username = userprif + number.ToString();
                NewU.UserName = username;

                int password = rd.Next(10000, 99999);

                NewU.User_Password = password.ToString();

                string    r    = cmbRole.Text;
                User_Role role = db.User_Role.FirstOrDefault(c => c.User_Role_Description == r);

                NewU.User_Role_ID = role.User_Role_ID;



                //send email to user address with username and password


                SendEmail(txtEmail.Text, txtName.Text, username, password.ToString());
            }
            else
            {
                MessageBox.Show("Error: Name too short");
                return;
            }
            db.Employees.Add(NewE);
            db.Users.Add(NewU);
            db.SaveChanges();
            Globals.imageIdEmployee = NewE.Employee_ID;
            Globals.emNameImage     = NewE.Employee_Name;
            MessageBox.Show("Employee added successfully,employee Login details has been sent to their email address,Press ok and add picture");
            btnPictureAdd.Visible = true;
            btnPictureAdd.Enabled = true;
        }