Solved

How to solve Exception of Type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll  Additional information: Cast from string "6/1/2010 7:51:00 AM" to type 'Long' is not valid.

Posted on 2010-08-16
6
230 Views
Last Modified: 2012-05-10
Hello All,

I am trying to solve this error please help me with that. I am getting this exception for both long double. I am writing the code which is giving me this error please let me know if something is wrong with the code.

Thank you
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim i As Integer
        Dim w As Integer
        Dim Absent As Integer
        Dim Holiday As Integer
        Dim StartTime As Long
        Dim EndTime As Long
        Dim HoursWorked As Long
        Dim TimesLate As Integer
        Dim OnTime As Integer
        Dim PunchTime As String
        Dim lv As ListViewItem
        Dim data_reader As OleDbDataReader
        Dim ScheduledTime As Date
        Dim TempPunchTime As Date
        Dim count1 As Integer = 0
        Dim count2 As Integer = 0

        Me.OleDbConnection2.Open()

        For i = 0 To Me.ListBox2.Items.Count - 1
            lv = Me.ListView2.Items.Add(Me.ListBox2.Items.Item(i))
            lv.SubItems.Add("")
            lv.SubItems.Add("")
            lv.SubItems.Add("")
            lv.SubItems.Add("")
            lv.SubItems.Add("")
            lv.SubItems.Add("")

            Absent = 0
            Holiday = 0
            HoursWorked = 0
            TimesLate = 0
            OnTime = 0

            For w = 0 To Me.ListView1.Items.Count - 1
                ' MsgBox(TrimEndSpaces(Me.ListView1.Items(w).SubItems(0).Text))
                ' MsgBox(Me.ListView2.Items.Item(i).SubItems(0).Text)
                If TrimEndSpaces(Me.ListView1.Items(w).SubItems(0).Text) = TrimEndSpaces(Me.ListView2.Items.Item(i).SubItems(0).Text) Then

                    'Count Absent days
                    If TrimEndSpaces(Me.ListView1.Items(w).SubItems(32).Text) = "" Then
                        Absent += 1
                    End If
                    If TrimEndSpaces(Me.ListView1.Items(w).SubItems(32).Text) = "SICK" Then
                        Absent += 1
                    End If
                    If TrimEndSpaces(Me.ListView1.Items(w).SubItems(32).Text) = "HOL" Then
                        Holiday += 1
                    End If

                    'Count Hours Worked
                    If TrimEndSpaces(Me.ListView1.Items(w).SubItems(32).Text) = "WORK" Then
                        ' MsgBox("hi")
                        StartTime = TrimEndSpaces(Me.ListView1.Items(w).SubItems(2).Text)
                        EndTime = TrimEndSpaces(Me.ListView1.Items(w).SubItems(8).Text)

                        HoursWorked += (EndTime - StartTime)
                    End If

                    'Count Times Late & On Time
                    'If TrimEndSpaces(Me.ListView1.Items(w).SubItems(6).Text) <> "" Then
                    If TrimEndSpaces(Me.ListView1.Items(w).SubItems(5).Text) = "2" Then
                        PunchTime = TrimEndSpaces(Me.ListView1.Items(w).SubItems(2).Text)
                        If PunchTime <> "0" Then
                            Me.OleDbCommand2.CommandText = "SELECT count(*) FROM shift WHERE (number = " + TrimEndSpaces(Me.ListView1.Items(w).SubItems(15).Text) + ")"
                            'If Me.OleDbCommand2.ExecuteScalar = "0" Then
                            'MsgBox(TrimEndSpaces(Me.ListView1.Items(w).SubItems(15).Text))

                            'End If
                            Me.OleDbCommand2.CommandText = "SELECT lngstart FROM shift WHERE (number = " + TrimEndSpaces(Me.ListView1.Items(w).SubItems(15).Text) + ")"
                            data_reader = OleDbCommand2.ExecuteReader
                            count1 += 1
                            Do While data_reader.Read
                                ScheduledTime = Me.DateTimePicker1.Value.AddSeconds(TrimEndSpaces(data_reader.Item(0)) + 360)
                                TempPunchTime = Me.DateTimePicker1.Value.AddSeconds(PunchTime)
                                TempPunchTime = "1/1/1900 " + TempPunchTime.ToShortTimeString '

                                count2 += 1
                                If TempPunchTime <= ScheduledTime Then
                                    OnTime += 1
                                    Me.ListView1.Items(w).SubItems(6).Text = "ON TIME"
                                Else
                                    TimesLate += 1
                                    Me.ListView1.Items(w).SubItems(6).Text = "LATE"
                                End If
                            Loop
                            data_reader.Close()
                        End If
                End If
                End If

            Next
            'MsgBox(count1.ToString)
            'MsgBox(count2.ToString)
            Dim q As Integer
            For q = 0 To Me.ListView3.Items.Count - 1 ' counts non paid sick days
                If TrimEndSpaces(Me.ListView3.Items(q).SubItems(0).Text) = TrimEndSpaces(Me.ListView2.Items.Item(i).SubItems(0).Text) Then
                    If TrimEndSpaces(Me.ListView3.Items(q).SubItems(20).Text) = "" Then
                        Absent += 1
                    End If
                End If
            Next


            Absent = Absent - Holiday
            Me.ListView2.Items.Item(i).SubItems(3).Text = Absent.ToString

            HoursWorked = HoursWorked / 60
            HoursWorked = HoursWorked / 60
            Me.ListView2.Items.Item(i).SubItems(4).Text = HoursWorked.ToString



            Me.ListView2.Items.Item(i).SubItems(1).Text = TimesLate.ToString
            Me.ListView2.Items.Item(i).SubItems(2).Text = OnTime.ToString

        Next

        Me.OleDbConnection2.Close()
        Me.SqlConnection1.Open()
        Dim x As Integer
        For x = 0 To Me.ListView2.Items.Count - 1
            Me.SqlCommand1.Parameters(0).Value = Me.ListView2.Items.Item(x).SubItems(0).Text


            Dim data_reader2 As SqlDataReader
            data_reader2 = Me.SqlCommand1.ExecuteReader

            Do While data_reader2.Read
                Me.ListView2.Items.Item(x).SubItems(0).Text() = TrimEndSpaces(data_reader2.Item(0))
            Loop

            data_reader2.Close()
        Next x

        'SELECT COUNT(*) AS Expr1 FROM AcadAtten WHERE (Employee = @Employee) AND (Type = 'Time Off Request - Approved') AND (Date > @Date1) AND (Date < Date2)
        Dim r As Integer
        For r = 0 To Me.ListView2.Items.Count - 1
            Me.SqlCommand2.Parameters(0).Value = Me.ListView2.Items.Item(r).SubItems(0).Text
            Me.SqlCommand2.Parameters(1).Value = Me.DateTimePicker2.Value.AddDays(-1).ToShortDateString
            Me.SqlCommand2.Parameters(2).Value = Me.DateTimePicker3.Value.AddDays(1).ToShortDateString
            Me.ListView2.Items.Item(r).SubItems(5).Text = Me.SqlCommand2.ExecuteScalar
        Next
        Dim t As Integer
        For t = 0 To Me.ListView2.Items.Count - 1
            Me.SqlCommand3.Parameters(0).Value = Me.ListView2.Items.Item(t).SubItems(0).Text
            Me.SqlCommand3.Parameters(1).Value = Me.DateTimePicker2.Value.AddDays(-1).ToShortDateString
            Me.SqlCommand3.Parameters(2).Value = Me.DateTimePicker3.Value.AddDays(1).ToShortDateString

            Me.ListView2.Items.Item(t).SubItems(6).Text = Me.SqlCommand3.ExecuteScalar
        Next
        Me.SqlConnection1.Close()


        'Delete zero's
777:
        Dim z As Integer
        For z = 0 To Me.ListView2.Items.Count - 1
            If Me.ListView2.Items.Item(z).SubItems(4).Text = "0" Then
                Me.ListView2.Items.RemoveAt(z)
                GoTo 777
            End If
        Next
    End Sub

Open in new window

0
Comment
Question by:dualtech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 33449869
Change data types of StartTime and EndTime to Date, and change the following line:

HoursWorked += (EndTime - StartTime)

To

HoursWorked += DateDiff(DateInterval.Hour,  StartTime, EndTime)

0
 

Author Comment

by:dualtech
ID: 33450056
Yes thts fine for Long but what about double. i am getting double type cast error on TempPunchTime
0
 

Author Comment

by:dualtech
ID: 33450068
Which i have already taken as Date
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dualtech
ID: 33450512
Hello Zhaolai Please guide me through my second problem of type cast double error. I already worte about the same in my question too. your solution has worked fine for me for long and I think i am not getting any error at that point but now even when I have taken Punchtime as Date then also it is giving me exception of type cast double please help me with that.
0
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 500 total points
ID: 33451304
PunchTime is declared as String. AddSeconds takes a double.
You can fix it by simply doing this:

TempPunchTime = Me.DateTimePicker1.Value.AddSeconds(Val(PunchTime))

0
 

Author Closing Comment

by:dualtech
ID: 33458124
Thnx for helping me quickly
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question