System.IndexOutOfRangeException: 异常
程序中有这样的代码:
System.Data.DataView contactlist=new System.Data.DataView(table);
contactlist.Sort = "companyId";
其中table是个DataTable对象,companyId 是table中的一列。
现在出现这样的异常信息:
System.IndexOutOfRangeException: 无法找到列 companyid。
在 System.Data.DataTable.ParseSortString(String sortString)
在 System.Data.DataView.CheckSort(String sort)
在 System.Data.DataView.set_Sort(String value)
在 BLL.Company.InitAllCompanyLineAndAllCompanyInfoAndAllCityCompany()
其中:BLL.Company.InitAllCompanyLineAndAllCompanyInfoAndAllCityCompany()是出现上述两行代码的方法。
现已知抛出异常的代码是contactlist.Sort = "companyId";
问题现象:
这个错误不是每次都有,是偶尔发生
环境:win2008 64位,sqlserver2008 64位, asp.net 2.0
请教高手指教
------解决方案--------------------本帖最后由 net_lover 于 2010-12-09 12:33:08 编辑
你的数据源里面没有 companyid 这个字段,错误说的很清楚了
不信的话,
你在
contactlist.Sort = "companyId";
的前面打印出 table 的所有列名看看
------解决方案--------------------跟 table 是不是动态没关系。既然查出来了,那就是DataTable,计算机不会撒谎的,你输出一下table 的列名不是明白了吗???
------解决方案--------------------无法找到表 1。
在 System.Data.DataTableCollection.get_Item(Int32 index)
应该是只有一个表,而你采用 DataTableCollection[1]去得到。才会报告错误的,
这种问题只能自己分析代码啊,别人又看不到代码。比如使用static 修饰符是要特别小心的