private void undeleteToolStripMenuItem_Click(object sender, EventArgs e) { this.lastId = getId("Update"); DBCommands.UnDelete(this.lastId, executeCMD); loadData(""); resetForm(); }
/* * This is the main function of the form. It uses a Select statement that encompasses a searchTerm. * This query allows for Searching all (just "" as the searchTerm) and searching for any name, first or last or both. * We use this loadData function anytime we add,update,or delete records */ private void loadData(string searchTerm) { DBCommands.sql = "SELECT users.id, first_nm, last_nm, job_ttl, work_location, yrs_in_job " + "FROM users JOIN job_details ON job_details.id = users.id WHERE CONCAT(first_nm, ' ', last_nm) ILIKE @searchTerm::varchar AND users.stat_cd = 'A' ORDER BY last_nm"; // The format %str% is postgres language for grabbing anything before and after the search term. // example: "John Snow Movies" if search term was snow, it would still grab "John Snow Movies" string strSearchTerm = string.Format("%{0}%", searchTerm); // We use our cmd method from our DBCommands class again here to setup a new command with parameters // In this case the parameter will be the search term, if there is one // We set create a new DataTable object called dt which we set equal to the dt we return from // the PerformCommand method in the class. DBCommands.cmd = new NpgsqlCommand(DBCommands.sql, DBCommands.con); DBCommands.cmd.Parameters.Clear(); // you will notice the @ sign inside of the sql Select statment above in front of searchTerm. // This is how the AddWithValue() works. It will find searchTerm as the given parameter and set it // to the value we pass in, in this case strSearchTerm DBCommands.cmd.Parameters.AddWithValue("searchTerm", strSearchTerm); DataTable dt = DBCommands.PerformCommand(DBCommands.cmd); // The rest below is just styling our Datagridview from the form DataGridView dgv1 = dbGrid; dgv1.MultiSelect = false; dgv1.AutoGenerateColumns = true; dgv1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dgv1.DataSource = dt; dgv1.Columns[0].HeaderText = "ID"; dgv1.Columns[1].HeaderText = "First_Name"; dgv1.Columns[2].HeaderText = "Last_Name"; dgv1.Columns[3].HeaderText = "Job_Title"; dgv1.Columns[4].HeaderText = "Work_Location"; dgv1.Columns[5].HeaderText = "Years_In_Job"; dgv1.Columns[0].Width = 120; dgv1.Columns[1].Width = 150; dgv1.Columns[2].Width = 150; dgv1.Columns[3].Width = 150; dgv1.Columns[4].Width = 150; dgv1.Columns[5].Width = 125; }
private void saveToolStripMenuItem1_Click(object sender, EventArgs e) { if (MessageBox.Show("Do You Want To Save Current Updates To Selected Employee?", "Save Updates", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { DBCommands.Update(dbGrid, this.id, executeCMD, missingField); loadData(""); resetForm(); } else { MessageBox.Show("Updates Were Not Saved", "Not Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); loadData(""); resetForm(); } }
private void deleteToolStripMenuItem_Click(object sender, EventArgs e) { if (MessageBox.Show("Do You Want To Delete The Selected Employee?", "Delete Employee", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { DBCommands.softDelete(dbGrid, this.id, executeCMD); loadData(""); resetForm(); } else { MessageBox.Show("Employee Was Not Deleted", "Not Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information); loadData(""); resetForm(); } }
/* * PostgreSQL requires us to execute after doing sql queries with a command and parameters * and it gets lengthy to type the whole thing out. * So, instead, it is good practice to create an execute, or in this case executeCMD function * so you just have to call executeCMD instead. This is like committing in other languages like GIT */ private void executeCMD(string mySQL, string param) { DBCommands.cmd = new NpgsqlCommand(mySQL, DBCommands.con); addParameters(param); DBCommands.PerformCommand(DBCommands.cmd); }
private void purgeToolStripMenuItem_Click(object sender, EventArgs e) { DBCommands.Purge(executeCMD); loadData(""); resetForm(); }
private void delBtn_Click(object sender, EventArgs e) { DBCommands.softDelete(dbGrid, this.id, executeCMD); loadData(""); resetForm(); }
private void updateBtn_Click(object sender, EventArgs e) { DBCommands.Update(dbGrid, this.id, executeCMD, missingField); loadData(""); resetForm(); }