[英]WF C# VS2012 - InvalidCast Exception was unhandled by user code?
I was trying an example from a book on Windows Workflow and I got an error : 我正在尝试一本关于Windows Workflow的书中的示例,但出现错误:
InvalidCast Exception was unhandled by user code 用户代码未处理InvalidCast异常
Unable to cast object of type 'System.DBNull' to type 'System.String'. 无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象。
The exact code causing the error is : 导致错误的确切代码是:
try
{
// Send data to workflow!
IDictionary<string, object> outputArgs =
WorkflowInvoker.Invoke(new CheckInventory(), wfArgs);
// Print out the output message.
Console.WriteLine(outputArgs["FormattedResponse"]);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
The program will run, taking two questions from the user: color & make of a car and it then throws up this error. 该程序将运行,并向用户提出两个问题:汽车的颜色和制造商,然后引发此错误。 Any ideas ?
有任何想法吗 ?
I guess the problem is the line 我想问题是线
Console.WriteLine(outputArgs["FormattedResponse"]);
It seems like you're trying to convert outputArgs["FormattedResponse"] into a String (in order to write it to the console), but it evaluates to DBNull (ie, there is not such output message in the output args). 似乎您正在尝试将outputArgs [“ FormattedResponse”]转换为String(以便将其写入控制台),但其结果为DBNull(即,输出args中没有此类输出消息)。 Therefore, check whether outputArgs["FormattedResponse"] != DBNull.Value before printing it:
因此,在打印输出之前,请检查outputArgs [“ FormattedResponse”]!= DBNull.Value:
var outputResponse = outputArgs["FormattedResponse"];
if(outputResponse != DBNull.Value)
Console.WriteLine(outputResponse);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.