private void btUpdate_Click(object sender, EventArgs e)//Used to update an existing record { //validation for the textboxes being updated temp = IV.DateValid(tbDOB.Text); if (temp == false) { MessageBox.Show("Please enter Date Of Birth in format dd/mm/yyyy"); } else { count++; } foreach (var c in gbxBorrowerData.Controls) { if (c is TextBox) { temp = IV.Null(((TextBox)c).Text); if (temp == false) { count++; } } if (c is ComboBox) { temp = IV.Null(((ComboBox)c).Text); if (temp == false) { count++; } } } temp = IV.CharCheck(tbEmail.Text, Convert.ToString('@')); if (temp == true) { count++; } else { MessageBox.Show("Please enter a valid email address"); } temp = IV.CharCheck2(tbNumber.Text, "07"); temp2 = IV.CharCheck2(tbNumber.Text, "0161"); if (temp == false && temp2 == false) { MessageBox.Show("Please enter a valid contact number"); } else { count++; } LengthCheck2(tbFName.Text, 25, temp, ref count); LengthCheck2(tbSurname.Text, 20, temp, ref count); LengthCheck2(tbSex.Text, 6, temp, ref count); LengthCheck1(tbNumber.Text, 11, temp, ref count); LengthCheck2(tbAddress.Text, 40, temp, ref count); LengthCheck2(tbEmail.Text, 25, temp, ref count); if (count == 18)//count = 18 when everything is valid { OleDbConnection Conn = new OleDbConnection(Program.ConnString); Conn.Open(); OleDbCommand Cmd = new OleDbCommand(); Cmd.Connection = Conn; //updates the databse to include what the user has changed Cmd.CommandText = "UPDATE Borrowers"; Cmd.CommandText += " SET Firstname = '" + tbFName.Text + "', Surname = '" + tbSurname.Text + "', "; Cmd.CommandText += " DateOfBirth = '" + tbDOB.Text + "', Sex = '" + tbSex.Text + "', "; Cmd.CommandText += " Address = '" + tbAddress.Text + "', EmailAddress = '" + tbEmail.Text + "', "; Cmd.CommandText += " ContactNumber = '" + tbNumber.Text + "', BorrowerCategory = '" + cbCategory.Text + "', Branch = '" + cbBranch.Text + "'"; Cmd.CommandText += " WHERE BorrowerNumber = '" + BorrowerNumber + "'"; Cmd.ExecuteNonQuery(); Conn.Close(); count = 0;//resets count } else { //MessageBox.Show(count.ToString()); MessageBox.Show("Please make sure that there are no empty fields and everything entered is valid. "); //tells the user to check the inputs count = 0; //resets count } }
private void btAdd_Click(object sender, EventArgs e) { //Validation for all tbs temp = IV.DateValid(tbDOB.Text);//uses the routine found in SampleLibrary to check whether the date of birth entered by the user is valid if (temp == false) { MessageBox.Show("Please enter Date Of Birth in format dd/mm/yyyy");//message telling user the date is invalid and why } else { count++; //count is incremented if the date is valid } foreach (var c in this.Controls) //checks to see whether something has been entered in each textbox { if (c is TextBox) { temp = IV.Null(((TextBox)c).Text); if (temp == false) { count++;//count is incremented if the textbox has something in it } } if (c is ComboBox)//checks to see whether something has been selected for each combobox { temp = IV.Null(((ComboBox)c).Text); if (temp == false) { count++;//count is incremented if the something has been selected for the combobox } } } temp = IV.CharCheck(tbEmail.Text, Convert.ToString('@'));//checks to see whether the email entered is valid using the routine found in SampleLibrary if (temp == true) { count++;//count is incremented if email is valid } else { MessageBox.Show("Please enter a valid email address");//notifies user if email is invalid } //checks to see whether the contact number is valid temp = IV.CharCheck2(tbNumber.Text, "07"); temp2 = IV.CharCheck2(tbNumber.Text, "0161"); if (temp == false && temp2 == false) //if the number doesn't start with '07' or '0161' then it is invalid { MessageBox.Show("Please enter a valid contact number"); //tells user number is invalid } else { count++;// count is incremented if valid } //checks whether everything entered is the correct length barring what is in the combo boxes as all the options they can choose from are correct //count is incremented each time the lenght of an input is correct LengthCheck2(tbFName.Text, 25, temp, ref count); LengthCheck2(tbSurname.Text, 20, temp, ref count); LengthCheck2(tbSex.Text, 6, temp, ref count); LengthCheck1(tbNumber.Text, 11, temp, ref count); LengthCheck2(tbAddress.Text, 40, temp, ref count); LengthCheck2(tbEmail.Text, 25, temp, ref count); if (count == 19)//count = 19 if all inputs are valid { //MessageBox.Show(count.ToString()); OleDbConnection Conn = new OleDbConnection(Program.ConnString); Conn.Open(); OleDbCommand Cmd = new OleDbCommand(); Cmd.Connection = Conn; //adds the new borrower to the database string SQL = "INSERT INTO Borrowers "; SQL += " VALUES('" + Convert.ToString(tbBorrowerNumber.Text) + "', '" + tbFName.Text + "',' " + tbSurname.Text + "', #" + tbDOB.Text + "#, '" + tbSex.Text + "', '" + tbAddress.Text + "', '" + tbEmail.Text + "','" + Convert.ToString(tbNumber.Text) + "','"; SQL += cbCategory.SelectedItem + "', '" + cbBranch.SelectedItem + "')"; Cmd.CommandText = SQL; Cmd.ExecuteNonQuery(); Conn.Close(); count = 0; //resets count foreach (var c in this.Controls) //clears all text boxes and combo boxes { if (c is TextBox && c != tbBorrowerNumber) { ((TextBox)c).Text = String.Empty; } if (c is ComboBox) { ((ComboBox)c).Text = String.Empty; } } ID();//generates a new BorrowerNumber after all text boxes have been cleared } else//if there are any invalid inputs { //MessageBox.Show(count.ToString()); MessageBox.Show("Please make sure that there are no empty fields and everything entered is valid. "); //displays an error message count = 0; //restes count } }