【发布时间】:2010-05-23 10:50:54
【问题描述】:
foreach (PropertyInfo PropertyItem in this.GetType().GetProperties())
{
PropertyItem.SetValue(this, objDataTable.Rows[0][PropertyItem.Name.ToString()], null);
}
在其中一个循环中,我得到了这个异常错误:
“System.DBNull”类型的对象无法转换为“System.String”类型。
发生错误是因为数据库中的某个字段没有值(null),因此字符串属性无法处理它。如何将此 null 转换为字符串?
如果您知道更短或更好的,请随时发布。我试图避免检查每个循环当前值是否为空。
【问题讨论】:
-
recent.item == null ? "~/Content/images/default.jpg" : 最近的.item;
标签: c# .net vb.net reflection