1
votes

I have to datetimepickers FromDate and ToDate, and want to do select data between to dates, I have written the query

select  * from tbl where pDate>='" + dtpFrom.value + "'and pDate<='" + dtpTo.value + "'");

This query giving error

Data type mismatch in criteria expression

But datatype is Date/Time in ms access table.

1
Heard of sql injection? Try using the string representation of the date object - 3dd
I am using ms access database - Vampire

1 Answers

2
votes

Looks like you try to put single quotes for your DateTime values. # for dates and ' for strings but they required for literal SQL queries.

If you use parameterize queries, you will not need them.

using(var con = new OleDbConnection(conString))
using(var cmd = con.CreateCommand())
{
   cmd.CommandText = "select * from tbl where pDate >= ? and pDate <= ?"
   cmd.Parameters.AddWithValue("?", dtpFrom.Value);
   cmd.Parameters.AddWithValue("?", dtpTo.Value);
   ...
   ...
}