Search code examples
c#winformsoledbdataadapter

OleDbCommand syntax error 0x80040E14 in SELECT Statement


This query runs perfectly in Access 2010:

SELECT te.[EnterpirseName], COUNT(to.[ID]) AS Orgs, SUM(to.[Members]) AS Mems
FROM tbl_Enterprise AS te, tbl_Organizations AS to
WHERE to.[Enterprise_ID] = te.[ID] AND te.[Status] = 1
GROUP BY te.[EnterpirseName]

However when I try to execute it from my C# winforms application, I get syntax error:

0x80040E14 Syntax error near keyword FROM

EDIT: My C# code:

string strCommand = "SELECT te.[EnterpirseName], COUNT(to.[ID]) AS Orgs, SUM(to.[Members]) AS Mems" +
    " FROM tbl_Enterprise AS te, tbl_Organizations AS to" +
    " WHERE to.[Enterprise_ID] = te.[ID]" +
    " AND te.[Status] = 1" +
    " GROUP BY te.[EnterpirseName]";

DataTable dt = new DataTable();

using (OleDbConnection conn = new OleDbConnection(strConnString))
{
    OleDbDataAdapter da = new OleDbDataAdapter(strCommand, conn);

    using (DataSet ds = new DataSet())
    {
        // THIS IS WHERE I GET THE ERROR
        da.Fill(ds);
        dt = ds.Tables[0];
    }
}

That's how strCommand looks like in Debug:

enter image description here


Solution

  • TO is a reserved word in Access as well. Try to replace it to something like tbl_Org.