Search code examples
sqlasp.netwebformsoledbms-access-2016

Syntax error (missing operator) in query expression: System.Data.OleDb.OleDbException


I am sending the following SQL query in MS Access 2016 as a string in ASP WebForm:

string showGridViewQuery = "select creditorName, amount, interestRate, interestType, interestCalculationMethod, insertedDate, o.fullName as owner, u.fullName as dataInsertedBy from tbl_savings s left join tbl_users o on ownerID = o.userID left join tbl_users u on dataEnteredByID = u.userID";

When I run, I get the following error:

System.Data.OleDb.OleDbException: Syntax error (missing operator) in query expression 'ownerID = o.userID left join tbl_users u on dataEnteredByID = u.userID'.

when the execution reaches showGridViewCommand.ExecuteNonQuery(); in the following code:

string connectionString = GetConnString();
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
string showGridViewQuery = "select creditorName, amount, interestRate, interestType, interestCalculationMethod, insertedDate, o.fullName as owner, u.fullName as dataInsertedBy from tbl_savings s left join tbl_users o on ownerID = o.userID left join tbl_users u on dataEnteredByID = u.userID";
OleDbCommand showGridViewCommand = new OleDbCommand(showGridViewQuery, con);
showGridViewCommand.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter olda = new OleDbDataAdapter(showGridViewCommand);
olda.Fill(dt);
GridViewSavingsTracker.DataSource = dt;
GridViewSavingsTracker.DataBind();
con.Close();

What am I missing here?


Solution

  • After a lot of trails, I was able to come to the right solution:

    string showGridViewQuery = "select savingsID, creditorName, amount, interestRate, interestType, interestCalculationMethod, insertedDate, o.fullName as owner, u.fullName as dataEnteredBy from ((tbl_savings as s) left join tbl_users as o on s.ownerID = o.userID) left join tbl_users as u on s.dataEnteredByID = u.userID";
    

    I knew MS Access was bad. But didn't know it was THIS bad!