I’m getting this:
private object setReportValues(object report, FormCollection values)
{
PropertyInfo[] properties = report.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
string val = values.GetValue(property.Name).ToString();
property.SetValue(report, val, null);
}
return report;
}
Exception is on string val = values.GetValue(property.Name).ToString();. Do I have to check for nulls before?
I’m gonna go out on a limb and suggest that there’s no property with the provided
property.Nameinvalues. So your call to values.GetValue returns a null. When you try to do ToString() on that null value, it complains.In short, what does your values variable contain?
Update:
With the provided information that values is a FormsCollection it is quite probable that your properties collection contains a few properties for which you have no FormsCollection field. And what happens is that you try to get this field, it returns a null value and you call ToString on that, causing everything to break.
I would invert my strategy and loop through my FormsCollection getting the properties 1 by 1 as you encounter them. The alternative is to keep it as you have it and check for null before doing a ToString.
PS: I hope all of your properties represented on the form are strings, or things will break.