0

I am working a C# Windows Application Form connecting to MS Access. I am filling the datagridview with a specific query but this happened

Syntax error (missing operator) in query expression 'Model WHERE Status = 'AVAILABLE''.

on this query

OleDbDataAdapter daAvailable = new OleDbDataAdapter("SELECT Type, Brand, Model, SerialNo, Status, Remarks, RAM, HDD, ODD, VideoCard, PS FROM Available ORDER BY Type, Brand, Model WHERE Status = 'AVAILABLE'", cnn);

What should I do?

Here's my code

OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\james\Documents\Visual Studio 2010\Projects\Vault\Vault\VaultDatabase.accdb");
DataSet dstAvailable = new DataSet();
DataSet dstData = new DataSet();
DataSet dstDeployment = new DataSet();
DataSet dstLog = new DataSet();


public void FillAvailable(DataGridView dgv)
{
    OleDbDataAdapter daAvailable = new OleDbDataAdapter("SELECT Type, Brand, Model, SerialNo, Status, Remarks, RAM, HDD, ODD, VideoCard, PS FROM Available ORDER BY Type, Brand, Model WHERE Status = 'AVAILABLE'", cnn);
    daAvailable.Fill(dstAvailable);
    dgv.DataSource = dstAvailable.Tables[0];
}
1
  • AVAILABLE is a string Commented May 16, 2013 at 9:19

3 Answers 3

1

Write like this

public void FillAvailable(DataGridView dgv)
{
    OleDbDataAdapter daAvailable = new OleDbDataAdapter("SELECT Type, Brand, Model, SerialNo, Status, Remarks, RAM, HDD, ODD, VideoCard, PS FROM Available  WHERE Status = 'AVAILABLE' ORDER BY Type, Brand, Model", cnn);
    daAvailable.Fill(dstAvailable);
    dgv.DataSource = dstAvailable.Tables[0];
}

*Order By Comes After Where *

Sign up to request clarification or add additional context in comments.

Comments

1

ORDER BY

is always after the WHERE clause, it should be:

OleDbDataAdapter daAvailable = new OleDbDataAdapter("SELECT Type, Brand, Model, SerialNo, Status, Remarks, RAM, HDD, ODD, VideoCard, PS FROM Available WHERE Status = 'AVAILABLE'  ORDER BY Type, Brand, Model", cnn);

Comments

1

ORDER BY clause should be after the WHERE clause in SQL

So your query should be:

OleDbDataAdapter daAvailable = new OleDbDataAdapter("SELECT Type, Brand, Model, SerialNo, "+
     "Status, Remarks, RAM, HDD, ODD, VideoCard, PS " + 
     "FROM Available " +
     "WHERE Status = 'AVAILABLE' "+
     "ORDER BY Type, Brand, Model", cnn);

     //The above query is broken down on multiple lines for clarity

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.