将值设置为 属性 c# 日期时间错误格式

Set value to property c# datetime error format

当你select数据值在属性的class时会产生如下错误"Input string not in a correct format".

我正在转换图像中的日期,它生成的日期是正确的,但是在分配时生成了错误。

我将其转换为 DateTime 并创建为新的,但仍然出现错误。

使用 Scheduled = sd,

时也会出现同样的情况

我正在使用 asp.net 内核

1. Convert.ToDateTime 困扰我,虽然没有抛出这个异常。

首先,您不应该在日期时间对象上调用 Convert.ToDateTime。 它不会抛出异常,但也不进行任何转换。

参考MSDN Documentation这里。

2。异常有明确的细节,它无法将字符串转换为数字

堆栈跟踪显示 "System.Number.StringToNumber" 转换失败。

所以它是其他 3 个字符串属性之一,您正试图将它们转换为 Int64 或 long。

如果你是 运行 调试模式下的解决方案,你会通过行号知道哪个 属性。

3。如果你的值是整数字符串(例如 "10" )即使它不起作用,那么机器的文化设置也有问题。

请参考完整的thread here

据说有时您从“10”到整数值 10 的转换失败,因为某些注册表设置设置不正确。您可以通过以下方式更正它们:

While it is possible to directly edit the value for the problem key, the preferred method is to change the Regional Settings to a different Region/Language and then reset it to the desired setting:

  1. Open the Regional and Language Options applet from the Control Panel.
  2. Note the Current Format.
  3. Change the Current Format to English (Australian)
  4. Click Apply.
  5. Change the Current Format to the noted format, eg, English (American).
  6. Click Apply and then click OK.

这应该可以解决您的问题。