Jace
Jace

Reputation: 37

Syntax error (missing operator) in query expression in C#

string sqlStatement = "select ID, Checkintime, RoomPrice, OrderNo from Orders where RoomType='" + selectedRoom.Key + "' and RoomNumber=" + selectedRoom.Value + " and Checkintime>="+dateOnly+" and CheckinTime<'"+endingTime+"'"; 

myAccessCommand = new OleDbCommand(sqlStatement, myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myDataAdapter.Fill(myDataSet, "Orders");

I have syntax error in the SQL statement line but I am not sure what went wrong that caused an syntax exception.

Upvotes: 0

Views: 235

Answers (1)

Flat Eric
Flat Eric

Reputation: 8111

Short fix: change

Checkintime>="+dateOnly+" 

to

Checkintime>='"+dateOnly+"'

(you forgot the tick marks in the first reference to Checkintime)


Much better: Use a parameterized query

Upvotes: 2

Related Questions