Hey all so basically I have a C# application that allows a user to enter their query about cars in stock (Fields are Car Manufacturer, Model, Age, Litre Size).
The query's contents can vary on what fields the user is looking for (e.g. a person may look for all Ford cars or another query may be all Ford cars that are 6 years old).
After the query is entered the program SHOULD return the data requested to a dataGridView on the form. My problem is that the query executes but only returns a blank record as though it cannot find a suitable record (I have only entered query's that would definitely return a record) this has lead me to believe something is wrong with my coding (particularly the parameters) but I can't figure out where I'm going wrong, could anyone lend a hand?
The query's contents can vary on what fields the user is looking for (e.g. a person may look for all Ford cars or another query may be all Ford cars that are 6 years old).
After the query is entered the program SHOULD return the data requested to a dataGridView on the form. My problem is that the query executes but only returns a blank record as though it cannot find a suitable record (I have only entered query's that would definitely return a record) this has lead me to believe something is wrong with my coding (particularly the parameters) but I can't figure out where I'm going wrong, could anyone lend a hand?
Code:
string ConnStr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = H:\\School Work\\Computing A Level\\Stock checker\\Program\\Morgan's Motors Database.mdb;"; OleDbConnection conn_database = new OleDbConnection(); conn_database.ConnectionString = ConnStr; OleDbCommand comm_database = new OleDbCommand(); comm_database.CommandText = "SELECT * FROM [Car Info] WHERE ? = ?"; comm_database.Connection = conn_database; conn_database.Open(); OleDbDataAdapter adap_database = new OleDbDataAdapter(comm_database); DataTable data_database = new DataTable(); for (int i = 0; i < ColumnName.Count; i++) { //comm_database.Parameters.AddWithValue("?", ColumnName[i].ToString()); //comm_database.Parameters.AddWithValue("?", EnteredFields[i].ToString()); adap_database.SelectCommand.Parameters.AddWithValue("?", ColumnName[i]); adap_database.SelectCommand.Parameters.AddWithValue("?", EnteredFields[i]); adap_database.Fill(data_database); } BindingSource bind_database = new BindingSource(); bind_database.DataSource = data_database; dataGridView1.DataSource = bind_database;
Comment