Vampire
Vampire

Reputation: 320

How to do select query between dates of two datetime picker?

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.

Upvotes: 1

Views: 3142

Answers (1)

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98750

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);
   ...
   ...
}

Upvotes: 2

Related Questions