SQL - Between Command - Date range

kelum2000

New member
Joined
Oct 27, 2020
Messages
3
Programming Experience
Beginner
Hi,
VB.net 2010, Windows 7

I need to filter records according date Range from mysql table. I tried to use following command.

Public DtRpt As New DataTable("DTReport")
DtRpt = Nothing
DtRpt = GetDataFromTable("SELECT * FROM tblTransaction WHERE TransDate BETWEEN '" & DTPFrom.Value & "' AND '" & DTPTO.Value & "';")
DGViewMaster.DataSource = DtRpt

Program Running. No errors coming. But not viewing Records.
Please tell me what is the Problem?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
Firstly, what's the point in creating a new DataTable here:
VB.NET:
Public DtRpt As New DataTable("DTReport")
if you're just going to throw it away and replace it here:
VB.NET:
DtRpt = GetDataFromTable("SELECT * FROM tblTransaction WHERE TransDate BETWEEN '" & DTPFrom.Value & "' AND '" & DTPTO.Value & "';")
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
As for the issue, if there really is no exception thrown then there are simply no records matching your filters. Of course, there's no guarantee that your filters actually are what you you intend them to be. You should absolutely not be using string concatenation to insert values into SQL code at all for a number of reasons and potential issues with date formatting is one of them. Always use parameters. If you need to rewrite that method to enable that, you should do so.

By the way, that method is poorly named. Based on how you're calling it, it simply executes an arbitrary query, which might involved a single table or multiple or none.
 
Top Bottom