private void exitEditToolStripMenuItem_Click(object sender, EventArgs e) { Height = Screen.FromControl(this).Bounds.Height - 250; updateBox.Hide(); BookSearchGroupBox.Show(); BooksGridView.Show(); BookSearchFormPanel.Show(); exitEditToolStripMenuItem.Enabled = false; editBookDetailsToolStripMenuItem1.Enabled = true; _selectedId = 0; }
private void UpdateSaveButton_Click(object sender, EventArgs e) { if (_selectedId > 0) { var numRegex = new Regex("[0-9]"); if (SubjectTextBox.Text == "") { label11.Show(); } if (TitleTextBox.Text == "") { label12.Show(); } if (AuthorTextBox.Text == "") { label13.Show(); } if (PublisherTextBox.Text == "") { label14.Show(); } if (EditionTextBox.Text == "") { label15.Show(); } if (PageTextBox.Text == "") { label21.Hide(); label16.Show(); } if (PageTextBox.Text != "") { if (!numRegex.IsMatch(PageTextBox.Text)) { label16.Hide(); label21.Show(); } if (numRegex.IsMatch(PageTextBox.Text)) { label16.Hide(); label16.Hide(); } } if (IsbnTextBox.Text == "") { label17.Show(); } if (AmountTextBox.Text == "") { label22.Hide(); label18.Show(); } if (AmountTextBox.Text != "") { if (!numRegex.IsMatch(AmountTextBox.Text)) { label18.Hide(); label22.Show(); } if (numRegex.IsMatch(AmountTextBox.Text)) { label18.Hide(); label22.Hide(); } } if (LibraryTextBox.Text == "") { label19.Show(); } if (SelfTextBox.Text == "") { label20.Show(); } if (SubjectTextBox.Text == "" || TitleTextBox.Text == "" || AuthorTextBox.Text == "" || PublisherTextBox.Text == "" || EditionTextBox.Text == "" || PageTextBox.Text == "" || IsbnTextBox.Text == "" || AmountTextBox.Text == "" || LibraryTextBox.Text == "" || SelfTextBox.Text == "") { return; } if (!numRegex.IsMatch(PageTextBox.Text) || !numRegex.IsMatch(AmountTextBox.Text) || !numRegex.IsMatch(EditionTextBox.Text) || !numRegex.IsMatch(SelfTextBox.Text)) { return; } try { var bookKUpdateClass = new BookUpdateClass() { Subject = SubjectTextBox.Text, Title = TitleTextBox.Text, Author = AuthorTextBox.Text, Publisher = PublisherTextBox.Text, Edition = Convert.ToInt32(EditionTextBox.Text), Pages = Convert.ToInt32(PageTextBox.Text), TotalNumber = Convert.ToInt32(AmountTextBox.Text), Library = LibraryTextBox.Text, SelfNumber = Convert.ToInt32(SelfTextBox.Text), Isbn = IsbnTextBox.Text }; _connection.Open(); var sqlCommand = new SqlCommand( "update Book set Subject=@Subject,Title=@Title,Author=@Author,Publisher=@Publisher,Edition=@Edition,Pages=@Pages,Isbn=@Isbn,NumberOfBooks=@TotalNumber,Library=@Library,ShelfNo=@SelfNumber where BookId=@Id", _connection); sqlCommand.Parameters.AddWithValue("@Subject", bookKUpdateClass.Subject); sqlCommand.Parameters.AddWithValue("@Title", bookKUpdateClass.Title); sqlCommand.Parameters.AddWithValue("@Author", bookKUpdateClass.Author); sqlCommand.Parameters.AddWithValue("@Publisher", bookKUpdateClass.Publisher); sqlCommand.Parameters.AddWithValue("@Edition", bookKUpdateClass.Edition); sqlCommand.Parameters.AddWithValue("@Pages", bookKUpdateClass.Pages); sqlCommand.Parameters.AddWithValue("@Isbn", bookKUpdateClass.Isbn); sqlCommand.Parameters.AddWithValue("@TotalNumber", bookKUpdateClass.TotalNumber); sqlCommand.Parameters.AddWithValue("@Library", bookKUpdateClass.Library); sqlCommand.Parameters.AddWithValue("@SelfNumber", bookKUpdateClass.SelfNumber); sqlCommand.Parameters.AddWithValue("@Id", _selectedId); sqlCommand.ExecuteNonQuery(); _connection.Close(); SubjectTextBox.Text = ""; TitleTextBox.Text = ""; AuthorTextBox.Text = ""; PublisherTextBox.Text = ""; EditionTextBox.Text = ""; PageTextBox.Text = ""; AmountTextBox.Text = ""; LibraryTextBox.Text = ""; SelfTextBox.Text = ""; IsbnTextBox.Text = ""; MessageBox.Show(@"Updated successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information); updateBox.Hide(); BooksGridView.Show(); LoadBookGridView(); UpdateSaveButton.Hide(); BookSearchFormPanel.Show(); _selectedId = 0; editBookDetailsToolStripMenuItem1.Enabled = true; exitEditToolStripMenuItem.Enabled = false; Height = Screen.FromControl(this).Bounds.Height - 250; } catch (Exception) { MessageBox.Show( @"Something may be wrong.Please make sure that all data fields are valid.Specially make sure that all integer fields have valid value!", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show(@"Unable to update.Something wrong ", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }