Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to display records from a table named staffpresentee from a database using the where clause...

i am using visual studio 2010...

the table design is as follows-
     fieldname                  datatype          allownull
1    staff_id (fk)              numeric(18,0)       yes
2      date                           date                no
3    present                       char(1)             yes

Note that the type of the 2nd field is date and not datetime...

I want to display all records from the table that have today's date int the 'date' field...
My current query is-

cmd = New SqlClient.SqlCommand("select * from staffpresentee where date=" & Date.Now.Date, cn)
dr = cmd.ExecuteReader

for dr = cmd.ExecuteReader i get the error "Operand type clash: date is incompatible with int"

i tried the select * from staffpresentee query without the where clause it runs correctly...
Posted
Comments
ZurdoDev 18-Mar-13 8:51am    
What type of database? SQL? You likely just need to add single quotes around it, if it's SQL. SELECT * FROM staffpresentee WHERE date = '" & Date.Now.Date & "'"
Shraa1 18-Mar-13 10:25am    
it was sql... i tried the ' ' marks but it did not help. i mean the "Operand type clash: date is incompatible with int" error was gone but the next line showed a related problem... it said cannot concatenate to string or something like that... never mind now anyways, the soltion by originalgriff was helpful...
Shraa1 18-Mar-13 10:25am    
and dont forget to vote for the question :)

1 solution

Two problems:
1) "date" is an SQL reserved word, so calling your column that is always going to give problems.
2) Send the date as a parameter rather that concatenating strings - that is always a better idea as it avoids SQL injection attacks as well.
VB
cmd = New SqlCommand("select * from staffpresentee where [date]=@DT", cn)
cmd.Parameters.AddWithValue("@DT", DateTime.Now.Date)
dr = cmd.ExecuteReader()
 
Share this answer
 
Comments
Shraa1 18-Mar-13 10:19am    
Thanks a lot the code was very much helpful..
thanks a lot...
OriginalGriff 18-Mar-13 10:35am    
You're welcome!
Shraa1 18-Mar-13 10:21am    
the 2 points you made, i did consider the first point when i first got the error... i tried changing the name of the column but it still showed the same error... so i thought i'd just keep the name of the field "date"
OriginalGriff 18-Mar-13 10:38am    
Don't - try to avoid calling columns "name", "Date" or "password" because they are reserved words and it's too easy to forget the "[" and "]" and get an error - it is also generally easier to read code that talks about "startDate" or "birthDate" rather than a straight "date". And easier to not make mistakes and put data in the wrong table, too... :laugh:
Shraa1 18-Mar-13 10:25am    
and dont forget to vote for the question :)

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