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
229 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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