Click here to Skip to main content
16,016,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a ms access db with a datetime column 'resolved on' ....In m c# application i have two datetimepickers.... datetimepicker1 is 'from date' and datetimepicker2 is 'to date'

my sql query is
string query = "select count(*) from incidents where [resolved on] > '" + dateTimePicker1.Value + "' and [resolved on] < '" + dateTimePicker2.Value + "' ";

im getting this error >> data type mismatch in criteria expresion

ms access column is datatime format long
datetimepickers format is long date

What I have tried:

string query = "select count(*) from incidents where [resolved on] > '" + dateTimePicker1.Value + "' and [resolved on] < '" + dateTimePicker2.Value + "' ";
Posted
Updated 15-Apr-18 3:08am
v2
Comments
Richard MacCutchan 15-Apr-18 9:01am    
What data type is the [resolved on] column?
Member 13101003 15-Apr-18 9:06am    
datetime
Richard MacCutchan 15-Apr-18 9:13am    
DatePicker.Value is the same according to the documentation. You need to investigate your code more closely to see exactly why the mismatch is occurring. Are you sure that the MSAccess DateTime is the same format?
Member 13101003 15-Apr-18 9:21am    
How to know if or not the format is same?
Richard MacCutchan 15-Apr-18 9:43am    
You need to check the documentation for both types.

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

In this case, you won't have an SQL Injection problem, but you need as a matter of urgency to check everywhere else in your code to make sure that you have fixed all of them. Leave one, and you will get problems sooner or later.

When you have it parameterized, the problem may go away - but if it doesn't then you need to check your DB design, and make sure that the "Resolved On" column is set as DATE, DATETIME, or DATETIME2 - if it's VARCHAR or NVARCHAR then it will never work properly, as string comparisons are conducted character-by character, with the result of the whole comparison being the result of the first different character pair. That's useless for dates, because just as with numbers it leads to this sort order:
1
10
11
...
19
2
20
21
...
which isn't any use to you at all.
 
Share this answer
 
The only sensible way to handle dates and times is to use the DateTime data type both in C# and Access.
All Access sees is a string, it have no way to know that it is a date and what it means.
In order to compare such a string, you have to reorder parts to 'yyyymmdd' with a month transcoding to its number. And this for every date you want to compare.
 
Share this answer
 
Comments
Member 13101003 15-Apr-18 7:43am    
it is datetime only
Patrice T 15-Apr-18 7:52am    
what is the question ?
Member 13101003 15-Apr-18 7:58am    
im getting 'data type mismatch in criteria expression' error
Patrice T 15-Apr-18 8:09am    
Use Improve question to update your question.
and show what you are doing now.
Member 13101003 15-Apr-18 8:49am    
i have a ms access db with a datetime column 'resolved on' ....In m c# application i have two datetimepickers.... datetimepicker1 is 'from date' and datetimepicker2 is 'to date'

my sql query is
string query = "select count(*) from incidents where [resolved on] > '" + dateTimePicker1.Value + "' and [resolved on] < '" + dateTimePicker2.Value + "' ";

im getting this error >> data type mismatch in criteria expresion

ms access column is datatime format long
datetimepickers format is long date

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