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?
 
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 & "';")
 
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.
 
Back
Top