I've got a hashtable that I want to update from a second hashtable. For any of the keys that match I want to copy the value over. The problem I have is when I enumerate the hashtable keys and try to cast each to a string I receive an exception about casting a Guid to a String. Well it's the string I want. When you use the index operator with something like hashtable["FirstName"] then I expect FirstName to be the key. It might use Guids underneath I guess but I need to get out the string for the key, the key value.
private void UpdateSharePointFromInfoPath(Hashtable infopathFields)
{
// Go through all the fields on the infopath form
// Invalid Cast Exception Here
foreach (String fieldName in infopathFields.Keys)
{
// If the same field is on sharepoint
if (workflowProperties.Item.Fields.ContainsField(fieldName))
{
// Update the sharepoint field with the new value from infopath
workflowProperties.Item[fieldName] = infopathFields[fieldName];
}
}
// Commit the changes
workflowProperties.Item.Update();
}
EDIT I don't create either of these hashtables. The keys have strings somewhere because I can put the field name in like the following and get the value of the field out. I'm trying to make a shorthand way of doing the following for every field:
workflowProperties.Item["FirstName"] = infopathFields["FirstName"];
workflowProperties.Item["LastName"] = infopathFields["LastName"];
workflowProperties.Item["Address"] = infopathFields["Address"];
workflowProperties.Item["DOB"] = infopathFields["DOB"];
ect...
EDIT It's been said that the hashtable uses Guids, but it also obviously has a string inside else I wouldn't be able to do infopathFields["FirstName"]. It's the value on the string I pass in there that I want.
Every item is a Key/Value pair of format DictionaryEntry
foreach (DictionaryEntry de in infopathFields)
{
string fieldName = de.Key as string;
if (workflowProperties.Item.Fields.ContainsField(fieldName))
{
workflowProperties.Item[fieldName] = infopathFields[fieldName];
}
}
workflowProperties.Item.Update();
The standard version of the Hashtable can have different type keys, so most of your keys may be strings, but some of your keys may be GUIDs. I'm willing to bet that is the case and is causing your issue. The following little console app demonstrates the problem.
static void Main(string[] args)
{
System.Collections.Hashtable htable = new System.Collections.Hashtable();
htable.Add("MyName", "WindyCityEagle");
htable.Add("MyAddress", "Here");
htable.Add(new Guid(), "That Was My Guid");
int loopCount = 0;
foreach (string s in htable.Keys)
{
Console.WriteLine(loopCount++.ToString());
Console.WriteLine(htable[s]);
}
}
You'll get the exact same exception that you're reporting here.
My suggestion to fix the problem would be to go with the following
private void UpdateSharePointFromInfoPath(Hashtable infopathFields)
{
// Go through all the fields on the infopath form
// Invalid Cast Exception Here
foreach (object key in infopathFields.Keys)
{
string wfpKey = key.ToString();
// If the same field is on sharepoint
if (workflowProperties.Item.Fields.ContainsField(wfpKey))
{
// Update the sharepoint field with the new value from infopath
workflowProperties.Item[wfpKey] = infopathFields[key];
}
}
// Commit the changes
workflowProperties.Item.Update();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With