【问题标题】:Getting System.FormatException: Input string was not in a correct format. when creating a workflow获取 System.FormatException:输入字符串的格式不正确。创建工作流时
【发布时间】:2019-07-02 11:43:35
【问题描述】:

创建自定义工作流程。

我有一个输入,它是一个选项集。

我想获取输入的这个选项集的文本值。

将其转换为十进制,然后将其添加到我的输出中以填充 Dyanimcs 中的另一个十进制字段

var setvalue = hours.Get<OptionSetValue>(executionContext).ToString();

var hoursDecimal = Convert.ToDecimal(setvalue);
this.decimalval.Set(executionContext,hoursDecimal);

System.FormatException:输入字符串的格式不正确。是错误

【问题讨论】:

  • 当您尝试将setvalue 转换为小数时,它的值是多少?
  • 7.5 是值
  • 让我添加选项集值列表是小数列表,因此选项是 2.5、1.0 等等
  • @jonathan 使用Decimal.TryParse 方法:docs.microsoft.com/en-us/dotnet/api/…

标签: c# dynamics-crm


【解决方案1】:

在您的情况下,使用 Decimal.TryParse 方法产生您想要的正确结果。在您的代码中:

var setvalue = hours.Get<OptionSetValue>(executionContext).ToString();
decimal parsedsetvalue;

if(decimal.TryParse(setvalue,out parsedsetvalue))
{ 
  // success - can use parsedsetvalue
  this.decimalval.Set(executionContext,parsedsetvalue);
}
else
{
  //error in coverting parsedsetvalue to decimal
  return "Error, could not convert setvalue to decimal";
}

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2011-11-23
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多