Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How to show data in listview1 between two dates. Showing Some ERROR
Please help me...........



Error
https://ibb.co/hOOgPa

Listview
https://ibb.co/gyjo4a

Database
https://ibb.co/eTJ4cv

What I have tried:

VB
Public Sub Displayitemrpt()
        If cn.State = ConnectionState.Open Then
            cn.Close()
        End If
        cn.Open()
        Dim cmd As New OleDb.OleDbCommand("SELECT * FROM pur_inv,inv_type, party_ldg WHERE pur_inv.partyID=party_ldg.partyID and pur_inv.invtypid=inv_type.invtypid and purinvdt BETWEEN '" & txtdtf.Text & " And " & txtdtt.Text & "' ORDER BY purinvdt  ", cn)
        Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader()
        ListView1.Items.Clear()
        Do While dr.Read()
            Dim new_item As New  _
                     ListViewItem(dr.Item("purinvdt").ToString)
            new_item.SubItems.Add(dr.Item("purinvid").ToString)

            new_item.SubItems.Add(dr.Item("invno").ToString)
            new_item.SubItems.Add(dr.Item("invdt").ToString)

            new_item.SubItems.Add(dr.Item("prtynm").ToString)
            new_item.SubItems.Add(dr.Item("invtyp").ToString)
            new_item.SubItems.Add(dr.Item("taxamt").ToString)
            new_item.SubItems.Add(dr.Item("tottaxblamt").ToString)
            new_item.SubItems.Add(dr.Item("invamt").ToString)
            ListView1.Items.Add(new_item)
        Loop
        cn.Close()
    End Sub
Posted
Updated 10-Jul-17 3:59am
v2
Comments
Michael_Davies 10-Jul-17 9:49am    
Your dates and the AND are in quotes so treated as a single entity;

and purinvdt BETWEEN '" & txtdtf.Text & " And " & txtdtt.Text & "' ORDER BY

You open the quote after the BETWEEN and close it after the second date, whereas it ought possibly to be;

and purinvdt BETWEEN '" & txtdtf.Text & "' And '" & txtdtt.Text & "' ORDER BY

Two things:
1) 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. Use Parametrized queries instead.
2) Never trust user input: parse your dates into DateTime values (reporting problems to the user) and pass the DateTime values to SQL
VB
Dim dtFrom as DateTime
If Not DateTime.TryParse(txtdtf.Text, dtFrom) Then
	' Report problem
        ...
	Return
End If
Dim dtTo as DateTime
If Not DateTime.TryParse(txtdtt.Text, dtTo) Then
	' Report problem
        ...
	Return
End If

Dim cmd As New OleDb.OleDbCommand("SELECT * FROM pur_inv,inv_type, party_ldg WHERE pur_inv.partyID=party_ldg.partyID AND pur_inv.invtypid=inv_type.invtypid AND purinvdt BETWEEN @DF AND @DT ORDER BY purinvdt", cn)
cmd.Parameters.AddWithValue("@DF", dtFrom)
cmd.parameters.AddWithValue("@DT", dtTo)
...
 
Share this answer
 
Comments
Jayanta Modak 11-Jul-17 2:05am    
Hello sir It is work fine, But a problem date show with time how i can show only date without time please help me.
https://ibb.co/iYyxfF
OriginalGriff 11-Jul-17 2:18am    
Use the format options on the ToString for your date-based items:

https://www.codeproject.com/Tips/54577/Formatting-a-DateTime-for-display-format-string-de
Jayanta Modak 11-Jul-17 4:12am    
WHERE I USED THE DATE FORMAT (WHICH LINE)
OriginalGriff 11-Jul-17 4:27am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously - or if you want help from people.
Jayanta Modak 11-Jul-17 4:41am    
I am sorry. unfortunately caps lock button on in my keyboard I have no intention to do this. Again I am very sorry. And I am newer in vb.net 1st time I used the vb.net so, I don't know the where I use the format.
Thanks for your Reply and suggestion
Please forgive me, sorry
Never build queries in strings! This mistake is common AND it leave you open to SQL Injection! This is a bad thing.

First off, This is your mistake:
VB
"'" & txtdtf.Text & " And " & txtdtt.Text & "'"

this evaluates to
VB
"'mytest And myother'"


See, you missed some "'"'s

Use parameters instead. This kind of issue wouldn't occur!
OleDbCommand.Parameters Property[^]


Here's a note on SQL Injection:
SQL Injection[^]
 
Share this answer
 
v2

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