private void gamesButton_Click(object sender, EventArgs e) { this.Hide(); GamesList gamesListForm = new GamesList(); gamesListForm.ShowDialog(); }
//Back button returns to GamesList private void backButton_Click(object sender, EventArgs e) { this.Hide(); GamesList list = new GamesList(); list.ShowDialog(); }
//Submit Button updates all new information in the database (even if unchanged) private void submitButton_Click(object sender, EventArgs e) { //Set Game information from form string newTitle = titleTextBox.Text; string newRating = ratingComboBox.Text; string newStatus = statusComboBox.Text; string newGenres = ""; List <string> genresList = new List <string>(); for (int i = 0; i < genresCheckedListBox.Items.Count; i++) { if (genresCheckedListBox.GetItemChecked(i)) { genresList.Add((string)genresCheckedListBox.Items[i]); } } for (int i = 0; i < genresList.Count; i++) { newGenres += genresList[i] + " "; } string newReview = reviewRichTextBox.Text; string newLocation = locationLabel.Text; //Update Game try { string connectionInfo = "datasource = 127.0.0.1; port = 3306; username = root; password = password"; MySqlConnection connect = new MySqlConnection(connectionInfo); string mySelectQuery = "use mydb; update games set Title = @0, Rating = @1, Status = @2, Genres = @3, Review = @4, Location = @5 where Title = '" + originalTitle + "';"; connect.Open(); MySqlCommand command = new MySqlCommand(mySelectQuery, connect); command.Parameters.AddWithValue("@0", newTitle); command.Parameters.AddWithValue("@1", newRating); command.Parameters.AddWithValue("@2", newStatus); command.Parameters.AddWithValue("@3", newGenres); command.Parameters.AddWithValue("@4", newReview); command.Parameters.AddWithValue("@5", newLocation); command.Prepare(); command.ExecuteNonQuery(); connect.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } this.Hide(); GamesList gamesListForm = new GamesList(); gamesListForm.ShowDialog(); }
//Delete Button. Removes game from database by matching title. Make sure AddGame form blocks adding games with matching titles. private void deleteButton_Click(object sender, EventArgs e) { //Confirm Delete var confirmDelete = MessageBox.Show("Are you sure you want to delete?", "Confirm Delete", MessageBoxButtons.YesNo); //If yes, delete from database then return to GamesList if (confirmDelete == DialogResult.Yes) { string title = titleBox.Text; try { string connectionInfo = "datasource = 127.0.0.1; port = 3306; username = root; password = password"; MySqlConnection connect = new MySqlConnection(connectionInfo); string mySelectQuery = "use mydb; delete from games where title = '" + title + "';"; connect.Open(); MySqlCommand myCommand = new MySqlCommand(mySelectQuery, connect); myCommand.ExecuteNonQuery(); connect.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } this.Hide(); GamesList gamesListForm = new GamesList(); gamesListForm.ShowDialog(); } //If no, do nothing else { //Nothing } }
//Submit Button adds the game to the database. Title is made to be unique in the database (Duplicate entry popup, returns to GamesList) private void submitButton_Click(object sender, EventArgs e) { //Set Game information from form string title = titleTextBox.Text; string rating = ratingComboBox.Text; string status = statusComboBox.Text; string genres = ""; //Genres starts off as a list and is converted to a single string List <string> genresList = new List <string>(); //loop to add each item in the checkedListBox to genresList for (int i = 0; i < genresCheckedListBox.Items.Count; i++) { if (genresCheckedListBox.GetItemChecked(i)) { genresList.Add((string)genresCheckedListBox.Items[i] + " "); } } //loop to add each item of genresList to a single string for (int i = 0; i < genresList.Count; i++) { genres += genresList[i]; } string review = reviewRichTextBox.Text; string location = pictureLocation.Text; //Add Game to database try { string connectionInfo = "datasource = 127.0.0.1; port = 3306; username = root; password = password"; MySqlConnection connect = new MySqlConnection(connectionInfo); string mySelectQuery = "use mydb; insert into games (Title, Rating, Status, Genres, Review, Location) Values( @0, @1, @2, @3, @4, @5);"; connect.Open(); MySqlCommand command = new MySqlCommand(mySelectQuery, connect); command.Parameters.AddWithValue("@0", title); command.Parameters.AddWithValue("@1", rating); command.Parameters.AddWithValue("@2", status); command.Parameters.AddWithValue("@3", genres); command.Parameters.AddWithValue("@4", review); command.Parameters.AddWithValue("@5", location); command.Prepare(); command.ExecuteNonQuery(); connect.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } this.Hide(); GamesList gamesListForm = new GamesList(); gamesListForm.ShowDialog(); }