示例#1
0
        public Message Save(City newCity)
        {
            Message message = new Message();
            if (newCity.Name.Length == 0)
            {
                message.Status = "alert alert-warning";
                message.Details = "City name is required.";
                return message;
            }
            bool isCityExists = IsCityExists(newCity);

            if (isCityExists)
            {
                message.Status = "alert alert-danger";
                message.Details = "City name [" + newCity.Name + "] is already exists.";
                return message;
            }

            if (newCity.About.Length == 0)
            {
                message.Status = "alert alert-warning";
                message.Details = "About field name is required.";
                return message;
            }

            if (newCity.Location.Length == 0)
            {
                message.Status = "alert alert-warning";
                message.Details = "Location field name is required.";
                return message;
            }

            if (newCity.Weather.Length == 0)
            {
                message.Status = "alert alert-warning";
                message.Details = "Weather field name is required.";
                return message;
            }

            if (newCity.CountryId == 0)
            {
                message.Status = "alert alert-warning";
                message.Details = "Select a CountryDropDown of the city.";
                return message;
            }

            try
            {
                aCityGateway.AddCity(newCity);
                message.Status = "alert alert-success";
                message.Details = "City Added Successfully";
            }
            catch (SqlException ex)
            {
                message.Status = "alert alert-danger";
                message.Details = ex.Message;
            }

            return message;
        }
示例#2
0
        public void AddCity(City newCity)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            string sql = "INSERT INTO Cities (Name, About, NoOfDwellers, Location, Weather, CountryID) VALUES (@name, @about, @dwellers, @location, @weather, @countryId)";
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);
            command.Parameters.Clear();
            command.Parameters.Add("name", sqlDbType: SqlDbType.NVarChar);
            command.Parameters["name"].Value = newCity.Name;

            command.Parameters.Add("about", sqlDbType: SqlDbType.NVarChar);
            command.Parameters["about"].Value = newCity.About;

            command.Parameters.Add("dwellers", sqlDbType: SqlDbType.Int);
            command.Parameters["dwellers"].Value = newCity.NoOfDwellers;

            command.Parameters.Add("location", sqlDbType: SqlDbType.NVarChar);
            command.Parameters["location"].Value = newCity.Location;

            command.Parameters.Add("weather", sqlDbType: SqlDbType.NVarChar);
            command.Parameters["weather"].Value = newCity.Weather;

            command.Parameters.Add("countryId", sqlDbType: SqlDbType.Int);
            command.Parameters["countryId"].Value = newCity.CountryId;

            command.ExecuteNonQuery();
            connection.Close();
        }
示例#3
0
        public City GetCityByName(string name)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            string query = "SELECT * FROM Cities WHERE Name = @name";
            connection.Open();
            SqlCommand command = new SqlCommand(query, connection);

            command.Parameters.Clear();
            command.Parameters.Add("name", sqlDbType: SqlDbType.NVarChar);
            command.Parameters["name"].Value = name;

            City aCity = null;
            SqlDataReader reader = command.ExecuteReader();

            if (reader.Read())
            {
                aCity = new City();
                aCity.Id = Convert.ToInt32(reader["Id"].ToString());
                aCity.Name = reader["Name"].ToString();
                aCity.About = reader["About"].ToString();
                aCity.NoOfDwellers = Convert.ToInt32(reader["NoOfDwellers"].ToString());
                aCity.Location = reader["Location"].ToString();
                aCity.Weather = reader["Weather"].ToString();
                aCity.CountryId = Convert.ToInt32(reader["CountryID"].ToString());
            }
            reader.Close();
            connection.Close();
            return aCity;
        }
示例#4
0
        protected void saveButton_Click(object sender, EventArgs e)
        {
            Message message = new Message();

            try
            {
                string name = cityNameTextBox.Text.Trim();
                string about = aboutTextBox.Text.Trim();
                int dwellers = Convert.ToInt32(dwellersTextBox.Text);
                string location = locationTextBox.Text.Trim();
                string weather = weatherTextBox.Text.Trim();
                int countryId = Convert.ToInt32(countryDropDownList.SelectedItem.Value);
                City aCity = new City(name, about, dwellers, location, weather, countryId);
                message = aCityManager.Save(aCity);
            }
            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")
                {
                    ClearFormData();
                }
                LoadCityList();
            }
        }
示例#5
0
 private bool IsCityExists(City newCity)
 {
     City aCity = aCityGateway.GetCityByName(newCity.Name);
     if (aCity != null)
     {
         return true;
     }
     return false;
 }