private void button3_Click(object sender, EventArgs e) { using (var db = new Session4Entities()) { //First it goes through each item in the list foreach (var item in strings) { //Gets the skill name and user type var skill = item[0]; var userType = item[1]; //Goes through each user and finds the users with the skill and user type var user = db.Users.Where(x => x.Skill.skillName == skill && x.User_Type.userTypeName == userType).ToList(); //Goes through each user foreach (var items in user) { //Creates a new DBO Assign_Training assign_Training = new Assign_Training(); //Gets the Module Name var modName = item[2]; //Goes to the traning module table and finds the module with the right module name, skill and user type var trainingMod = db.Training_Module.Where(x => x.moduleName == modName && x.Skill.skillName == skill && x.User_Type.userTypeName == userType).FirstOrDefault(); //Checks if it exists if (trainingMod != null) { assign_Training.moduleIdFK = trainingMod.moduleId; assign_Training.userIdFK = items.userId; assign_Training.startDate = dateTimePicker1.Value; assign_Training.progress = 0; db.Assign_Training.Add(assign_Training); } } } try { db.SaveChanges(); MessageBox.Show("Success!"); this.Hide(); AdminMainMenu adminMainMenu = new AdminMainMenu(users); adminMainMenu.Show(); } catch (Exception es) { MessageBox.Show(es.ToString()); } } }
private void assign_button_Click(object sender, EventArgs e) { using (var db = new Session4Entities()) { for (int i = 0; i < dgvlist.Count; i++) { var currentmodule = dgvlist[i].TrainingModule; var id = (from a in db.Training_Module where a.moduleName == currentmodule select a.moduleId).First(); var skill = dgvlist[i].Skill; var persontype = dgvlist[i].TraineeCategory; var currentskill = (from a in db.Skills where a.skillName == skill select a.skillId).First(); var usertype = (from u in db.User_Type where u.userTypeName == persontype select u.userTypeId).First(); var people = (from b in db.Users where b.skillIdFK == currentskill where b.userTypeIdFK == usertype select b.userId).ToList(); foreach (var item in people) { try { var ast = new Assign_Training() { moduleIdFK = id, progress = 0, startDate = DateTime.Now, trainingId = (from a in db.Assign_Training orderby a.trainingId descending select a.trainingId).First() + 1, userIdFK = item }; db.Assign_Training.Add(ast); db.SaveChanges(); } catch { var ast = new Assign_Training() { moduleIdFK = id, progress = 0, startDate = DateTime.Now, trainingId = 1, userIdFK = item }; db.Assign_Training.Add(ast); db.SaveChanges(); } } } } }