/// <summary>
        /// Populate comboBoxModule depending on the selected project
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxModule.Items.Clear();
            List <Module> modules = new List <Module>();

            if (DbConnector.OpenSQLConnection()) // Open connection to the database
            {
                // Connection opened
                ProjectDataAccess project = new ProjectDataAccess();
                int projectId             = project.FindProjectByProjectName(comboBoxProject.Text);
                ModuleDataAccess module   = new ModuleDataAccess();
                modules = module.FindModuleByProject(Convert.ToInt32(projectId));
            }
            else
            {
                // Connection could not be opened
                MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (modules != null)
            {
                // Populate comboBoxes with data
                foreach (Module m in modules)
                {
                    comboBoxModule.Items.Add(m.ModuleName);
                }
            }
        }
        /// <summary>
        /// Method for populating the DataDridView
        /// </summary>
        public void populateDataGridView(int projectId)
        {
            this.projectId = projectId;
            if (DbConnector.OpenSQLConnection()) // Open connection to the database
            {
                // Connection opened
                ModuleDataAccess module = new ModuleDataAccess();
                dgvModule.AutoGenerateColumns = false; // To only show the columns needed
                dgvModule.DataSource          = module.GetAllModules(projectId);

                ProjectDataAccess project = new ProjectDataAccess();
                projectName = project.FindProject(projectId).ProjectName;
                for (int i = 0; i < dgvModule.RowCount; i++)
                {
                    dgvModule.Rows[i].Cells[2].Value = projectName;
                }
                txtSearchModules.Text = "";
                Modules_Tab_Child.getInstance().clearModuleText();
            }
            else
            {
                // Connection could not be opened
                MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            DbConnector.CloseSQLConnection(); // Close connection to the database
        }
        /// <summary>
        /// Populate the datagridview with search result
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearchModules_Click(object sender, EventArgs e)
        {
            if (DbConnector.OpenSQLConnection()) // Open connection to the database
            {
                // Connection opened
                ModuleDataAccess module = new ModuleDataAccess();
                dgvModule.AutoGenerateColumns = false; // To only show the columns needed
                dgvModule.DataSource          = module.SearchModules(txtSearchModules.Text.Trim());

                if (dgvModule.RowCount != 0)
                {
                    ProjectDataAccess project = new ProjectDataAccess();
                    var result = project.FindProject(Convert.ToInt32(dgvModule.Rows[0].Cells[1].Value)).ProjectName;
                    for (int i = 0; i < dgvModule.RowCount; i++)
                    {
                        dgvModule.Rows[i].Cells[2].Value = result;
                    }
                }
            }
            else
            {
                // Connection could not be opened
                MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            DbConnector.CloseSQLConnection(); // Close connection to the database
        }
        /// <summary>
        /// Handles DataGridView button click events
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvModule_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var Column = ((DataGridView)sender).Columns[e.ColumnIndex];

            if (Column is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                if (Column.Index == 6)                   // Update button
                {
                    if (DbConnector.OpenSQLConnection()) // Open connection to the database
                    {
                        // Connection opened
                        ModuleDataAccess module = new ModuleDataAccess();
                        var result = module.FindModule(Convert.ToInt32(dgvModule.Rows[e.RowIndex].Cells[0].Value));
                        if (result != null)
                        {
                            Modules_Tab_Child.getInstance().BringToFront();
                            Modules_Tab_Child.getInstance().changeView(result, projectId, projectName);
                        }
                    }
                    else
                    {
                        // Connection could not be opened
                        MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    DbConnector.CloseSQLConnection(); // Close connection to the database
                }
                else if (Column.Index == 7)           // Delete button
                {
                    if (MessageBox.Show("Are you sure you want to delete this record?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (DbConnector.OpenSQLConnection()) // Open connection to the database
                        {
                            // Connection opened
                            ModuleDataAccess module = new ModuleDataAccess();
                            if (module.DeleteModule(Convert.ToInt32(dgvModule.Rows[e.RowIndex].Cells[0].Value)))
                            {
                                // Record deleted successfully
                                MessageBox.Show("Record has been deleted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                populateDataGridView(Convert.ToInt32(dgvModule.Rows[e.RowIndex].Cells[1].Value));
                            }
                            else
                            {
                                // Record was not deleted
                                MessageBox.Show("The record could not be deleted", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        else
                        {
                            // Connection could not be opened
                            MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                        DbConnector.CloseSQLConnection(); // Close connection to the database
                    }
                }
            }
        }
        /// <summary>
        /// Handle the function of adding a module to the database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnModuleSave_Click(object sender, EventArgs e)
        {
            string moduleName     = txtModuleName.Text.Trim();
            string moduleFunction = txtModuleFunction.Text.Trim();
            string addedBy        = txtAddedBy.Text.Trim();

            if (moduleName != "" && moduleFunction != "" && addedBy != "") // Check if required fields are filled
            {
                if (DbConnector.OpenSQLConnection())                       // Open connection to the database
                {
                    // Connection opened
                    ModuleDataAccess module = new ModuleDataAccess();

                    if (btnModuleSave.Text == "Save")
                    {
                        if (module.InsertModule(projectId, moduleName, moduleFunction, addedBy))
                        {
                            // Record inserted successfully
                            MessageBox.Show("Record has been inserted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Modules_Tab.getInstance().BringToFront();
                            Modules_Tab.getInstance().populateDataGridView(projectId);
                        }
                        else
                        {
                            // Record was not inserted
                            MessageBox.Show("The record could not be saved", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else if (btnModuleSave.Text == "Update")
                    {
                        if (module.UpdateModule(moduleId, moduleName, moduleFunction, addedBy))
                        {
                            // Record updated successfully
                            MessageBox.Show("Record has been updated successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Modules_Tab.getInstance().BringToFront();
                            Modules_Tab.getInstance().populateDataGridView(projectId);
                        }
                        else
                        {
                            // Record was not updated
                            MessageBox.Show("The record could not be updated", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                else
                {
                    // Connection could not be opened
                    MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                // Fields not filled correctly
                MessageBox.Show("Please fill all the required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }