Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1153
  • Last Modified:

Conversion from string '"' to type 'Date' is not valid in vb

Hello

I've taken over the development of an email application written in VB. The application polls an exchange mailbox and then creates records in a sql database. The application runs fine for the most part except for an error that occurs every now and again. There is a function that reads as follows:

Public Function convertDate(ByVal strDate) As String

        Dim now As DateTime = DateTime.Now
        Dim nowGMT As DateTime = now.ToUniversalTime()
        Dim diff As TimeSpan = now.Subtract(nowGMT)
        Dim s_Date(0 To 6) As String
        Dim sDateBuilder As StringBuilder = New StringBuilder
        Dim i As Integer
        i = 0
        Dim _matches As MatchCollection = Regex.Matches(strDate, "((\d+))", RegexOptions.Singleline)
        Dim _match As Match
        For Each _match In _matches
            s_Date(i) = _match.ToString
            i = i + 1
        Next
        Dim convDate As New DateTime(s_Date(0), s_Date(1), s_Date(2), s_Date(3), s_Date(4), s_Date(5))
        convDate = convDate + diff
        Return convDate.ToString("d MMMM yyyy HH:mm tt")

    End Function

I've tried adding a Try Catch as follows:

Public Function convertDate(ByVal strDate) As String

        Dim now As DateTime = DateTime.Now
        Dim nowGMT As DateTime = now.ToUniversalTime()
        Dim diff As TimeSpan = now.Subtract(nowGMT)
        Dim s_Date(0 To 6) As String
        Dim sDateBuilder As StringBuilder = New StringBuilder
        Dim i As Integer
        i = 0
        Dim _matches As MatchCollection = Regex.Matches(strDate, "((\d+))", RegexOptions.Singleline)
        Dim _match As Match
        Dim convDate As New DateTime
        Try
            For Each _match In _matches
                s_Date(i) = _match.ToString
                i = i + 1
            Next
            convDate = (s_Date(0), s_Date(1), s_Date(2), s_Date(3), s_Date(4), s_Date(5))
            convDate = convDate + diff
        Catch ex As Exception
            Return now
        End Try
        Return convDate.ToString("d MMMM yyyy HH:mm tt")


    End Function

Intelli sense in vis studio says ' name convDate is not declared' on the last line of the function

I've tried declaring the convDate outside the try catch as follows:

Public Function convertDate(ByVal strDate) As String

        Dim now As DateTime = DateTime.Now
        Dim nowGMT As DateTime = now.ToUniversalTime()
        Dim diff As TimeSpan = now.Subtract(nowGMT)
        Dim s_Date(0 To 6) As String
        Dim sDateBuilder As StringBuilder = New StringBuilder
        Dim i As Integer
        i = 0
        Dim _matches As MatchCollection = Regex.Matches(strDate, "((\d+))", RegexOptions.Singleline)
        Dim _match As Match
        Dim convDate As New DateTime
        Try
            For Each _match In _matches
                s_Date(i) = _match.ToString
                i = i + 1
            Next
            convDate = (s_Date(0), s_Date(1), s_Date(2), s_Date(3), s_Date(4), s_Date(5))
            convDate = convDate + diff
        Catch ex As Exception
            Return now
        End Try
        Return convDate.ToString("d MMMM yyyy HH:mm tt")


    End Function

but this line 'convDate = (s_Date(0), s_Date(1), s_Date(2), s_Date(3), s_Date(4), s_Date(5))'
shows ')' expected on the first comma of this line.

Any ideas?

Thanks


0
pedley123
Asked:
pedley123
  • 6
  • 5
1 Solution
 
ladarlingCommented:
but this line 'convDate = (s_Date(0), s_Date(1), s_Date(2), s_Date(3), s_Date(4), s_Date(5))'
shows ')' expected on the first comma of this line.


 Thats just never gonna work for you. s_Date() is a string array, and none of the overloads for DateTime.New() accept strings as parameters. When this function is working, its because all of the parameters are being parsed to integers. When you get a zero length string in the mix - "", you are going to have problems.
The code above is fairly unintuitive... what exactly is the Input and Output of this function *supposed* to be. That would help greatly in figuring out a fix.
0
 
pedley123Author Commented:
not sure how to respond to 'what exactly is the Input and Output of this function *supposed* to be'

Going back to the function in its original state that for the most part works, and leaving off the try and catch:

Public Function convertDate(ByVal strDate) As String

        Dim now As DateTime = DateTime.Now
        Dim nowGMT As DateTime = now.ToUniversalTime()
        Dim diff As TimeSpan = now.Subtract(nowGMT)
        Dim s_Date(0 To 6) As String
        Dim sDateBuilder As StringBuilder = New StringBuilder
        Dim i As Integer
        i = 0
        Dim _matches As MatchCollection = Regex.Matches(strDate, "((\d+))", RegexOptions.Singleline)
        Dim _match As Match
        For Each _match In _matches
            s_Date(i) = _match.ToString
            i = i + 1
        Next
        Dim convDate As New DateTime(s_Date(0), s_Date(1), s_Date(2), s_Date(3), s_Date(4), s_Date(5))
        convDate = convDate + diff
        Return convDate.ToString("d MMMM yyyy HH:mm tt")

    End Function

how would i modify the above code so that if this error occurs it can be ignored and the application can continue running?

Thanks
0
 
ladarlingCommented:
What I meant was by In/out was:
Public Function convertDate(ByVal strDate) As String
What is being passed into the function parameter strDate? From what I can tell, its a date is string format that is being subtracted from Now() and then ... transformed again using regular expressions... Im sure it has a purpose by I dont know the context.
Anyway, to implement a workaround, change:
Catch ex As Exception
            Return now
To:
Catch ex As Exception
           Return Format(Now, "d MMMM yyyy HH:mm tt").ToString
0
Technology Partners: 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!

 
ladarlingCommented:
That will get you Now() in the expected string format that the function apparently creates.
0
 
pedley123Author Commented:
Thanks for the quick response

i now have:

Public Function convertDate(ByVal strDate) As String

        Dim now As DateTime = DateTime.Now
        Dim nowGMT As DateTime = now.ToUniversalTime()
        Dim diff As TimeSpan = now.Subtract(nowGMT)
        Dim s_Date(0 To 6) As String
        Dim sDateBuilder As StringBuilder = New StringBuilder
        Dim i As Integer
        i = 0
        Dim _matches As MatchCollection = Regex.Matches(strDate, "((\d+))", RegexOptions.Singleline)
        Dim _match As Match
        Dim convDate As New DateTime
        Try
            For Each _match In _matches
                s_Date(i) = _match.ToString
                i = i + 1
            Next
            convDate = (s_Date(0), s_Date(1), s_Date(2), s_Date(3), s_Date(4), s_Date(5))
            convDate = convDate + diff
        Catch ex As Exception
            Return Format(Now, "d MMMM yyyy HH:mm tt").ToString
        End Try
        Return convDate.ToString("d MMMM yyyy HH:mm tt")


    End Function

but this line 'convDate = (s_Date(0), s_Date(1), s_Date(2), s_Date(3), s_Date(4), s_Date(5))' still shows ')' expected on the first comma of the line

thanks
0
 
MTEXXCommented:
Hmmm.  Many things can go wrong parsing a formatted string into a date/time.

Perhaps you can show the caller an error occurred.  A rather unobtrusive way would look like:
Public Function convertDate(ByVal strDate as string, Optional byval defaultValue as String = nothing) As String
 
'
'
'
'
Catch ex As Exception
    if(defaultValue isnot nothing) then
         return defaultValue
    else
         return Format(Now, "d MMMM yyyy HH:mm tt").ToString
    end if
End Try

Open in new window

0
 
pedley123Author Commented:
Thanks for the update.

what i realy want the existing function to do is simply catch the error and not necessarily do anything with it, other than prevent the application from halting which is what it does now. The application has processed thousands of emails without any problems but on 2 occasions, an email that was received has generated the 'Conversion from string '"' to type 'Date' is not valid' error and as a result of this 1 email, the whole application has stopped working, which i then have to manually reboot.  so i simply want to be able to say, 'if the application comes across another example like this, rather than crash the application, ignore that email and carry on processing the rest, which is why i'm trying to add the try/catch so the application can continue working.

thanks
0
 
ladarlingCommented:
The application has processed thousands of emails without any problems but on 2 occasions, an email that was received has generated the 'Conversion from string '"' to type 'Date' is not valid' error  
That is exactly what I was trying to get across. Try making these two changes and lets see what happens:
Change:
Dim s_Date(0 To 6) As String

To:
Dim s_Date() As String = {0, 0, 0, 0, 0, 0}
This will give you a failsafe value. Then Change:
convDate = (s_Date(0), s_Date(1), s_Date(2), s_Date(3), s_Date(4), s_Date(5))

To:'
convDate = New DateTime(CInt(s_Date(0)), CInt(s_Date(1)), CInt(s_Date(2)), CInt(s_Date(3)), CInt(s_Date(4)), CInt(s_Date(5)))
To fix the 'New' and invalid type problems.
0
 
ladarlingCommented:
Oh, and the Catch Return value I provided above is still good as a last resort.
0
 
pedley123Author Commented:
Thanks again for the quick response

No syntax errors, i now have:

Public Function convertDate(ByVal strDate) As String

        Dim now As DateTime = DateTime.Now
        Dim nowGMT As DateTime = now.ToUniversalTime()
        Dim diff As TimeSpan = now.Subtract(nowGMT)
        Dim s_Date() As String = {0, 0, 0, 0, 0, 0}
        Dim sDateBuilder As StringBuilder = New StringBuilder
        Dim i As Integer
        i = 0
        Dim _matches As MatchCollection = Regex.Matches(strDate, "((\d+))", RegexOptions.Singleline)
        Dim _match As Match
        Dim convDate As New DateTime
        Try
            For Each _match In _matches
                s_Date(i) = _match.ToString
                i = i + 1
            Next
            convDate = New DateTime(CInt(s_Date(0)), CInt(s_Date(1)), CInt(s_Date(2)), CInt(s_Date(3)),   CInt(s_Date(4)), CInt(s_Date(5)))
            convDate = convDate + diff
        Catch ex As Exception
            Return Format(Now, "d MMMM yyyy HH:mm tt").ToString
        End Try
        Return convDate.ToString("d MMMM yyyy HH:mm tt")


    End Function

This includes the try/catch you mentioned as well.

For my sanity, is this correct before i publish the code to the application.

Thanks
0
 
ladarlingCommented:
I tested it in its current form, this is the output of the function. If that looks right to you, I'd say you are good to go. I have one more thing I would recommend, and its just for sanity's sake...
The word 'now' is a built in function, but this line redefines it
Dim now As DateTime = DateTime.Now

I would rename it to dtNow, or something, just to prevent any unexpected problems down the road.

sshot.png
0
 
pedley123Author Commented:
thanks for taking the time out to resolve this for me. i really appreciate your quick response and your patience in dealing with a novice like me
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now