0
votes

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];
    }
3
AVAILABLE is a stringJames Kevin De Jesus

3 Answers

1
votes

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 *

1
votes

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);
1
votes

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