Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all...

i have table with columns
description,user,date(DATE),time(TIME)

i written Query Like:
SQL
SELECT * FROM `tbluser` where `date`<='" + DateTime.Now.ToString("yyyy-MM-dd") + "' and `time`<='" + DateTime.Now.ToString("hh:mm") + "' and user='" + txtuser.text + "'


but it wont fetch records less than the time how can i fetch records less then the correct date and time can any one please help me
thanks in advance...
Posted
Updated 14-Dec-11 18:57pm
v5

1 solution

Don't concatenate literals (date, text etc) to your SQL statements. Instead use MySqlParameter[^].

When that is done, set the proper datetime to the parameter as value and you should get correct results.

Addition:

Currently you have a query like:
SQL
SELECT * FROM `tbluser` where `date`<='" + DateTime.Now.ToString("yyyy-MM-dd") + "' and `time`<='" + DateTime.Now.ToString("hh:mm") + "' and user='" + txtuser.text + "'"

What you should do is to use parameter (see the link I provided) so your query would look something like:
SQL
SELECT * FROM `tbluser` where `date`<= @date and `time`<=@time and user=@user"

After that modification you define proper values for all of the three parameters via C# (as in the example).

I'm not sure but it looks like you may actually store dates and times in the database as character information. If that is true, then you should correct the table structure and define a single datetime field in your table and use that instead. See: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html[^]

If that is done your query could look something like:
SQL
SELECT * FROM `tbluser` 
where datetimefield <= @datetimetosearch 
and user=@user"

That would eliminate the problems for having the date and time information in separate fields and possibly conversion problems if you currently use character data type.
 
Share this answer
 
v2
Comments
tulasiram3975 15-Dec-11 1:11am    
i Did't Get Anything From Your Answer can You Please Clarify Clearly. i dont have any problem with inserting data....
Wendelius 15-Dec-11 3:43am    
Answer updated
tulasiram3975 15-Dec-11 4:27am    
Thank You Sir.....
Wendelius 15-Dec-11 4:41am    
You're welcome :)

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