Reputation: 565
I am looping through a class with multiple properties and am searching for any textboxes that have the same ID. If there is a match then I want to update the property value to that of the textbox value, but I am getting this error:
Object does not match target type
This is the code:
foreach (var prop in contactInfo.GetType().GetProperties())
{
var ctrl = WizardCampaign.FindControl(prop.Name) ?? Page.Master.FindControl(prop.Name);
if (ctrl != null)
{
if (ctrl.GetType() == typeof(TextBox))
{
var r = (TextBox)ctrl;
prop.SetValue(prop, r.Text, null);
}
}
}
Upvotes: 1
Views: 2995
Reputation: 16318
Look here:
prop.SetValue(prop, r.Text, null);
SetValue
is supposed to take the object you want to change as the first parameter, but you are passing the PropertyInfo
object. I believe your actual code should be:
prop.SetValue(contactInfo, r.Text, null);
Upvotes: 5