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(); } } } } }
/// <summary> /// Reads the CSV from filepath from the textbox, then check if account exist. If it doesnm't, /// add to DB /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uploadBtn_Click(object sender, EventArgs e) { string[] lines = File.ReadAllLines(textBox3.Text); for (int i = 1; i < lines.Count(); i++) { using (var context = new Session4Entities()) { var values = lines[i].Split(','); var id = values[0].Trim(); var checkIfExist = (from x in context.Users where x.userId == id select x).FirstOrDefault(); if (checkIfExist == null) { context.Users.Add(new User() { userId = values[0].Trim(), skillIdFK = Int32.Parse(values[1]), passwd = values[2].Trim(), name = values[3].Trim(), userTypeIdFK = Int32.Parse(values[4]) }); context.SaveChanges(); } } } MessageBox.Show("Users added!", "Successful account creation(s)", MessageBoxButtons.OK, MessageBoxIcon.Information); }
/// <summary> /// Triggered when Remove button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void removeBtn_Click(object sender, EventArgs e) { //Check if there is even any rows selected if (dataGridView1.CurrentRow == null) { MessageBox.Show("Please select a record to delete!"); } else { ///Removes training from DB, then adds the mopdule back to Combo box using (var context = new Session4Entities()) { var moduleID = Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].Value); var getTrainingsCheck = (from x in context.Assign_Training where x.moduleIdFK == moduleID select x).FirstOrDefault(); if (getTrainingsCheck != null) { var getTrainings = (from x in context.Assign_Training where x.moduleIdFK == moduleID select x).ToList(); foreach (var item in getTrainings) { context.Assign_Training.Remove(item); context.SaveChanges(); } } moduleBox.Items.Add(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value); dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index); } } }
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 update_button_Click(object sender, EventArgs e) { using (var db = new Session4Entities()) { foreach (var item in dgvlist) { var assign = (from a in db.Assign_Training where a.trainingId == item.ID select a).First(); assign.progress = item.Progress; } db.SaveChanges(); } }
/// <summary> /// Triggered when the Add button is clicked. Checks if training is assigned already in DB, else assign training /// to all of the category's and skill's participants /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void assignBtn_Click(object sender, EventArgs e) { using (var context = new Session4Entities()) { foreach (DataGridViewRow rows in dataGridView1.Rows) { var skillName = rows.Cells[0].Value.ToString(); var getSkillID = (from x in context.Skills where x.skillName == skillName select x.skillId).First(); var categoryName = rows.Cells[1].Value.ToString(); var getCategoryID = (from x in context.User_Type where x.userTypeName == categoryName select x.userTypeId).First(); var getAllID = (from x in context.Users where x.User_Type.userTypeName == categoryName && x.Skill.skillName == skillName select x.userId).ToList(); var ID = Convert.ToInt32(rows.Cells[3].Value); var checkIfIDExist = (from x in context.Assign_Training where x.moduleIdFK == ID select x).FirstOrDefault(); if (checkIfIDExist == null) { foreach (var item in getAllID) { context.Assign_Training.Add(new Assign_Training() { startDate = Convert.ToDateTime(rows.Cells[4].Value), progress = 0, userIdFK = item, moduleIdFK = Convert.ToInt32(rows.Cells[3].Value) }); context.SaveChanges(); } } else { continue; } } } this.Hide(); (new AdminMain(_userID)).ShowDialog(); this.Close(); }
private void button2_Click(object sender, EventArgs e) { foreach (DataGridViewRow dr in dataGridView1.Rows) { if (dr.Cells["ID"].Value != null) { if (dr.Cells["Progress (%)"].Value.ToString().Contains(".")) { MessageBox.Show("Progress cannot be decimals!"); return; } var ID = int.Parse(dr.Cells["ID"].Value.ToString()); var progress = dr.Cells["Progress (%)"].Value.ToString(); if (int.Parse(progress) >= 0 && int.Parse(progress) <= 100) { using (var db = new Session4Entities()) { var lol = db.Assign_Training.Where(x => x.trainingId == ID).FirstOrDefault(); if (lol.progress > int.Parse(progress)) { MessageBox.Show("Can only increase not decrease progress!"); return; } lol.progress = int.Parse(progress); try { db.SaveChanges(); } catch (Exception es) { MessageBox.Show(es.ToString()); } MessageBox.Show("Success!"); filter(); } } else { MessageBox.Show("Error!"); } } } }
private void button2_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Text files | *.csv"; // file types, that will be allowed to upload dialog.Multiselect = false; // allow/deny user to upload more than one file at a time if (dialog.ShowDialog() == DialogResult.OK) // if user clicked OK { textBox2.Text = dialog.FileName; // get name of file try { using (var db = new Session4Entities()) { string[] Lines = File.ReadAllLines(dialog.FileName); string[] Fields = Lines[0].Split(new char[] { ',' }); //The system should then append the //information from this CSV file to the //database for (int i = 1; i < Lines.GetLength(0); i++) { Fields = Lines[i].Split(new char[] { ',' }); User user = new User(); user.userId = Fields[0].Trim(); user.skillIdFK = int.Parse(Fields[1]); user.passwd = Fields[2].Trim(); user.name = Fields[3].Trim(); user.userTypeIdFK = int.Parse(Fields[4]); db.Users.Add(user); } db.SaveChanges(); MessageBox.Show("Successfully added to DB!"); } } catch (Exception es) { MessageBox.Show(es.ToString()); } } }
private void csv_open_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog() { Filter = "csv files (*.csv)|*.csv", FilterIndex = 1, CheckFileExists = true, CheckPathExists = true }; if (ofd.ShowDialog() == DialogResult.OK) { textBox3.Text = ofd.FileName; var list = File.ReadAllLines(ofd.FileName) .Skip(1) .Select(a => a.Split(',')) .Select(a => new User() { userId = a[0].Trim(), skillIdFK = int.Parse(a[1]), passwd = a[2].Trim(), name = a[3].Trim(), userTypeIdFK = int.Parse(a[4]) } ).ToList(); using (var db = new Session4Entities()) { foreach (var item in list) { db.Users.Add(item); } db.SaveChanges(); } } else { MessageBox.Show("No file was selected, Aborting!"); } }
/// <summary> /// Triggered when Update Button is clicked. When user's response to MessageBox is Yes, /// then update all progress to DB /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void updateBtn_Click(object sender, EventArgs e) { var dl = MessageBox.Show("Are you sure you want to update progress?", "Update Progress", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dl == DialogResult.Yes) { using (var context = new Session4Entities()) { foreach (DataGridViewRow item in dataGridView1.Rows) { var getID = Convert.ToInt32(item.Cells[5].Value); var getTraining = (from x in context.Assign_Training where x.trainingId == getID select x).First(); getTraining.progress = Convert.ToInt32(item.Cells[4].Value); context.SaveChanges(); } } MessageBox.Show("Update successful!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information); } }