Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am trying to search database between 2 dates that come from datetimepicker but I have got this error ( fill:selectcommand.connection property has not been initialized ). I don't know what is wrong with my code as I am still in the VB.net nursery.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim connstrg As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=M:\REGISTER.mdb;Persist Security Info=True;Jet OLEDB:Database Password=he123;"
            Dim conn As New OleDbConnection(connstrg)
            Dim DA As New OleDbDataAdapter("select* FROM reg", conn)
            Dim ds As New DataSet
            Dim com As New OleDbCommand
            Dim dd1 As Date = Me.DateTimePicker1.Value.Date

            Dim dd2 As Date = Me.DateTimePicker2.Value.Date
            conn.Open()
            com.CommandType = CommandType.Text
            com.CommandText = "select * from reg where [date] BETWEEN  & dd1 &  And  & dd2"
            DA.SelectCommand = com
            DA.Fill(ds, "reg")
            Me.DataGridView1.DataSource = ds.Tables(0)
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub
Posted
Updated 18-Nov-10 3:02am
v2

The line

com.CommandText = "select * from reg where [date] BETWEEN  & dd1 &  And  & dd2"


is wrong. It should be

com.CommandText = "select * from reg where [date] BETWEEN #" & dd1.ToString("mm/dd/yyyy") & "# And #" & dd2.ToString("mm/dd/yyyy") & "#"


Dates need to be delimited with the '#' character in queries.

Even better would be to use a parameterised query

com.CommandText = "select * from reg where [date] BETWEEN ? And ?"
com.Parameters.AddWithValue("@StartDate", dd1)
com.Parameters.AddWithValue("@EndDate", dd2)


which would save all the fiddling about with getting the dates into the correct format in the query.
 
Share this answer
 
Comments
edriso 19-Nov-10 4:03am    
thanks Geoff it works fine with the parameterised query, thank you
Hi
I have added on line
com.Connection = conn

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim connstrg As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=M:\REGISTER.mdb;Persist Security Info=True;Jet OLEDB:Database Password=he123;"
            Dim conn As New OleDbConnection(connstrg)
            Dim DA As New OleDbDataAdapter("select* FROM reg", conn)
            Dim ds As New DataSet
            Dim com As New OleDbCommand
            Dim dd1 As Date = Me.DateTimePicker1.Value.Date

            Dim dd2 As Date = Me.DateTimePicker2.Value.Date
            conn.Open()
            com.CommandType = CommandType.Text
            com.CommandText = "select * from reg where [date] BETWEEN  & dd1 &  And  & dd2"
            --------------------------------------------------
            --Imdad was added
            com.Connection = conn
            --end 
            --------------------------------------------------
            DA.SelectCommand = com
            DA.Fill(ds, "reg")
            Me.DataGridView1.DataSource = ds.Tables(0)
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub



Please do let me know, if you have any doubt.

Please provide "Vote":thumbsup: if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen
 
Share this answer
 
v2
Comments
#realJSOP 18-Nov-10 9:42am    
Don't beg for votes.
edriso 18-Nov-10 10:23am    
hi
thanks for your replay, i did add this line but i got another ex.message
(syntax error (missing operator) in query expression.
Sunasara Imdadhusen 19-Nov-10 0:05am    
But why?

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