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
225 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now