Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi brothers , thanks for your time and help.

i want to search data from access database between two dates in vb.net (and put search data into ListView) using
two datetimepickers .

please help me !! 


When i execute this following error show .....

Syntax error (missing operator) in query expression '(billdate = '06/01/2022') and between(billdate = '06/01/2022')'.


What I have tried:

Dim d1 As Date
        Dim d2 As Date
        ListView1.Items.Clear()
       
        cm = New OleDb.OleDbCommand
        With cm
            .Connection = cn
            .CommandType = CommandType.Text
            date1.Value = Convert.ToDateTime(date1.Value.Date)
            date2.Value = Convert.ToDateTime(date2.Value.Date)
            d1 = date1.Value
            d2 = date2.Value
            .CommandText = "SELECT  billnumber,govtamount,selfamount,profit from billinfo WHERE (billdate = '" & date1.Text & "') AND BETWEEN (billdate = '" & date2.Text & "')"

         


        End With
        dr = cm.ExecuteReader
        Do While dr.Read()
            Item = ListView1.Items.Add(dr("billnumber").ToString())
            Item.SubItems.Add(dr("govtamount").ToString())
            Item.SubItems.Add(dr("selfamount").ToString())
            Item.SubItems.Add(dr("profit").ToString())


        Loop
        dr.Close()
        If ListView1.Items.Count = 0 Then
            lblinfo.Text = " ***** ATTENTION ***** Bill Information Not Found by Date !" & " " & txtroznamalist.txttimer.Text
            ListView1.Items.Clear()
            txtprofit.Text = ""
            txtselfamount.Text = ""
            txttotalbill.Text = ""
            txtuscamount.Text = ""

        Else
            lblinfo.Text = " ***** ATTENTION ***** Bill Information  Found by Date ! " & " " & txtroznamalist.txttimer.Text

        End If
Posted
Updated 5-Jan-22 18:45pm
v2
Comments
Member 15490519 6-Jan-22 2:49am    
.CommandText = "SELECT billnumber,govtamount,selfamount,profit from billinfo WHERE (billdate = '" & date1.Text & "') AND (billdate = '" & date2.Text & "')"
Member 15490519 6-Jan-22 2:51am    
.CommandText = "SELECT billnumber,govtamount,selfamount,profit from billinfo WHERE (billdate = '" & date1.Value.Date & "') AND (billdate = '" & date2.Value.Date & "')"
Member 15490519 6-Jan-22 3:10am    
.CommandText = "SELECT * FROM billinfo WHERE billdate >= @dt1 AND billdate <= @dt2"

.Parameters.AddWithValue("@dt1", date1.Text.Trim)
.Parameters.AddWithValue("@dt2", date2.Text.Trim)

Quote:
When i execute this following error show .....
Syntax error (missing operator) in query expression '(billdate = '06/01/2022') and between(billdate = '06/01/2022')'.

What about reading documentation ?
A simple google search on "SQL between" gives 200+ millions results.
second result is : SQL BETWEEN Operator[^]

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Why are you taking a DateTime value, and converting it to a DateTime, then converting it to Text via other DateTime values you subsequently ignore?
date1.Value = Convert.ToDateTime(date1.Value.Date)
date2.Value = Convert.ToDateTime(date2.Value.Date)
d1 = date1.Value
d2 = date2.Value
Then you convert that to text to concatenate strings to form an SQL command - which is a bad idea at the best of times - and write bad SQL to hold it?

So ... first off: 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. Always use Parameterized 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?

Secondly: the SQL syntax for BETWEEN is nothing like what you have there: SQL BETWEEN Operator[^]

Thirdly, pass the DateTime.Date value directly to the DB via a parameter and there is no need for text at either end, so no possibility for misinterpreting date values: is 01/02/03 the 1st Feb 2003, 2nd Jan 2003, or 3rd Feb 2001? Pass it as a DateTime and teh DB already knows what the user wanted!
 
Share this answer
 
Your SQL is incorrect. Correct SQL is

select  billnumber,govtamount,selfamount,profit 
from billinfo 
WHERE billdate Between #" & date1.Text & "# And #" & date2.Text & "#";


Final Query Should Look like this

select  billnumber,govtamount,selfamount,profit 
from billinfo 
WHERE billdate Between #1/1/2022# And #6/1/2022#
 
Share this answer
 
Comments
Member 15490519 6-Jan-22 0:50am    
Sorry for this. When i execute, All data from database load into Listview. I want only get specific date record.
Member 15490519 6-Jan-22 2:47am    
Nothing found.
_Asif_ 6-Jan-22 2:54am    
What is the final query you getting?

try executing the same query on MS Access query window. May be your date settings are different. it could be mm/dd/yy instead of dd/mm/yy
Member 15490519 6-Jan-22 3:03am    
My data query is "dd/MM/yyyy"
Richard Deeming 6-Jan-22 4:53am    
You haven't addresses the critical security vulnerability in the OP's code. See the other solutions for the correct way to solve 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