protected void saveButton_Click(object sender, EventArgs e) { try { string name = cityNameTextBox.Text; string about = aboutCityTextBox.Text; long noofDwellers = Convert.ToInt64(dwellersTextBox.Text); string location = locationTextBox.Text; string weather = weatherTextBox.Text; int countryId = Convert.ToInt32(countryDropDownList.SelectedValue); City aCity=new City(name,about,noofDwellers,location,weather,countryId); message = cityManager.Save(aCity); messageLabel.Text = message; LoadAllCity(); ClearAlltextBoxes(); } catch (Exception exceptionObj) { message = exceptionObj.Message; if (exceptionObj.InnerException != null) { message += "<br/>System Error:" + exceptionObj.InnerException.Message; } messageLabel.Text = message; } }
public int Insert(City aCity) { try { string query = "INSERT INTO t_City VALUES(@name,@about,@noOfDwellers,@location,@weather,@countryId)"; CommandObj.CommandText = query; CommandObj.Parameters.Clear(); CommandObj.Parameters.AddWithValue("@name", aCity.Name); CommandObj.Parameters.AddWithValue("@about", aCity.About); CommandObj.Parameters.AddWithValue("@noOfDwellers", aCity.NoOfDewellers); CommandObj.Parameters.AddWithValue("@location", aCity.Location); CommandObj.Parameters.AddWithValue("@weather", aCity.Weather); CommandObj.Parameters.AddWithValue("@countryId", aCity.CountryId); ConnectionObj.Open(); int rowAffected = CommandObj.ExecuteNonQuery(); ConnectionObj.Close(); CommandObj.Dispose(); return rowAffected; } catch (Exception exception) { throw new Exception("Unable not save",exception); } finally { ConnectionObj.Close(); CommandObj.Dispose(); } }
protected void saveButton_Click(object sender, EventArgs e) { try { string name = nameTextBox.Text; string about = Request.Form["edit"]; int noOfDwellers = Convert.ToInt32(noOfDwellersTextBox.Text); string location = locationTextBox.Text; string weather = weatherTextBox.Text; int countryId = Convert.ToInt32(countryDropDownList.SelectedValue); City aCity = new City(name, about, noOfDwellers, location, weather, countryId); string message = cityManager.Save(aCity); messageLabel.Text = message; LoadAllCity(); ClearAllTextBoxes(); } catch (Exception exception) { string mgs = exception.Message; if (exception.InnerException != null) { mgs += "<br/>System Error:" + exception.InnerException.Message; } messageLabel.Text = mgs; } }
private bool IsCityExits(City aCity) { City city=cityGateway.GetCityInformationByName(aCity.Name); if (city != null) { return true; } return false; }
public string Save(City aCity) { if (IsCityExits(aCity)) { return "City Name must be unique"; } if (cityGateway.Insert(aCity)>0) { return "Save sucessfully!"; } return "Failed to Save"; }
public City GetCityInformationByName(string cityName) { try { string query = "SELECT * FROM t_City WHERE Name=@name"; CommandObj.CommandText = query; CommandObj.Parameters.Clear(); CommandObj.Parameters.AddWithValue("@name", cityName); ConnectionObj.Open(); City city = null; SqlDataReader reader = CommandObj.ExecuteReader(); if (reader.Read()) { city = new City(); city.Id = Convert.ToInt32(reader["Id"].ToString()); city.Name = reader["Name"].ToString(); city.About = reader["About"].ToString(); city.NoOfDewellers = Convert.ToInt32(reader["No_Of_Dwellers"].ToString()); city.Location = reader["Location"].ToString(); city.Weather = reader["Weather"].ToString(); city.CountryId = Convert.ToInt32(reader["CountryId"].ToString()); } reader.Close(); ConnectionObj.Close(); return city; } catch (Exception exception) { throw new Exception("Unable to Get city information",exception); } finally { ConnectionObj.Close(); CommandObj.Dispose(); } }
protected void saveButton_Click(object sender, EventArgs e) { try { if (cityNameTextBox.Text.Length > 50) { messageLabel.ForeColor = System.Drawing.Color.Red; messageLabel.Text = "Please! Enter a City Name within 50 character and then try"; return; } string name = cityNameTextBox.Text; string about = aboutCityTextBox.Text; long noofDwellers = Convert.ToInt64(dwellersTextBox.Text); string location = locationTextBox.Text; string weather = weatherTextBox.Text; int countryId = Convert.ToInt32(countryDropDownList.SelectedValue); City aCity=new City(name,about,noofDwellers,location,weather,countryId); message = cityManager.Save(aCity); messageLabel.ForeColor = System.Drawing.Color.Black; messageLabel.Text = message; LoadAllCity(); ClearAlltextBoxes(); } catch (Exception exceptionObj) { message = exceptionObj.Message; if (exceptionObj.InnerException != null) { message += "<br/>System Error:" + exceptionObj.InnerException.Message; } messageLabel.ForeColor = System.Drawing.Color.Red; messageLabel.Text = message; } }
public List<City> GetAll() { string query = "SELECT * FROM t_City"; CommandObj.CommandText = query; List<City> cities=new List<City>(); ConnectionObj.Open(); SqlDataReader reader = CommandObj.ExecuteReader(); while (reader.Read()) { City city=new City(); //city.Id = Convert.ToInt32(reader["Id"].ToString()); city.Name = reader["Name"].ToString(); city.About = reader["About"].ToString(); city.Location = reader["Location"].ToString(); city.Weather = reader["Weather"].ToString(); city.NoOfDewellers = Convert.ToInt32(reader["No_Of_Dwellers"].ToString()); cities.Add(city); } reader.Close(); ConnectionObj.Close(); return cities; }