private void BtnActivities_Click(object sender, EventArgs e) { if (listViewTasks.FocusedItem != null) { taskId = listViewTasks.FocusedItem.SubItems[0].Text; ActivitiesForm activitiesForm = new ActivitiesForm(); activitiesForm.Show(); } else { MessageBox.Show("Please select one item", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void BtnCreate_Click(object sender, EventArgs e) { // Get values from components string activityName = txtActivityName.Text; string priority = comboBoxPriority.SelectedItem.ToString(); string description = txtDescription.Text; string plannedStartDate = datePlannedStart.Text; string plannedEndDate = datePlannedEnd.Text; string actualStartDate = dateActualStart.Text; string actualEndDate = dateActualEnd.Text; string employeeName = comboBoxEmployee.SelectedItem.ToString(); // Db connection string server = "server = localhost;"; string database = "database = team_management;"; string user = "******"; string pass = "******"; string connetionString = server + database + user + pass; string create_activity = "INSERT INTO activity(activity_name, task_id, priority, description, " + "planned_start_date, planned_end_date, actual_start_time, actual_end_time) VALUES(@activity_name, " + "@task_id, @priority, @description, @planned_start_date, @planned_end_date, @actual_start_time, " + "@actual_end_time)"; string create_assigned = "INSERT INTO assigned(activity_id, role_id, employee_id) " + "VALUES(@activity_id, @role_id, @employee_id)"; MySqlConnection conn = null; MySqlDataReader reader = null; try { conn = new MySqlConnection(connetionString); conn.Open(); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandText = create_activity; cmd.Prepare(); cmd.Parameters.AddWithValue("@activity_name", activityName); cmd.Parameters.AddWithValue("@task_id", TasksForm.taskId); cmd.Parameters.AddWithValue("@priority", int.Parse(priority)); cmd.Parameters.AddWithValue("@description", description); cmd.Parameters.AddWithValue("@planned_start_date", Convert.ToDateTime(plannedStartDate)); cmd.Parameters.AddWithValue("@planned_end_date", Convert.ToDateTime(plannedEndDate)); cmd.Parameters.AddWithValue("@actual_start_time", Convert.ToDateTime(actualStartDate)); cmd.Parameters.AddWithValue("@actual_end_time", Convert.ToDateTime(actualEndDate)); cmd.ExecuteNonQuery(); // get last inserted activities id long activityId = cmd.LastInsertedId; cmd.CommandText = create_assigned; cmd.Prepare(); cmd.Parameters.AddWithValue("@activity_id", activityId); cmd.Parameters.AddWithValue("@role_id", employee_roleId[employeeName]); cmd.Parameters.AddWithValue("@employee_id", employee_id[employeeName]); cmd.ExecuteNonQuery(); MessageBox.Show("Created successfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); ActivitiesForm activitiesForm = new ActivitiesForm(); activitiesForm.Show(); this.Close(); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.ToString()); } finally { if (reader != null) { reader.Close(); } if (conn != null) { conn.Close(); } } }