problem in selecting Date column and displaying in datagridview

raags

New member
Joined
Jan 4, 2008
Messages
1
Programming Experience
Beginner
hi...
plzz help me out....
very urgent..
i need to display all the records which are stored in the database today(date)
but there is some problem ..when i do the following it says..no row at that position...........where shiftDate is a date/time column in the Bookings table

VB.NET:
Dim dt As Date = Format(Today.Date, "d")

        Try

Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * from Bookings where ShiftDate =" + dt + "", con)

            Dim ds As DataSet = New DataSet()
            da.Fill(ds, "Bookings")
            DataGridView1.DataSource = ds.Tables(0).DefaultView
       
 Catch ex As Exception
            MsgBox(ex.Message)
        End Try
 
VB.NET:
Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * from Bookings where ShiftDate = @ShiftDate", con)

da.SelectCommand.Parameters.AddWithValue("@ShiftDate", Date.Today)

Dim tbl As New DataTable

da.Fill(tbl)
Me.BindingSource1.DataSource = tbl
Me.DataGridView1.DataSource = Me.BindingSource1
You need to have added a BindingSource to your form too.
 
Read the DW2 link, section Createing a Simple Data Application to find out how to downlaod data from a database and display it in a grid.

Bear in mind that a date is a point in time, TIME being the important word! if you select all records where the record date = some specific moment in time then you wont get any records unless the time matches too. Either strip the time portion off the date when you store it in the table, when you run the query or run a query on a range of dates
 
Back
Top