user2176150
user2176150

Reputation: 303

MS Access Syntax error (missing operator)

I am using the below code to get the record from a access database. Now i got the below error Syntax error (missing operator) in query expression. I can't able to understand this issue. Please help me to fix this issue.

cmd = new OleDbCommand(@"Select * from tbl_men_schedule where fld_mem_id=" + 0 + " and  fld_startdate=" + Convert.ToDateTime(txt_startDate.Text) + " and fld_enddate=" + Convert.ToDateTime(txt_enddate.Text) + "", con);
 da = new OleDbDataAdapter(cmd);
 DataSet ds = new DataSet();
 da.Fill(ds);

Syntax error (missing operator) in query expression 'fld_mem_id=0 and  fld_startdate=1/4/2013 12:00:00 AM and fld_enddate=4/11/2013 12:00:00 AM'.

Upvotes: 0

Views: 2637

Answers (3)

HansUp
HansUp

Reputation: 97101

If fld_startdate and fld_enddate are Date/Time data type, enclose those date values with the # delimiter.

where
        fld_mem_id=0
    and fld_startdate=#1/4/2013 12:00:00 AM#
    and fld_enddate=#4/11/2013 12:00:00 AM#

If they are text data type, enclose them with quotes.

where
        fld_mem_id=0
    and fld_startdate='1/4/2013 12:00:00 AM'
    and fld_enddate='4/11/2013 12:00:00 AM'

However if you use a parameter query instead, you wouldn't need to delimit those values.

Upvotes: 1

sam yi
sam yi

Reputation: 4934

If you hardcoding "0"... and you're missing single quotes.

"Select * from tbl_men_schedule where fld_mem_id=0 and fld_startdate='" + 
txt_startDate.Text + "' and fld_enddate='" + 
txt_enddate.Text + "'"

Upvotes: 0

Piyas De
Piyas De

Reputation: 1764

Your statement should be -

"Select * from tbl_men_schedule where fld_mem_id=" + 0 + " and  fld_startdate='" + Convert.ToDateTime(txt_startDate.Text) + "' and fld_enddate='" + Convert.ToDateTime(txt_enddate.Text) + "'"

Hope this helps you

Upvotes: 0

Related Questions