Solved

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

Posted on 2008-10-08
12
1,121 Views
Last Modified: 2012-05-05
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
Comment
Question by:pedley123
  • 6
  • 5
12 Comments
 
LVL 11

Expert Comment

by:ladarling
ID: 22672486
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
 

Author Comment

by:pedley123
ID: 22672935
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
 
LVL 11

Expert Comment

by:ladarling
ID: 22673162
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
 
LVL 11

Expert Comment

by:ladarling
ID: 22673179
That will get you Now() in the expected string format that the function apparently creates.
0
 

Author Comment

by:pedley123
ID: 22673325
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
 
LVL 2

Expert Comment

by:MTEXX
ID: 22673464
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:pedley123
ID: 22673601
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
 
LVL 11

Accepted Solution

by:
ladarling earned 500 total points
ID: 22673688
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
 
LVL 11

Expert Comment

by:ladarling
ID: 22673696
Oh, and the Catch Return value I provided above is still good as a last resort.
0
 

Author Comment

by:pedley123
ID: 22673829
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
 
LVL 11

Expert Comment

by:ladarling
ID: 22677973
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
 

Author Closing Comment

by:pedley123
ID: 31504396
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 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

15 Experts available now in Live!

Get 1:1 Help Now