protected void saveCountryButton_Click(object sender, EventArgs e) { Message message = new Message(); try { string name = nameCountryTextBox.Text.Trim(); string about = aboutCountryTextBox.Text.Trim(); Country newCountry = new Country(name, about); message = aCountryManager.Save(newCountry); } catch (Exception ex) { message.Status = "alert alert-danger"; message.Details = ex.Message; } finally { messageLabel.CssClass = message.Status; messageLabel.Text = message.Details; if (message.Status == "alert alert-success") { ClearText(); } LoadAllCountries(); } }
public Message Save(Country newCountry) { Message message = new Message(); if (newCountry.CountryName.Length == 0) { message.Status = "alert alert-warning"; message.Details = "Name is Blank."; return message; } bool alreadyExists = aCountryGateway.SearchCountryByName(newCountry.CountryName); if (alreadyExists) { message.Status = "alert alert-danger"; message.Details = "Country name [" + newCountry.CountryName + "] is already exists."; return message; } if (newCountry.CountryAbout.Length == 0) { message.Status = "alert alert-warning"; message.Details = "About field is required."; return message; } try { aCountryGateway.Save(newCountry); message.Status = "alert alert-success"; message.Details = "Country Added Successfully"; } catch (SqlException ex) { message.Status = "alert alert-danger"; message.Details = ex.Message; } return message; }
public List<Country> GetAllCountries() { SqlConnection connection = new SqlConnection(connectionString); string query = @"SELECT * FROM Countries ORDER BY Name"; connection.Open(); SqlCommand command = new SqlCommand(query, connection); SqlDataReader reader = command.ExecuteReader(); List<Country> countries = new List<Country>(); while (reader.Read()) { Country country = new Country(); country.CountryId = Convert.ToInt32(reader["Id"].ToString()); country.CountryName = reader["Name"].ToString(); countries.Add(country); } return countries; }