Solved

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

Posted on 2008-10-08
12
1,132 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

689 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