我用的是VB,程序如下:
dim name as string
dim dr as DataRow
dr=dt.NewRow() 'dt是DataTable的一个实例
name=dr("名称") '“名称”是表的一个子段名我的目的是获得某一行某一列(即指定单元格)的值,在IIS运行后却出错,提示“Value of type 'System.Data.DataRow' cannot be converted to 'string'”.
不知我的写法错在哪,该如何解决?
dim name as string
dim dr as DataRow
dr=dt.NewRow() 'dt是DataTable的一个实例
name=dr("名称") '“名称”是表的一个子段名我的目的是获得某一行某一列(即指定单元格)的值,在IIS运行后却出错,提示“Value of type 'System.Data.DataRow' cannot be converted to 'string'”.
不知我的写法错在哪,该如何解决?
DataTable都不能用.
它所在的类是System.Data
dr("名称")是个object,你需要转换!
取表中一行的值dim name as string
dim dr as DataRow
dr=dt.NewRow() 'dt是DataTable的一个实例
name=dr("名称") '“名称”是表的一个子段名
应该是 name=dr("名称") As String dr 是object类型,你需要转换成String 的类型
提示“Value of type 'System.Data.DataRow' cannot be converted to 'string'”.
=======================================
楼主要学会看错误提示阿!
name=dr(“名称”)<--这里左边是string 右边是DataRow,DataRow值当然无法赋给string了。
正确的写法是
name=dr["名称"];
不过dr既然是dt的newrow,不知道楼主取这个null干虾米?= =!
楼上的高人说明能否详细点?多谢!!!
如果你的dt这个实例是从数据库中读出来的,那么请下面的两种方式,即使原来字段的类型已经在数据库中被定义为varchar等:
name=dr["名称"].ToString();
或者
name=Convert.ToString(dr["名称"]);
========================================
从数据库读出来的dt在VS中默认为object型,必须显式地转换为你需要的类型。
dt.rows[行][例]