Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used parameters but it not working it shows error on
da.Fill(ds);
= Data type mismatch in criteria expression. how to solve it I really search everywhere but found nothing similar to this any help with this?

What I have tried:

string ccc = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ahmed\OneDrive\Documents\shop.accdb";

using (OleDbConnection conn = new OleDbConnection(ccc))
            {
string query = "select * from tbl3 where datetime between @ddlStates and @ddlStates2";
using (OleDbCommand myCommand = new OleDbCommand(query, conn))
                {

              myCommand.Parameters.AddWithValue("@ddlStates", dateTimePicker1.Value);
              myCommand.Parameters.AddWithValue("@ddlStates2", dateTimePicker2.Value);
                    conn.Open();

                    using (OleDbDataAdapter da = new OleDbDataAdapter(myCommand))
                    {
                   DataSet ds = new DataSet();
                        da.Fill(ds);
                        dataGridView1.DataSource = ds;

                    }
                }

            }
Posted
Updated 13-Jan-20 3:51am

Two possibillities:
1) Don't use keywords as column names: DATETIME is a SQL datatype, so that may not be helping. You can use them, but you need to "escape" the whole column name:
SQL
... WHERE [datetime] BETWEEN ...
But ... it is considered "bad practice". Instead, use a name that describes the values in the column, not their datatype: "EnterDate", "BirthDate", "ExpiryDate" or similar.

2) Check the column datatype: if you are storing date based info in text based columns, that can also cause problems when the system tries to convert them to a DateTime for comparison. Always store values in the most appropriate datatype, not string based just "because it's easier to code" - it always causes problems later on, when they are much, much harder to fix.
 
Share this answer
 
select * from tbl3 where datetime >= @ddlStates and datetime <= @ddlStates2

between does not always give you the results you are looking for

Also, I think that you should try dateTimePicker1.Text instead of Value (which is usually the index not the actual date)

and what Griff said below, make sure you have the data types correct for the ddl value/text your are sending.
 
Share this answer
 
v3
Comments
Member 14630006 13-Jan-20 10:03am    
thank so much it worked

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