Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to save a users time data in database between two dates then be printed in crystal reports but I only get one date instead of all the dates that he/she have. What am I missing in my code:

VB
Public Sub getLogs(ByVal id As Integer, ByVal fromDate As Date, ByVal toDate As Date)
        Dim startDay, endDay As Date
        conn1 = New OleDbConnection
        conn1.ConnectionString = conCopy
        conn4 = New OleDbConnection
        conn4.ConnectionString = conCopy
        Dim query, query2 As String

        Try
            conn1.Open()
            query = "SELECT USERID,CHECKDATE," _
            & "MIN(IIF(CHECKTYPE='Check In',CHECKTIME,NULL) ) AS 'AM IN', " _
            & "MIN(IIF(CHECKTYPE='Break Out',CHECKTIME,NULL) ) AS 'LUNCH OUT', " _
            & "MAX(IIF(CHECKTYPE='Break In',CHECKTIME,NULL) ) AS 'LUNCH IN', " _
            & "MAX(IIF(CHECKTYPE='Check Out',CHECKTIME,NULL) ) AS 'PM OUT' " _
            & "FROM CHECKINOUT WHERE USERID = @ID " _
            & "GROUP BY USERID, CHECKDATE"

            cmd = New OleDbCommand(query, conn1)
            cmd.Parameters.Clear()
            cmd.Parameters.AddWithValue("@ID", Integer.Parse(id))
            
            Dim reader As OleDbDataReader = cmd.ExecuteReader

            conn4.Open()
            query2 = "INSERT INTO LOGDATES(ID,CHECKDATE,AMIN,LUNCHOUT,LUNCHIN,PMOUT) " _
            & "VALUES(@ID,@DATE,@AM,@LOUT,@LIN,@PM)"
            cmd2 = New OleDbCommand(query2, conn4)

            startDay = DateTime.Parse(fromDate)
            endDay = DateTime.Parse(toDate)

            If reader.HasRows Then

                While reader.Read
                    Do While startDay <= endDay
                        With cmd2
                            .Parameters.Clear()
                            .Parameters.AddWithValue("@ID", reader.Item(0))
                            .Parameters.AddWithValue("@DATE", DateTime.Parse(startDay).ToString("MM/dd/yyyy"))

                            If DateTime.Parse(startDay).ToString("MM/dd/yyyy") = DateTime.Parse(reader.Item(1)).ToString("MM/dd/yyyy") Then

                                If Not IsDBNull(reader.Item(2)) Then
                                    .Parameters.AddWithValue("@AM", DateTime.Parse(reader.Item(2)).ToString("HH:mm tt"))
                                Else
                                    .Parameters.AddWithValue("@AM", DBNull.Value)
                                End If

                                If Not IsDBNull(reader.Item(3)) Then
                                    .Parameters.AddWithValue("@LOUT", DateTime.Parse(reader.Item(3)).ToString("HH:mm tt"))
                                Else
                                    .Parameters.AddWithValue("@LOUT", DBNull.Value)
                                End If

                                If Not IsDBNull(reader.Item(4)) Then
                                    .Parameters.AddWithValue("@LIN", DateTime.Parse(reader.Item(4)).ToString("HH:mm tt"))
                                Else
                                    .Parameters.AddWithValue("@LIN", DBNull.Value)
                                End If

                                If Not IsDBNull(reader.Item(5)) Then
                                    .Parameters.AddWithValue("@PM", DateTime.Parse(reader.Item(5)).ToString("HH:mm tt"))
                                Else
                                    .Parameters.AddWithValue("@PM", DBNull.Value)
                                End If

                            Else
                                .Parameters.AddWithValue("@AM", DBNull.Value)
                                .Parameters.AddWithValue("@LOUT", DBNull.Value)
                                .Parameters.AddWithValue("@LIN", DBNull.Value)
                                .Parameters.AddWithValue("@PM", DBNull.Value)
                            End If

                            .ExecuteNonQuery()
                        End With

                        startDay = startDay.AddDays(1)
                    Loop
                End While

            Else
                Do While startDay <= endDay
                    With cmd2
                        .Parameters.Clear()
                        .Parameters.AddWithValue("@ID", Integer.Parse(id))
                        .Parameters.AddWithValue("@DATE", DateTime.Parse(startDay).ToString("MM/dd/yyyy"))
                        .Parameters.AddWithValue("@AM", DBNull.Value)
                        .Parameters.AddWithValue("@LOUT", DBNull.Value)
                        .Parameters.AddWithValue("@LIN", DBNull.Value)
                        .Parameters.AddWithValue("@PM", DBNull.Value)
                        .ExecuteNonQuery()
                    End With

                    startDay = startDay.AddDays(1)
                Loop
            End If

            conn1.Close()
            conn4.Close()
            reader.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Form2.Show()

    End Sub


What I have tried:

I already checked the query in ms access and it did return all data of users from between dates so the error is within in my vb code. Please help....
Posted
Updated 29-Sep-19 16:52pm
Comments
phil.o 28-Sep-19 4:55am    
DateTime.Parse(fromDate)
Why are you trying to parse to a DateTime value something which already is a proper DateTime value? I am even surprised this code compiles, because Parse method accepts a String value, not a DateTime value.
Parse methods are for when you have a String representation of a value from which you want to get the proper value back.
Tip: you do not need to transform anything to string until you want to present the value to the end user. Always work with proper types in the first place, and avoid betting back and forth from value to string. For example, to compare two dates, compare the DateTime values directly, do not compare their string representations.

And for your question: put a breakpoint at the beginning of your method, press F5, and debug from there. Do you need help on how to use the debugger?
Richard Deeming 30-Sep-19 10:03am    
It compiles because it's VB.NET with Option Strict Off - the "just compile, damnit!" flag. :)

Without access to your DB, and your code while it is running, we can't tell you what to do.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
RaydenFox 29-Sep-19 12:25pm    
I isolate the code and run it several times. The problem that I see was the data stopped incrementing values and just loop through the dates. Can someone help me change the code so that it will also loop through data.

While reader.Read
Do While startDay <= endDay
If DateTime.Parse(startDay).ToString("MM/dd/yyyy") = DateTime.Parse(reader.Item(1)).ToString("MM/dd/yyyy") Then
MsgBox(reader.Item(0) & " " & DateTime.Parse(reader.Item(1)).ToString("MM/dd/yyyy") & " " & "Equal")
Else
MsgBox(reader.Item(0) & " " & "Not equal")
End If

startDay = startDay.AddDays(1)
Loop
For almost one week of debugging and configuring every loop, I finally solved the problem. Here is my solution:

VB
cmd3 = New OleDbCommand(query, conn1)
            cmd3.Parameters.Clear()
            cmd3.Parameters.AddWithValue("@ID", id)
            
            Dim reader As OleDbDataReader = cmd.ExecuteReader
            schemaTable = New DataTable
            Dim adapter As New OleDbDataAdapter(cmd3)
            Dim i As Integer

            adapter.Fill(schemaTable)

            conn4.Open()
            query2 = "INSERT INTO LOGDATES(ID,CHECKDATE,AMIN,LUNCHOUT,LUNCHIN,PMOUT) " _
            & "VALUES(@ID,@DATE,@AM,@LOUT,@LIN,@PM)"
            cmd2 = New OleDbCommand(query2, conn4)

            startDay = DateTime.Parse(fromDate)
            endDay = DateTime.Parse(toDate)

            If reader.HasRows Then

                While reader.Read
                    i = 0
                    Do While startDay <= endDay
                        If i <= schemaTable.Rows.Count - 1 Then
                            With cmd2
                                .Parameters.Clear()
                                .Parameters.AddWithValue("@ID", reader.Item(0))

                                'MsgBox(DateTime.Parse(schemaTable.Rows(i)(1)).Date)

                                If DateTime.Parse(startDay).Date = DateTime.Parse(schemaTable.Rows(i)(1)).Date Then

                                    .Parameters.AddWithValue("@DATE", DateTime.Parse(schemaTable.Rows(i)(1)).Date)

                                    If Not IsDBNull(schemaTable.Rows(i)(2)) Then
                                        .Parameters.AddWithValue("@AM", DateTime.Parse(schemaTable.Rows(i)(2)).ToString("HH:mm tt"))
                                    Else
                                        .Parameters.AddWithValue("@AM", DBNull.Value)
                                    End If

                                    If Not IsDBNull(schemaTable.Rows(i)(3)) Then
                                        .Parameters.AddWithValue("@LOUT", DateTime.Parse(schemaTable.Rows(i)(3)).ToString("HH:mm tt"))
                                    Else
                                        .Parameters.AddWithValue("@LOUT", DBNull.Value)
                                    End If

                                    If Not IsDBNull(schemaTable.Rows(i)(4)) Then
                                        .Parameters.AddWithValue("@LIN", DateTime.Parse(schemaTable.Rows(i)(4)).ToString("HH:mm tt"))
                                    Else
                                        .Parameters.AddWithValue("@LIN", DBNull.Value)
                                    End If

                                    If Not IsDBNull(schemaTable.Rows(i)(5)) Then
                                        .Parameters.AddWithValue("@PM", DateTime.Parse(schemaTable.Rows(i)(5)).ToString("HH:mm tt"))
                                    Else
                                        .Parameters.AddWithValue("@PM", DBNull.Value)
                                    End If
                                    i = i + 1
                                Else
                                    .Parameters.AddWithValue("@DATE", DateTime.Parse(startDay).ToString("MM/dd/yyyy"))
                                    .Parameters.AddWithValue("@AM", DBNull.Value)
                                    .Parameters.AddWithValue("@LOUT", DBNull.Value)
                                    .Parameters.AddWithValue("@LIN", DBNull.Value)
                                    .Parameters.AddWithValue("@PM", DBNull.Value)
                                End If
                                .ExecuteNonQuery()
                            End With
                        End If
                        'i = i + 1
                        startDay = startDay.AddDays(1)
                    Loop
                End While


I added another connection and fill a datatable with it and loop inside the date loop just by using the IF statement. Now it prints all of the users attendance along with the dates that he/she absents.
 
Share this answer
 
Comments
Patrice T 29-Sep-19 23:27pm    
You should accept your solution, it will make it clear that the question is solved.

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