Peter
Peter

Reputation: 29

Problem with Syntax Error in SQL statement

I am trying to get a specific single value from an Access Database using C#.

For some reason what I am asking for is giving me an exception of

Syntax error in FROM clause

and I cant work out why.

I have tried running the SQL directly in Access itself and it works fine and gives me back the results I want, but I have no idea why its not working in my program.

ProbID is a number field as far as Access describes it and CorrDetails is a memo field.

For simplicity i have set the SQL to look for a specific value (137) but once i have the code working i will make it paramiterised.

Any ideas?

string corrAct;
            OleDbConnection dbConnection;
            dbConnection = new OleDbConnection(vDbString);
            string sqlString = "SELECT CorrDetails FROM Action WHERE ProbID=137";
            OleDbCommand command = new OleDbCommand(sqlString, dbConnection);

            using (dbConnection)
            {

            MessageBox.Show(sqlString);
            dbConnection.Open();
            corrAct = (String)command.ExecuteScalar();
            rtfCorrectiveAction.Text = Convert.ToString(corrAct);
            dbConnection.Close();
            }

Upvotes: 2

Views: 302

Answers (2)

BugFinder
BugFinder

Reputation: 17858

The problem is you havent taken into account keywords in SQL. https://learn.microsoft.com/en-us/sql/odbc/reference/appendixes/reserved-keywords?view=sql-server-ver15

Action is a keyword so should not be used really in another context, to use them put [] round them some it becomes

select stuff from [Action] where stuff=true

Upvotes: 1

Zohar Peled
Zohar Peled

Reputation: 82474

Action is a reserved word in MS Access. Wrap it with []:

 string sqlString = "SELECT CorrDetails FROM [Action] WHERE ProbID=137";

Upvotes: 3

Related Questions