Rajiv Choudhary
Rajiv Choudhary

Reputation: 141

Check Date Exist or not in vb.net

I write a sql statement into vb.net to fetch data from msaccess where date equal to patepicker control. But here syntax error. My code given below:-

 Dim ct As String = "select * from add_student where _Date <= #" & dtDate1.ToString("MM/dd/yyyy") & "#"
        cmd = New OleDbCommand(ct)
        cmd.Connection = con
        rdr = cmd.ExecuteReader()

Note:- _Date column had Date/Time Data type in ms access database Please suggest me, Where is mistake

Upvotes: 0

Views: 353

Answers (2)

Gustav
Gustav

Reputation: 55816

Your issue may be that "/" is not a slash but the localized date separator.

Thus, force slashes:

Dim ct As String = "select * from add_student where _Date <= #" & dtDate1.ToString("MM'/'dd'/'yyyy") & "#"

and perhaps brackets for the weird field name:

Dim ct As String = "select * from add_student where [_Date] <= #" & dtDate1.ToString("MM'/'dd'/'yyyy") & "#"

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460098

You should always use parameters:

dim sqlQuery ="Select * from add_student where _Date = ?"
using conn = New OleDbConnection("connectionstring")
    conn.open()
    Using selectCmd As New OleDbCommand(sqlQuery, conn)
        selectCmd.Parameters.Add("_Date", OleDbType.Date).Value = dtDate1
        using rdr = selectCmd.ExecuteReader()
            while rdr.read()

            End While
        End Using
    End Using
End Using

Upvotes: 1

Related Questions