类型“System.DBNull”的对象无法转换为类型“System.String”
行 35: {
行 36: if (columns[i].ColumnName == properties[j].Name)
行 37: { properties[j].SetValue(type, currentRow[i], null); }
行 38: }
行 39: } public static List<T> ConvertToList<T>(DataTable table) where T : new()
{
//置为垃圾对象
List<T> list = null;
if (table != null)
{
DataColumnCollection columns = table.Columns;
int columnCount = columns.Count;
T type = new T();
Type columnType = type.GetType();
PropertyInfo[] properties = columnType.GetProperties();
if (properties.Length == columnCount)
{
list = new List<T>();
foreach (DataRow currentRow in table.Rows)
{
for (int i = 0; i < columnCount; i++)
{
for (int j = 0; j < properties.Length; j++)
{
if (columns[i].ColumnName == properties[j].Name)
{ properties[j].SetValue(type, currentRow[i], null); }//说是这个行出错,怎么修改这个方法呢
}
}
list.Add(type); type = new T();
}
}
else { list = null; }
}
else
{
throw new ArgumentNullException("参数不能为空");
}
return list;
}
行 35: {
行 36: if (columns[i].ColumnName == properties[j].Name)
行 37: { properties[j].SetValue(type, currentRow[i], null); }
行 38: }
行 39: } public static List<T> ConvertToList<T>(DataTable table) where T : new()
{
//置为垃圾对象
List<T> list = null;
if (table != null)
{
DataColumnCollection columns = table.Columns;
int columnCount = columns.Count;
T type = new T();
Type columnType = type.GetType();
PropertyInfo[] properties = columnType.GetProperties();
if (properties.Length == columnCount)
{
list = new List<T>();
foreach (DataRow currentRow in table.Rows)
{
for (int i = 0; i < columnCount; i++)
{
for (int j = 0; j < properties.Length; j++)
{
if (columns[i].ColumnName == properties[j].Name)
{ properties[j].SetValue(type, currentRow[i], null); }//说是这个行出错,怎么修改这个方法呢
}
}
list.Add(type); type = new T();
}
}
else { list = null; }
}
else
{
throw new ArgumentNullException("参数不能为空");
}
return list;
}
解决方案 »
- 关于Repeater里控件后台事件问题
- 水晶报表字段显示能否垂直居中(上下居中显示)
- strings类型的文件有什么用?
- 在ASP页面里面取.NET页面FORM表单(POST方式)乱码的问题,请高手帮忙!!!!!!!!
- 关于成员管理的问题
- 树型展开什么做?
- asp.net+c#,button的OnClick事件不能用,点击后没有任何反映,详情请进来看看
- 效率问题请教大家: 我有一个明细表单近一百个字段要显示, 对于这些要显示的字段我是用多个Label控件还是选用一个Repeater控件更合理呢?
- 如何实现Table控件行的动态添加
- 请问一个 xsd数据集 和一个 DataSet 之间的关系是什么?如何转化?
- net中用C#怎样把我的GridView控件生成的表格用一张图片画出来?
- 关于dropdownlist控件的设计
就在后面写值实例
{
//置为垃圾对象
List<T> list = null;
if (table != null)
{
DataColumnCollection columns = table.Columns;
int columnCount = columns.Count;
T type = new T();
Type columnType = type.GetType();
PropertyInfo[] properties = columnType.GetProperties();
if (properties.Length == columnCount)
{
list = new List<T>();
foreach (DataRow currentRow in table.Rows)
{
for (int i = 0; i < columnCount; i++)
{
for (int j = 0; j < properties.Length; j++)
{
if (columns[i].ColumnName == properties[j].Name)
{
if (currentRow[i]!= DBNull.Value)
{
properties[j].SetValue(type, currentRow[i], null);
}
}
}
}
list.Add(type); type = new T();
}
}
else { list = null; }
}
else
{
throw new ArgumentNullException("参数不能为空");
}
return list;
}