// Add button to create customer entries // Does not allow empty/duplicate names and email private void CreateButton(object sender, EventArgs e) { //open database connection CustomerDB.Open(); //create sql command string variable SqlCommand command = CustomerDB.CreateCommand(); command.CommandType = CommandType.Text; if (string.IsNullOrWhiteSpace(NameText.Text) || string.IsNullOrWhiteSpace(NumberText.Text) || string.IsNullOrWhiteSpace(EmailText.Text)) { MessageBox.Show("Please enter a name, phone number, and email."); CustomerDB.Close(); } else { Account account = new Account(); command.CommandText = "Select CustomerName from Customer where CustomerName = '" + NameText.Text + "'"; account.Name = (string)command.ExecuteScalar(); command.CommandText = "Select Email from Customer where Email = '" + EmailText.Text + "'"; account.Email = (string)command.ExecuteScalar(); if (NameText.Text == account.Name) { MessageBox.Show("This customer already exists."); CustomerDB.Close(); } else if (EmailText.Text == account.Email) { MessageBox.Show("This email has already been used."); CustomerDB.Close(); } else { //insert 'new' Customer data entries command.CommandText = "insert into Customer values('" + NameText.Text + "','" + NumberText.Text + "','" + EmailText.Text + "')"; command.ExecuteNonQuery(); CustomerDB.Close(); live_update(); MessageBox.Show("Customer data added."); } } }
// Delete button - only deletes by CustomerID private void DeleteButton(object sender, EventArgs e) { //open database connection CustomerDB.Open(); //create sql command string variable SqlCommand command = CustomerDB.CreateCommand(); command.CommandType = CommandType.Text; Account account = new Account(); command.CommandText = "Select CustomerID from Customer where CustomerID = '" + IDText.Text + "'"; account.ID = command.ExecuteScalar().ToString(); if (string.IsNullOrWhiteSpace(IDText.Text)) { MessageBox.Show("Please enter a CustomerID to delete an entry."); CustomerDB.Close(); } else if (string.IsNullOrWhiteSpace(account.ID)) { MessageBox.Show("Customer does not exist. Delete not performed."); CustomerDB.Close(); } else { //delete Customer data by CustomerID command.CommandText = "delete from Customer where CustomerID = '" + IDText.Text + "'"; command.ExecuteNonQuery(); CustomerDB.Close(); live_update(); MessageBox.Show("Customer data deleted."); } }