Forum Discussion

RaptureLazarus's avatar
5 years ago
Solved

What is the correct way to parse DateTime on a server side onAfterGet event?

Hi everyone.

I'm currently trying to evaluate the current date and a date property however, when I try do parse the string provided by getProperty, it gives me the error:

String was not recognized as a valid DateTime.

I can confirm the property is of the Type: Date and Pattern: Short Date.

My relevant code:

[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:5ea4aa05-9cf8-4f39-aaab-d8f235e33c77:type=text&text=for%20%28int%20i%20%3D%200%3B%20i%20%3C%20this.getItemCount%28%29%3B%20i%2B%2B%29%0D%0A%7B%0D%0A%20%20%20%20Item%20thisItem%20%3D%20this.getItemByIndex%28i%29%3B%0D%0A%20%20%20%20%0D%0A%20%20%20%20string%20dateStr1%20%3D%20thisItem.getProperty%28%22_calibrated_next%22%2C%22%22%29%3B%0D%0A%20%20%20%20DateTime%20date1%20%3D%20DateTime.Parse%28dateStr1%29%3B%0D%0A%7D]

The only thing that I can think of that may cause this error is the date format conversion when localized from neutral UTC, but I couldn't find much in the Aras documentation related to this.

Thanks

  • So, strange enough, we had to restart the server machine and it worked flawlessly after that.

    Anyway, thanks for your help once again.

4 Replies

  • This might be because the dateStr1 property may be empty string. Can you try below code. 

    for (int i = 0; i < this.getItemCount(); i++)

    {

    Item thisItem = this.getItemByIndex(i);

    string dateStr1 = thisItem.getProperty("_calibrated_next","");

    if(!string.IsNullOrEmpty(dateStr1))

    {

    DateTime date1 = DateTime.Parse(dateStr1, CultureInfo.InvariantCulture);

    }

    }

    Thanks

    Gopikrishnan R

    • RaptureLazarus's avatar
      RaptureLazarus
      Ideator I

      Hi, thanks for your response, It seems you are correct, the string is empty., however the _calibrated_next value is displayed in the grid and item itself.

      So it definitely should be getting a value.

      • Gopikrishnan's avatar
        Gopikrishnan
        Ideator I

        Hi 

        1. Check whether the property name is correct. 

        2. Is this _calibrated_next is property in item type or on the form ?

        3. Debug the method and check the values

        Thank You

        Gopikrishnan R