Solved

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

Posted on 2008-10-08
12
1,118 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

20 Experts available now in Live!

Get 1:1 Help Now