protected void saveButton_Click(object sender, EventArgs e) { int countryID = Convert.ToInt32(countryDropDownList.SelectedValue); var city = new City(cityNameTextBox.Text, cityAboutTextBox.Text, Convert.ToInt32(noOfDwellersTextBox.Text), cityLocationTextBox.Text, cityWeatherTextBox.Text); city.CountryID = countryID; showMessageLable.Text = manager.AddCity(city); LoadAllCities(); }
public string AddCity(City city) { City aCity = gateway.IsExistCity(city); if (aCity == null) { gateway.AddCity(city); return "City added successfully."; } else if (aCity != null) { return "Exist City."; } else { return "No data has been Added."; } }
public List<City> GetAllCities() { SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT * FROM CityView ORDER BY CityName ASC"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); List<City> cities = new List<City>(); while (reader.Read()) { //int countryID = Convert.ToInt32(reader["CountryID"].ToString()); //string cityAbout = reader["CityAbout"].ToString(); //string cityLocation = reader["CityLocation"].ToString(); // string cityWeather = reader["CityWeather"].ToString(); string cityName = reader["CityName"].ToString(); int noOfDwellers = Convert.ToInt32(reader["NoOfDwellers"].ToString()); string countryName = reader["CountryName"].ToString(); City city = new City(); city.CityName = cityName; city.NoOfDwellers = noOfDwellers; city.CountryName = countryName; cities.Add(city); } reader.Close(); connection.Close(); return cities; }
public bool AddCity(City city) { SqlConnection connection = new SqlConnection(connectionString); string quiry = "INSERT INTO City VALUES('" + city.CityName + "','" + city.CityAbout + "','" + city.NoOfDwellers + "','" + city.CityLocation + "','" + city.CityWeather + "','" + city.CountryID + "')"; SqlCommand command = new SqlCommand(quiry, connection); connection.Open(); int rowAffectedCount = command.ExecuteNonQuery(); if (rowAffectedCount > 0) { return true; } return false; }
public City IsExistCity(City aCity) { SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT * FROM City WHERE CityName='" + aCity.CityName + "'"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); City city = null; while (reader.Read()) { string cityName = reader["CityName"].ToString(); string cityAbout = reader["CityAbout"].ToString(); int noOfDwellers = Convert.ToInt32(reader["NoOfDwellers"].ToString()); string cityLocation = reader["CityLocation"].ToString(); string cityWeather = reader["CityWeather"].ToString(); city = new City(cityName, cityAbout, noOfDwellers, cityLocation, cityWeather); } reader.Close(); connection.Close(); return city; }