private void GetDataInListView() { List<City> cities = new List<City>(); SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT * FROM city"; SqlCommand command = new SqlCommand(query,connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { City city1 = new City(); city1.cityid = reader["c_id"].ToString(); city1.cityname = reader["c_name"].ToString(); city1.aboutcity=reader["c_about"].ToString(); city1.countryname = reader["c_country"].ToString(); cities.Add(city1); } foreach (var city in cities) { ListViewItem item = new ListViewItem(); item.Text = city.cityid; item.SubItems.Add(city.cityname); item.SubItems.Add(city.aboutcity); item.SubItems.Add(city.countryname); cityInformationlLstView.Items.Add(item); } }
private void searchButton_Click(object sender, EventArgs e) { List<City> cities = new List<City>(); string search = searchTextBox.Text; if (cityRadioButton.Checked == true) { int id = 1; cityInformationlLstView.Items.Clear(); SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT * FROM city WHERE c_name LIKE'"+search+"%'"; SqlCommand command = new SqlCommand(query,connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { City city1 = new City(); city1.cityid = reader["c_id"].ToString(); city1.cityname = reader["c_name"].ToString(); city1.aboutcity = reader["c_about"].ToString(); city1.countryname = reader["c_country"].ToString(); cities.Add(city1); } foreach (var city in cities) { ListViewItem item = new ListViewItem(); item.Text = id.ToString(); item.SubItems.Add(city.cityname); item.SubItems.Add(city.aboutcity); item.SubItems.Add(city.countryname); cityInformationlLstView.Items.Add(item); id++; } } else if(countryRadioButton.Checked==true) { int id = 1; cityInformationlLstView.Items.Clear(); SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT * FROM city WHERE c_country LIKE'" + search + "%'"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { City city1=new City(); city1.cityid = reader["c_id"].ToString(); city1.cityname = reader["c_name"].ToString(); city1.aboutcity = reader["c_about"].ToString(); city1.countryname = reader["c_country"].ToString(); cities.Add(city1); } foreach (var city in cities) { ListViewItem item = new ListViewItem(); item.Text = id.ToString(); item.SubItems.Add(city.cityname); item.SubItems.Add(city.aboutcity); item.SubItems.Add(city.countryname); cityInformationlLstView.Items.Add(item); id++; } } }