Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a table (in Access) shown in a DataGridView Control. The table consists of some fields including a filed of the "Date/Time" type called "Date". I want to filter the table, based on this field. Although it sounds trivial, it raises an error saying that:

There is a syntax error (missing operator) in query expression "Date = 2010/07/11 12:00:00 AM".
I used the code below. Could you please let me know what is wrong with the code?

Thank you a lot


using System.Data.OleDb;


private void Form1_Load(object sender, EventArgs e)
        {
            string path = @"E:\Data\Myoffice\Access\";
            string DataBase_Name = "MyDataBase.accdb";
            string Table_Name = "Book";
            string Sql_Comnd = "select * from " + Table_Name + " Where  Date = 2010/07/11 12:00:00 AM";

            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = " + path + DataBase_Name;
            
            OleDbDataAdapter da = new OleDbDataAdapter( Sql_Comnd , con);
            DataSet ds = new DataSet();
            con.Open();
            da.Fill(ds, Table_Name);
            dataGridView1.DataSource = ds.Tables[Table_Name].DefaultView;
            con.Close();
        }
Posted

For your specific issue use a #

Where  Date = #2010/07/11 12:00:00 AM#
 
Share this answer
 
Comments
ahhashemi 22-Jul-10 10:59am    
Reason for my vote of 5
For fast Reply & being helpful
I suspect apart from the obvious design issues, that your date probably needs to be in quotes at least.
 
Share this answer
 
Comments
ahhashemi 22-Jul-10 3:51am    
Reason for my vote of 2
For Fast Reply but
Christian Graus 23-Jul-10 23:52pm    
FYI, I'd fire anyone who wrote this code in a commercial application. I don't use Access, and so I took a stab at how to format a date, but you really should be using parameterised queries. Your db path should not be hard coded, that is insane. Your data code should not be in the presentation layer. You should use meaningful variable names. For all these reasons, your inability to do basic research on how to format an Access query, are the least of your issues.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900