Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody

How can I find records between 2 date values?

My meaninng:
I have 2 text boxes and I fill them like textbox1 = 1988/5/4 and textbox2 = 2010/5/5.
When I click on button SEARCH I would like to show all records between these date.

Operator ">" "<" is not work for date.

Please help
Posted
Updated 12-Nov-11 1:05am
v2

1 solution

Why are you using TextBoxes and not DateTimePickers?
A TextBox will fail in many cases. Some people use different date formats (dd-MM-yyyy where I come from). Some people have different database settings for dates, it's just not going to work.
A better approach is the following:
VB
Dim cmd As New OleDbCommand("SELECT WhatEverYouNeed FROM MyTable Where MyDateColumn BETWEEN @Date1 AND @Date2", connection)
cmd.Parameters.Add("@Date1", DateTimePicker1.Value)
cmd.Parameters.Add("@Date2", DateTimePicker2.Value)
' Open connection, run the cmd query and read results.

How to use Commands, Connections, Adapters, Readers and DataSets etc. can be found here[^].
Alternatively you can use an ORM such as Entity Framework[^] and use LINQ[^] to query against your database.
 
Share this answer
 
v2
Comments
synctor 12-Nov-11 7:11am    
i cant use datetimepicker, Beacuse our Date is Different of You !
We dont use Gregorian Date ! our Date is Solar . have you any way to it ?
Sander Rossel 12-Nov-11 10:21am    
A what date now? Is it this calendar? http://en.wikipedia.org/wiki/Iranian_calendars
I never heard of it before, so obviously I cannot help you with that. Your best bet is probably to search Google for a custom Control that handles your calendar...
You could possibly make your own Control. But in that case having a TextBox that validates the format of your date is probably easier.
synctor 12-Nov-11 23:45pm    
Yeah it is ! Now 1390/8/22
Yes textbox is the best at this time
SubrahmanyamGurram 16-Jul-12 3:38am    
Hi Naerling, Can you suggest me a control for reading the date, which is optional and user will enter the the date in "DD-MM-YYYY" format and I need to save the date into database in "YYYY-MM-DD" format. Please send me C#.Net code to do this.

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