public static bool AddBookToDataBase(Book bookToAdd) { try { using (StreamWriter writer = new StreamWriter(FileOperations.FilePath, true)) { writer.WriteLine(bookToAdd); } return true; } catch (Exception) { return false; } }
protected void addButton_Click(object sender, EventArgs e) { var bookToAdd = new Book( this.titleTextBox.Text, this.authorTextBox.Text, double.Parse(this.priceTextBox.Text)); if (FileOperations.AddBookToDataBase(bookToAdd)) { this.messageLable.Text = "Successfully added the book to the library"; this.titleTextBox.Text = string.Empty; this.authorTextBox.Text = string.Empty; this.priceTextBox.Text = string.Empty; } else { this.messageLable.ForeColor = System.Drawing.Color.Red; this.messageLable.Text = "An error occured while adding the book"; } }
public static List<Book> ReadBooksFromDataBase() { var listOfBooks = new List<Book>(); using (var reader = new StreamReader(FileOperations.FilePath)) { var currentBookLine = reader.ReadLine(); while (currentBookLine != null) { var currentBook = currentBookLine.Split('|'); var book = new Book( currentBook[0].Trim(), currentBook[1].Trim(), double.Parse(currentBook[2].Trim())); listOfBooks.Add(book); currentBookLine = reader.ReadLine(); } } return listOfBooks; }
protected void addBookBtn_Click(object sender, EventArgs e) { try { Book bookToAdd = new Book(); bookToAdd.Title = titleTxtBox.Text; bookToAdd.Author = authorTxtBox.Text; bookToAdd.Price = double.Parse(priceTxtBox.Text); bookToAdd.Genre = genreTxtBox.Text; using (StreamWriter writer = new StreamWriter(ConfigurationManager.AppSettings["FilePath"], true)) { writer.WriteLine(bookToAdd); } resultLabel.Visible = true; resultLabel.Text = "Success"; } catch (Exception) { resultLabel.Visible = true; resultLabel.Text = "Something went wrong!"; } }