Query runs when ran against DB in server explorer but returns no rows in code

TyB

Well-known member
Joined
May 14, 2009
Messages
102
Programming Experience
3-5
This statement runs correctly when I open a new query on the DB in server explorer.

VB.NET:
SELECT ID, [Program Title], [Contact Hours], [Start Date], [Expiration Date], Presenter, [Type of Program], Method FROM Programs WHERE ([Expiration Date] BETWEEN ? AND ?)

Now when I run the following code in my code no rows return.

VB.NET:
    Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
        'Fill the table with the items that expire with in the month and year the user picks in the datetimepicker
        Dim dtExpired As Date
        Dim dtbegin As Date
        Dim dtend As Date
        Try
            'Get the date the user picked.
            dtExpired = DateTimePicker1.Value

            'Now get the month and year to be used as the begin date
            dtbegin = dtExpired.Month & "/1/" & dtExpired.Year

            dtend = dtbegin.AddMonths(1)

            cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Temp Projects\PSNA Application\PSNA\PSNA\bin\PSNA Application Expiration.accdb;")
            'provider to be used when working with access database
            cn.Open()
            cmd = New OleDbCommand("SELECT ID, [Program Title], [Contact Hours], [Start Date], [Expiration Date], Presenter, [Type of Program], Method FROM Programs WHERE ([Expiration Date] BETWEEN ? AND ?)", cn)

            cmd.Parameters.Add(New OleDbParameter("?", dtbegin))
            cmd.Parameters.Add(New OleDbParameter("?", dtend))
            dr = cmd.ExecuteReader

            If dr.HasRows = True Then
                'Load the datagrid
                DataGridView1.DataSource = dr
            End If
        Catch ex As Exception
            Dim strerror As String
            strerror = ex.Message.ToString

            System.Diagnostics.Debug.WriteLine(strerror)
        End Try
        dr.Close()
        cn.Close()

    End Sub

Any thoughts?

Thanks,
Ty
 
This got it

I changed part of the code to this and it works.

VB.NET:
cmd = New OleDbCommand("SELECT * FROM Programs WHERE ([Expiration Date] BETWEEN @date1 AND @date2)", cn)

            cmd.Parameters.Add(New OleDbParameter("@date1", OleDbType.Date)).Value = dtbegin
            cmd.Parameters.Add(New OleDbParameter("@date2", OleDbType.Date)).Value = dtend

Ty.
 
Back
Top