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
227 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
  • 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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