public List<Country> GetAllCountries() { SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT * FROM Country ORDER BY CountryName ASC"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); List<Country> countries = new List<Country>(); while (reader.Read()) { Country country = new Country(); country.CountryID = (int) reader["CountryID"]; country.CountryName = reader["CountryName"].ToString(); country.CountryAbout = reader["CountryAbout"].ToString(); countries.Add(country); } reader.Close(); connection.Close(); return countries; }
public int SaveCountry(Country country) { SqlConnection connection = new SqlConnection(connectionString); string query = "INSERT INTO Country VALUES ('" + country.CountryName + "','" + country.CountryAbout + "')"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); int roweffected = command.ExecuteNonQuery(); connection.Close(); return roweffected; }
protected void submitButton_Click(object sender, EventArgs e) { string countryName = countryNameTextBox.Text; string countryAbout = countryAboutTextBox.Text.Replace("'", "''"); Country country=new Country(countryName,countryAbout); messageBoxLabel.Text = countryManager.SaveCountry(country); countryGridView.DataSource = countryManager.GetAllCountries(); countryGridView.DataBind(); TextClear(); }
public string SaveCountry(Country country) { Country acountry = countryGateway.IsExist(country); if (acountry == null) { int roweffected = countryGateway.SaveCountry(country); if (roweffected > 0) { return "Save successfully"; } else { return "Save failed"; } } else { return "Country Name must be unique"; } }
public Country IsExist(Country country) { SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT * FROM Country WHERE CountryName='" + country.CountryName + "'"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); Country aCountry = null; if (reader.HasRows) { reader.Read(); aCountry = new Country(); aCountry.CountryID = (int) reader["CountryID"]; aCountry.CountryName = reader["CountryName"].ToString(); aCountry.CountryAbout = reader["CountryAbout"].ToString(); } reader.Close(); connection.Close(); return aCountry; }