Click here to Skip to main content
15,885,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear,

I want to access dbase database and fill in DataGridView in .Net. When i tried this code with passing date value in where Claus not filtering, but without where Claus its working fine. Please help on this

Thanks
Basit

VB
Try
    Dim ConnectionString As String
    ConnectionString = "Provider=vfpoledb;Data Source=D:\ATTEND.DBF;Collating Sequence=machine;"
    Dim dBaseConnection As New System.Data.OleDb.OleDbConnection(ConnectionString)
    dBaseConnection.Open()
    Dim dataadapter As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM ATTEND.DBF Where date between #" & Format(Today.Date, "mm/dd/yy") & "# and #" & Format(Today.Date, "mm/dd/yy") & "#", dBaseConnection)
    'Dim dataadapter As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM ATTEND.DBF", dBaseConnection)
    Dim ds As New DataSet()
    dataadapter.Fill(ds, "ATTEND")
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "ATTEND"
    dBaseConnection.Close()
Catch ex As Exception

End Try
Posted
Comments
Richard Deeming 19-May-15 10:03am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

The problem is that dBase/xBase doesn't support "between" for SQL dates

Try this:

Quote:
date = #" & Format(Today.Date, "mm/dd/yy") & "#
 
Share this answer
 
Comments
Richard Deeming 19-May-15 10:04am    
You've copied the SQL Injection[^] vulnerability from the question.
To fix the SQL Injection[^] vulnerability in your code, use a parameterized query:
VB.NET
Using dBaseConnection As New OleDbConnection(ConnectionString)
    Using dBaseCommand As New OleDbCommand("SELECT * FROM ATTEND.DBF Where date = ?", dBaseConnection)
        dBaseCommand.Parameters.AddWithValue("p0", DateTime.Today)
        
        Dim ds As New DataSet()
        Dim dataadapter As New OleDbDataAdapter(dBaseCommand)
        dataadapter.Fill(ds, "ATTEND")
        
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "ATTEND"
    End Using
End Using
 
Share this answer
 
Comments
basitsar 20-May-15 0:18am    
Thanks A lot Great.

Thanks
Basit

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