1

I am trying to write up a system that passes its value to the front end via an MVC web service.

While it's running, I keep encountering this error and I cannot get past it; I have isolated it to this line of code but I cannot see what is wrong with it. This is the line of code, the timestamp and the error:

code timestamp error

I cannot divulge too much code as some of it is sensitive, however this is generic. If you require more information I will do my best to provide it.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Matt Farrell
  • 191
  • 1
  • 1
  • 13
  • 2
    What you need to do is use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360). It will enable Visual Studio to tell you where data types do not match up. For example, the code you show implies that you are trying to store a string in a Integer or DateTime variable, which is wrong. – Andrew Morton Nov 22 '19 at 18:41
  • It's already on and adding the tag to the top of the code didn't do anything – Matt Farrell Nov 22 '19 at 18:50
  • 2
    Is the type of `paymentRequest.TimeStamp` definitely DateTime? I know it looks like it is from the #numbers PM#, but is that what it is declared as? Does `CDate(paymentRequest.TimeStamp).ToString("yyyyMMddHHmmss")` make it work? – Andrew Morton Nov 22 '19 at 19:00
  • That's worth checking – Matt Farrell Nov 22 '19 at 19:01
  • paymentRequest.TimeStamp = DateTime.Now – Matt Farrell Nov 25 '19 at 08:51
  • If `paymentRequest.TimeStamp` has a type of `Object` then that will work but the `.ToString()` won't work as intended. – Andrew Morton Nov 25 '19 at 09:16
  • Public Property Timestamp As String its built with Dim viewModel as new PaymentPageViewModel With { .Timestamp = paymentRequest.Timestamp.ToString("yyyyMMddHHmmss") } there is other stuff within that. i have since added CDate(paymentRequest.TimeStamp) before this its stored as part of an object with the type been "Date?" – Matt Farrell Nov 25 '19 at 09:44
  • If the type of `paymentRequest.TimeStamp` is `Date?` (i.e. `Nullable(Of DateTime)`) then you would need `paymentRequest.TimeStamp.Value` to get its value, and so the code snippet should be `.Timestamp = paymentRequest.TimeStamp.Value.ToString("yyyyMMddHHmmss")`. – Andrew Morton Nov 25 '19 at 11:24
  • well after some testing i had to rebuild and redeploy to get it to register the change but the CDate seemed to have fixed it, i still have some reservations given this code apparently works for others so i want to know why however for me i am just looking at knocking down each bug as it comes until this works. If you want to post as an answer rather than comment i will register it as the fix for this issue. – Matt Farrell Nov 25 '19 at 11:27
  • When you assign a value to a Nullable type, you just use `variable = x`, but to get the value from it you need `y = variable.Value`. The vital information to make it work properly was revealed when you said it has the type `Date?`. It really should have put a red squiggly line under the problem in Visual Studio - perhaps the Option Strict On is faulty in the project - if you toggle it off and on again maybe it will persuade it to notice it. – Andrew Morton Nov 25 '19 at 11:34
  • Perhaps, this isn't great code which makes debugging a little tricky as on paper it should have worked. it should never be null by the time it gets to this point though given its populated in the initial request. I will take on board adding .value however i will not add it as currently encapsulating it within CDate() has fixed this issue. My other issue is related to RSA within another project so unrelated to the date. – Matt Farrell Nov 25 '19 at 11:38

1 Answers1

1

Because the type of paymentRequest.TimeStamp is Date? (i.e. Nullable(Of DateTime)) then you would need paymentRequest.TimeStamp.Value to get its value, and so the code snippet should be

.Timestamp = paymentRequest.TimeStamp.Value.ToString("yyyyMMddHHmmss") 

Although it is undocumented (as far as I can find), you can also use CDate to get the date from a Date? thus: .Timestamp = CDate(paymentRequest.TimeStamp).ToString("yyyyMMddHHmmss").

You may wish to avoid using an undocumented feature, as it could change without notification in later versions.

Both ways will fail with an error of

System.InvalidOperationException: 'Nullable object must have a value.'

if paymentRequest.TimeStamp.HasValue is False.


Perhaps confusingly, the documentation for the Nullable.ToString Method says that

The ToString property returns the string yielded by calling the ToString property of the object returned by the Value property.

however, it should be noted that this only applies to the ToString() method with no arguments.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84