views:

43

answers:

1

I have a table that has two time columns. One is a login time one is a logout time. I would like to be able to filter between the two, basically greater than the minimum selected and less than the maximum selected. I am using a monthly calendar to get me the spans of time but it is not working correctly. From there I would like to add the 4 columns in the table to a gridview. The date in the DB is the in following format:

2/23/2010 11:17:01 AM

I know how to get single elements from the table, or a column but not entire rows.

So i guess I have 2 problems, getting the filter for the SQL correct then binding the results to a table.

Here is my code so far:

 Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim str As String
    Dim dr As OleDbDataReader
    Dim date1 As Date
    Dim date2 As Date


    If (Not SubmitNewToDB(session)) Then
        MsgBox("error")
    End If
    Try
        cn = New OleDbConnection("Provider=microsoft.Jet.OLEDB.4.0;Data Source=G:\Sean\BMSBonder3_0.mdb;")
        cn.Open()
        str = "Select BonderIdentifier, UserName, Login, Logout From [Session] Where (Login < " & MonthCalendar1.SelectionEnd _
            & " AND Logout > " & MonthCalendar1.SelectionStart & ") AND BonderIdentifier = " & session.bonderIdentifier

        cmd = New OleDbCommand(str, cn)
        dr = cmd.ExecuteReader

        While dr.Read()
            If dr.Item(0).ToString <> "" Then
                DataGridView1.Rows.Add(dr.Item(0))
            End If
        End While
        dr.Close()
        cn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

EDIT

SLaks has answered most of my question, one problem remains. I need to figure out how to make it so the monthcalendar can select just one day and return that days values. Seems like the time is an issue since it is in the DB as stated above. Just comparing the dates is not enough.

Not sure how to adjust the date to include the time.

+2  A: 

You should be using parameters, like this:

str = "Select BonderIdentifier, UserName, Login, Logout From [Session] Where Login >= ? AND Login <= ? AND BonderIdentifier = ?"

cmd = New OleDbCommand(str, cn)
cmd.Parameters.AddWithValue("Start", MonthCalendar1.SelectionStart
cmd.Parameters.AddWithValue("End", MonthCalendar1.SelectionEnd)
cmd.Parameters.AddWithValue("BID", session.bonderIdentifier)

To add values for the four columns, you can write

DataGridView1.Rows.Add(dr.Item(0), dr.Item(1), dr.Item(2), dr.Item(3))
SLaks
OleDb needs ordered ? for parameter placeholders rather than named @parameters.
Joel Coehoorn
Thanks, but my filter still does not work correctly.
Sean P
I edited the answer; try it now.
SLaks
I get this error: No row can be added to a DataGridView control that does not have columns. Columns must be added first.
Sean P
I could add them maunally to the collection correct?
Sean P
You need to add four columns to the DataGridView using the designer.
SLaks
I did that. Problem with the filter. I can only get the results when i pick the previous day and the day after. So if i chose just the 23rd for example, to get teh right results I would have to chose the 22nd and 24th. Is there a way to adjust the date so that I can account for the time problem as well?
Sean P
You need to check for greater than or equal to. I updated the SQL.
SLaks
That doesnt seem to work... also you changed the parameters to "?". Curious to why
Sean P
Oh i see, Ordered placeholders, BUT the single day selection does not work. Im thinking the time has to do with it. The DB has a time attached to it where the selectionStart and SelectionEnd do not.
Sean P
What timezones are the dates?
SLaks
Pacific time zone
Sean P