Reputation: 3730
I was trying an example from a book on Windows Workflow and I got an error :
InvalidCast Exception was unhandled by user code
Unable to cast object of type 'System.DBNull' to type '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 ?
Upvotes: 1
Views: 94
Reputation: 1246
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 (i.e., there is not such output message in the output args). Therefore, check whether outputArgs["FormattedResponse"] != DBNull.Value before printing it:
var outputResponse = outputArgs["FormattedResponse"];
if(outputResponse != DBNull.Value)
Console.WriteLine(outputResponse);
Upvotes: 1