public static bool AddCountryToCountryDatabase(Country CountryToAdd) { try { using (StreamWriter writer = new StreamWriter(FilePath, true)) { writer.WriteLine(CountryToAdd); } return true; } catch (Exception) { return false; } }
public static List<Country> ReadCountrysFromDatabase() { var listOfCountrys = new List<Country>(); using (StreamReader reader = new StreamReader(FilePath)) { var currentLine = reader.ReadLine(); while (currentLine != null) { var currentCountry = currentLine.Split('|'); var country = new Country(currentCountry[0].Trim(), currentCountry[1].Trim(), int.Parse(currentCountry[2].Trim())); listOfCountrys.Add(country); currentLine = reader.ReadLine(); } } return listOfCountrys; }
protected void btnAdd_Click(object sender, EventArgs e) { var country = new Country(this.txtCountry.Text, this.txtCapital.Text, int.Parse(this.txtPopulation.Text)); var listOfCountries = FileOperations.ReadCountrysFromDatabase(); if (FileOperations.AddCountryToCountryDatabase(country)) { lblMessage.Text = "Country successfuly added."; this.txtCountry.Text = ""; this.txtCapital.Text = ""; this.txtPopulation.Text = ""; } else { lblMessage.Text = "Country already ecsists"; this.txtCountry.Text = ""; this.txtCapital.Text = ""; this.txtPopulation.Text = ""; } }