1

As follows i see below error when trying to convert date from string. Strange thing is same code works correctly on the other server which has the same Regions configured. Moreover if i run this code from console application it works. This error only happen when scheduled in windows service. Why it happens, is there better way to do it?

Code with debug information: enter image description here

Try
   Dim fileDateStr = Data(0, 0).ToString()
   fileDateStr = fileDateStr.Substring(0, fileDateStr.IndexOf("(", StringComparison.Ordinal))

       Dim dateValue As Date
               If Date.TryParse(CDate(fileDateStr), dateValue) Then
                   ReportDate = CDate(fileDateStr)
               End If
Catch ex As Exception
 ..
2
  • 1
    Please copy-and-paste your code as text and do not include it as a picture. Commented Nov 20, 2017 at 8:31
  • The code appears to be unclear on the point of TryParse: you're trying to parse a Date into a Date, and because you use CDate unconditionally, you get the exception that TryParse is designed to avoid. You should either eliminate the TryParse and let the Try/Catch handle the errors, or eliminate the CDate calls and address any date errors in the False branch from the TryParse call. Commented Nov 20, 2017 at 14:02

2 Answers 2

1

You try to convert the string value to date by using CDate before the Date.TryParse is used!

So you can use the following code to set the date only if valid by Date.TryParse:

Dim fileDateStr = Data(0, 0).ToString()
fileDateStr = fileDateStr.Substring(0, fileDateStr.IndexOf("(", StringComparison.Ordinal))

Dim dateValue As Date

If Date.TryParse(fileDateStr, dateValue) Then
    ReportDate = dateValue
End If

You can also use Date.TryParseExact if the fileDateStr is always on the same format (in your case MM/dd/yyyy - more details how to create a format string):

Dim fileDateStr = Data(0, 0).ToString()
fileDateStr = fileDateStr.Substring(0, fileDateStr.IndexOf("(", StringComparison.Ordinal))

Dim dateValue As Date

If Date.TryParseExact(fileDateStr, "MM/dd/yyyy", CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dateValue) Then
    ReportDate = dateValue
End If

If there are multiple valid formats for the fileDateStr you can also use Date.TryParseExact:

Dim fileDateStr = Data(0, 0).ToString()
fileDateStr = fileDateStr.Substring(0, fileDateStr.IndexOf("(", StringComparison.Ordinal))

Dim dateValue As Date
Dim formats As String() = {"MM/dd/yyyy", "yyyy-MM-dd"}

If Date.TryParseExact(fileDateStr, formats, CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dateValue) Then
    ReportDate = dateValue
End If
Sign up to request clarification or add additional context in comments.

4 Comments

thank you ! However do you know why it was working on one server and giving the error on antoher (only in windows service) because even on second server using console app was working as on 1st one :O. To me that should be everytime error.
You are welcome! I don't know why it was not working. It could be globalization or an invalid result of the substring.
One more thing i checked more deeply and since i do not get error anymore since i implement your fix, this line: Date.TryParse(fileDateStr, dateValue) is nto giving true... Can you help me out?
So Date.TryParse couldn't get the date value from string. You can try a solution using TryParseExact with format string. Make also sure the value on fileDateStr is a valid date (in the expected format). You can also check and set the Application.Culture of your application to make sure the correct one is used.
1

As stated in Sebastian Brosch's answer, you're converting before the string is being parsed to a date.

The reason why it can't convert when ran as a service, may be the different culture/region settings of the user that runs the service. Depending on these settings "11/16/2017" won't be parsed with TryParse.

If the format of fileDateStr will always be "MM/dd/yyyy", then you can use TryParseExact instead.

Dim fileDateStr = Data(0, 0).ToString()
fileDateStr = fileDateStr.Substring(0, fileDateStr.IndexOf("(", StringComparison.Ordinal))

Dim dateValue As Date

If Date.TryParseExact(fileDateStr,
                      "MM/dd/yyyy",
                      CultureInfo.InvariantCulture,
                      DateTimeStyles.None,
                      dateValue) Then

    ReportDate = dateValue
End If

2 Comments

i checked more deeply, and unless Sebastian answer not giving error it was not parsed... mean this line: Date.TryParse(fileDateStr, dateValue) was not true. Can you help me out to fix that? Note that on the another server everything is working fine even before i made the fix, so i would like also when migrate to that server that all was working, isn;t there any universal approach to fix that?
I don't understand. What's not clear? My answer addresses exactly the problem that "11/16/2016" can't be parsed with TryParse, depending on the culture-settings, and thus will return False. Because of that you should use TryParseExact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.