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
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
228 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 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