0

I have 3 combobox that take there information from MS Access database. I want to select data from database according to the values of the combo boxes.

I wrote this query:

string query = "select * from products where category='" + comboBox1.Text + "' and subcategory='" + comboBox2.Text + "' and size='" + comboBox3.Text + "'";

But it gives me the following exception:

IErrorInfo.GetDescription failed with E_FAIL(0x80004005).

Can you help me?

Full code:

connection.Open(); 
OleDbCommand command = new OleDbCommand(); 
command.Connection = connection; 
string query = "select * from products where category='" + comboBox1.Text + 
    "' and subcategory='" + comboBox2.Text + "' and size='" + comboBox3.Text + "'"; 
command.CommandText = query; 
OleDbDataAdapter da = new OleDbDataAdapter(command); 
DataTable dt = new DataTable(); 
da.Fill(dt); 
dataGridView1.DataSource = dt; 
connection.Close();
Rob
  • 26,989
  • 16
  • 82
  • 98
Saleh923
  • 105
  • 8
  • 2
    The error is certainly not related with code of line you pasted. How are you accessing the DB? Post the call stack – Luis Filipe Aug 19 '15 at 00:12
  • 2
    btw, your code is vulnerable to sql injection. please read about it – Luis Filipe Aug 19 '15 at 00:12
  • connection.Open(); OleDbCommand command = new OleDbCommand(); command.Connection = connection; string query = "select * from products where category='" + comboBox1.Text + "' and subcategory='" + comboBox2.Text + "' and size='" + comboBox3.Text + "'"; command.CommandText = query; OleDbDataAdapter da = new OleDbDataAdapter(command); DataTable dt = new DataTable(); da.Fill(dt); dataGridView1.DataSource = dt; connection.Close(); – Saleh923 Aug 19 '15 at 00:21
  • Edit your question to include the code! – John Saunders Aug 19 '15 at 00:26
  • You can put your code in the question itself it's easier to read. One problem i identify is that you do not have a connection string - does it exist for MS Access? try to get full working examples from internet, i'd say, that have the necessary plumbing – Luis Filipe Aug 19 '15 at 00:27
  • i am trying to post my code but it gives me error that i have to put spaces – Saleh923 Aug 19 '15 at 00:32

1 Answers1

1

I'm guessing since size is a reserved word in MS Access, it is throwing that error.

See this link for list of reserved words in Access.

Try changing the column name. Also, try to use parameterized query to prevent sql injection. See this answer on how to use parameterized query in Access.

Community
  • 1
  • 1
ANewGuyInTown
  • 5,957
  • 5
  • 33
  • 45