I have a piece of code that I am trying to take all values from a connected database that fit criteria, stores them into a list, and then inputs them as options to select in a combobox.
The issue is when ran, rather than the values from the database, System.Data.SqlClient.SqlDataReader.
Example.
Here is the code being used
public void fillMakeBox() // used to populate the Make Box of the car builder.
{
connection.Open(); // opens connection to the database
List<String> makes =new List<string>();
string makesQuery = "SELECT makeName from makes";
SqlCommand makesSearch = new SqlCommand(makesQuery, connection);
SqlDataReader myReader = makesSearch.ExecuteReader();
while (myReader.Read()) // loops to add all values read to a list.
{
makes.Add(myReader.ToString());
}
while (makeSelection.SelectedItem != null)
{
List<String> filteredMakes = new List<string>();
string searchMakesQuery = $"Select makeName from makes WHERE makeName LIKE {makeSelection.SelectedItem} %";
SqlCommand searchMakeSearch = new SqlCommand(searchMakesQuery, connection);
SqlDataReader myReaderFilter = makesSearch.ExecuteReader();
while (myReaderFilter.Read())
{
filteredMakes.Add(myReaderFilter.ToString());
}
makes = filteredMakes;
}
makeSelection.DataSource = makes;
}
I have also tried using makes.Add(myReader["makeName"].ToString()); which did not work.