Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello Guys! i want to know how to select data between two dates i am using query but i get null data please help me

What I have tried:

DataSet dsa = new DataSet();
DataTable dt1 = new DataTable();
dsa.Tables.Add(dt1);
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date],[Receipt No],[Delivery Person] from [Total] Where [Date] between " + dateTimePicker2.Value.ToShortDateString() + " AND #" + dateTimePicker3.Value.ToShortDateString() + "#", VCON);
da.Fill(dt1);
Posted
Updated 20-May-19 23:30pm

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Use the debugger to see what is the query you really have.
You will see that the second date is embedded in "#" and the first is not, This is enough to make the query fail.
Also make sure the 2 dates as strings are in the expected format.

Otherwise, building the query the way you do is a bad idea because it open the door to sql injection.
SQL Injection[^]
 
Share this answer
 
try this

C#
da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date],[Receipt No],[Delivery Person] from [Total] Where [Date] between #" + dateTimePicker2.Value.ToShortDateString() + "# AND #" + dateTimePicker3.Value.ToShortDateString() + "#", VCON);
 
Share this answer
 
Comments
Hannan qureshi 6-Sep-22 8:04am    
Hello Guys! i want to know how to select data between two dates without sql/mysql
Karthik_Mahalingam 15-Dec-22 3:43am    
without sql/mysql meaning c# code ?
If you would post your schema and some sample data that would help. Without seeing your database, which i don't have access to, i am making some likely guesses as to the root cause.

But to clarify i'll share an example you can run in sql server (i don't have access sorry).

This example is more to show the issue of time stamps in your sql query.

SQL
DECLARE @Dates TABLE 
(
 Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
 DateExample DATETIME NULL
);

INSERT INTO @Dates ( DateExample ) VALUES ('2016-09-08 20:30:32.843')
INSERT INTO @Dates ( DateExample ) VALUES ('2016-09-08 19:00:32.843')
INSERT INTO @Dates ( DateExample ) VALUES ('2016-09-08 18:30:32.843')
INSERT INTO @Dates ( DateExample ) VALUES ('2016-09-08 16:00:32.843')
INSERT INTO @Dates ( DateExample ) VALUES ('2016-09-08 15:00:32.843')
INSERT INTO @Dates ( DateExample ) VALUES ('2016-09-08 14:30:32.843')

--This returns nothing due to the time stamps being midnight and the time stamps in the data being
SELECT * FROM @Dates WHERE DateExample BETWEEN '2016-09-08 00:00:00.000' AND '2016-09-08 00:00:00.000'

--Returns results because it converts dateexample to a date, which makes the timestamps midnight.
SELECT * FROM @Dates WHERE CAST(DateExample AS DATE) BETWEEN '2016-09-08 00:00:00.000' AND '2016-09-08 00:00:00.000'


The first select statement takes into account time stamps, and since your are stripping your times off of your dates being passed in , your comparing a date at 12:00am to other dates with a time stamp which is likely the cause of you not having any data returned.

The other issue that might be arising is you are converting a date to a string and that may not jive well with access.

The second query converts the dateexample column to date which makes the timestamp 12:00am and works with that query.

Given that T-SQL doesn't convert well to MS Access you have 2 options

1) Make sure your dates in your BETWEEN clause include time stamps that meet the criteria of what you are looking for. You may need to append 00:00:00 and 24:59:59.59 to your start and end dates of your BETWEEN statement in order to get all valid data.

2) I think MS Access may have functions that convert strings to date and in turn strip the time stamp off of it. I don't have MS access installed so i can't test this but I think this is possible from a quick google search.

ms access CAST - Google Search[^]


Again, i don't have access to your DB so this may be off. If it is then feel free to provide schema and clarify your issue.
 
Share this answer
 
v2
using System.Data.SqlClient;

Connection.CommandText = "Select * form table where Date between @stdate and @ldate;
Connection.Parameters.Addwithvalue("@stdate","startdate");
Connection.Parameters.Addwithvalue("@ldate","todate");
 
Share this answer
 
Comments
Richard Deeming 21-May-19 9:49am    
An unformatted, unexplained code snippet, with confusing variable names, incorrect parameter values, and which won't compile due to an obvious syntax error, is not a solution.

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