private void btnDeleteCustomer_Click(object sender, EventArgs e) { if (dgvCustomer.SelectedRows.Count == 0) //validation that there is at least one row selected { MessageBox.Show("Please select a customer"); //if above statement is true, shows message box and end function execcution. Row is usually autoselected from the beginning but this is just a precaution return; } DialogResult = MessageBox.Show("Are you sure you wish to delete the selected customer?", "Delete?", MessageBoxButtons.YesNo); //Dialogresult function to allow for check before deleting selected record if (DialogResult == DialogResult.Yes) { foreach (DataRow row in dtCustomer.Rows) //if statement above is true, loops through each row of table { if (dgvCustomer.SelectedCells[0].Value.ToString() == row["custid"].ToString()) //check to see if selected dgvCustomer userid is the same as the table userid { using (SQLiteCommand cmd = conn.CreateCommand()) //if above is true, sets SQL query based on custid of table row in loop, opens connection, runs the query, closes the connection { cmd.CommandText = @"DELETE FROM customer Where custid = @custID"; cmd.Parameters.AddWithValue("custID", row["custid"].ToString()); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } } Form form = new AllCustomers(); //refresh() not working, using new form load form.Show(); this.Dispose(); } else //if the statement is false, return without deleting { return; } }
private void btnCustomers_Click(object sender, EventArgs e) //lines 81 - 109 use buttons to open specific forms { Form NewForm = new AllCustomers(); NewForm.Show(); }
private void btnSubmitCustDetails_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtCustTitle.Text) || string.IsNullOrWhiteSpace(txtCustFirstName.Text) || string.IsNullOrWhiteSpace(txtCustLastName.Text) || string.IsNullOrWhiteSpace(mtxtCustDOB.Text) || string.IsNullOrWhiteSpace(txtCustNICode.Text) || string.IsNullOrWhiteSpace(txtCustEmailAddress.Text) || string.IsNullOrWhiteSpace(txtCustPassword.Text) || string.IsNullOrWhiteSpace(txtCustAllowance.Text)) //lines 87 - 88 validates that there is data in the textboxes { MessageBox.Show("Please fill all the fields with correct information"); return; } if (DateTime.TryParse(mtxtCustDOB.Text, out DateTime format) != true) //validates the masked textbox data is in the correct data format and is an acceptable data { MessageBox.Show("Please enter a date in the correct format (DD/MM/YYYY)"); return; } if (int.TryParse(txtCustAllowance.Text, out int result) == false) //validates the allowance field in a number { MessageBox.Show("Please only use numbers in the 'Allowance' field"); return; } if (loadCommand == "add") { using (SQLiteCommand cmd = conn.CreateCommand()) //if line 104 is true, runs command to add a customer by opening a connection, running the query and closing the connection { cmd.CommandText = @"Insert into customer(title, firstname, lastname, dob, nicode, email, password, allowance) values (@title, @firstName, @lastName, @dob, @niCode, @email, @password, @allowance)"; cmd.Parameters.AddWithValue("title", txtCustTitle.Text); cmd.Parameters.AddWithValue("firstName", txtCustFirstName.Text); cmd.Parameters.AddWithValue("lastName", txtCustLastName.Text); cmd.Parameters.AddWithValue("dob", mtxtCustDOB.Text); cmd.Parameters.AddWithValue("niCode", txtCustNICode.Text); cmd.Parameters.AddWithValue("email", txtCustEmailAddress.Text); cmd.Parameters.AddWithValue("password", txtCustPassword.Text); cmd.Parameters.AddWithValue("allowance", txtCustAllowance.Text); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } DialogResult = MessageBox.Show("Customer details have been added", "Customer Details", MessageBoxButtons.OK); //opens message and then, on DialogResult, shows allCustomer form and disposes this form if (DialogResult == DialogResult.OK) { allCustomers.Show(); this.Dispose(); } } else if (loadCommand == "update") { using (SQLiteCommand cmd = conn.CreateCommand()) //if line 130 is true, runs command to updates a customer by opening a connection, running the query and closing the connection { cmd.CommandText = @"UPDATE customer Set title = @title, firstname = @firstName, lastname = @lastName, dob = @dob, nicode = @niCode, email = @email, password = @password, allowance = @allowance Where custid = @custID"; cmd.Parameters.AddWithValue("title", txtCustTitle.Text); cmd.Parameters.AddWithValue("firstName", txtCustFirstName.Text); cmd.Parameters.AddWithValue("lastName", txtCustLastName.Text); cmd.Parameters.AddWithValue("dob", mtxtCustDOB.Text); cmd.Parameters.AddWithValue("niCode", txtCustNICode.Text); cmd.Parameters.AddWithValue("email", txtCustEmailAddress.Text); cmd.Parameters.AddWithValue("password", txtCustPassword.Text); cmd.Parameters.AddWithValue("allowance", txtCustAllowance.Text); cmd.Parameters.AddWithValue("custID", txtCustID.Text); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } DialogResult = MessageBox.Show("Customer details have been updated", "Customer Details", MessageBoxButtons.OK); //opens message and then, on DialogResult, shows allCustomer form and disposes this form if (DialogResult == DialogResult.OK) { allCustomers.Show(); this.Dispose(); } } }