private void pictureBox2_Click(object sender, EventArgs e) { if (SerialNumber.Text == "" || PublishYear.Text == "" || BookName.Text == "") { MessageBox.Show("Please enter the required data."); } else { bool b = false; FileStream fs = new FileStream("Books.txt", FileMode.OpenOrCreate); StreamReader sr = new StreamReader(fs); string[] fields; string[] records; while (sr.Peek() != -1) { records = sr.ReadLine().Split('#'); for (int i = 0; i < records.Length - 1; i++) { fields = records[i].Split('@'); if (fields[1] == BookName.Text) { b = true; } } } sr.Close(); fs.Close(); if (b == false && AutherIDs.SelectedIndex != -1) { FileStream File = new FileStream("Books.txt", FileMode.Append, FileAccess.Write); StreamWriter sw = new StreamWriter(File); // gets the string value of the combobox string AuthorId = AutherIDs.SelectedItem.ToString().Trim('\0'); //writes the book using '@' dilimter only if the auther id is valid sw.Write(SerialNumber.Text); sw.Write('@'); sw.Write(BookName.Text); sw.Write('@'); sw.Write(PublishYear.Text); sw.Write('@'); sw.Write(dic[AuthorId]); sw.Write('#'); //resets the text boxes SerialNumber.Clear(); BookName.Clear(); PublishYear.Clear(); AutherIDs.SelectedText = " "; // Confirms the addition to the user MessageBox.Show("Book added sucssefully."); sw.Close(); File.Close(); } else { MessageBox.Show("Book already exists."); } } }