| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1309 人关注过本帖
标题:类型转换出错!datareader 到datatable
取消只看楼主 加入收藏
yulphoenix
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2005-10-27
收藏
 问题点数:0 回复次数:2 
类型转换出错!datareader 到datatable

大家帮我看看这个类,为什么我在我自己机子上不会抛出类型转换出错,而我在我同学机子上就抛出类型转换出错,而且一律的都是数据库中的自增ID字段抛出异常:
public class SystemTools
{
/// <summary>
/// 将DataReader 转为 DataTable
/// </summary>
/// <param name="DataReader">DataReader</param>
public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
{
DataTable datatable = new DataTable();
DataTable schemaTable = dataReader.GetSchemaTable();
//动态添加列
try
{

foreach(DataRow myRow in schemaTable.Rows)
{
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType = myRow.GetType();
myDataColumn.ColumnName = myRow[0].ToString();
datatable.Columns.Add(myDataColumn);
}
//添加数据
while(dataReader.Read())
{
DataRow myDataRow = datatable.NewRow();
for(int i=0;i<schemaTable.Rows.Count;i++)
{
myDataRow[i] = dataReader[i].ToString();
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
dataReader.Close();
return datatable;
}
catch(Exception ex)
{
///抛出类型转换错误
throw new Exception("转换出错出错!",ex);
}

}

这是<ASP.NET网络数据库开发实例精解>那本书办公自动化系统那章里的代码,
我就搞不懂为什么我在自己PC什么问题也没有,但是在我老师PC就会在这个类里出错.而且我把网页发布到网上也可以浏览.地址是http:\\oaauto.vivp.net
下面是出错的地方提示:
Server Error in '/OfficeAuto' Application.
--------------------------------------------------------------------------------

Type of value has a mismatch with column type
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Type of value has a mismatch with column type

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[ArgumentException: Type of value has a mismatch with column type]
System.Data.Common.ObjectStorage.Set(Int32 recordNo, Object value) +955
System.Data.DataColumn.set_Item(Int32 record, Object value) +37

[ArgumentException: Type of value has a mismatch with column typeCouldn't store <1> in MessageID Column. Expected type is DataRow.]
System.Data.DataColumn.set_Item(Int32 record, Object value) +72
System.Data.DataRow.set_Item(DataColumn column, Object value) +194
System.Data.DataRow.set_Item(Int32 columnIndex, Object value) +25
OfficeAuto.Components.SystemTools.ConvertDataReaderToDataTable(SqlDataReader dataReader) in F:\Documents and Settings\liang\My Documents\Visual Studio Projects\OfficeAuto\Components\SystemTools.cs:40

[Exception: 转换出错出错!]
OfficeAuto.Components.SystemTools.ConvertDataReaderToDataTable(SqlDataReader dataReader) in F:\Documents and Settings\liang\My Documents\Visual Studio Projects\OfficeAuto\Components\SystemTools.cs:52
OfficeAuto.DesktopModules.MessageManage.ViewMsg.BindMsgData(Int32 nUserID) in f:\documents and settings\liang\my documents\visual studio projects\officeauto\desktopmodules\msgmanage\viewmsg.aspx.cs:43
OfficeAuto.DesktopModules.MessageManage.ViewMsg.Page_Load(Object sender, EventArgs e) in f:\documents and settings\liang\my documents\visual studio projects\officeauto\desktopmodules\msgmanage\viewmsg.aspx.cs:35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

[此贴子已经被作者于2007-6-1 12:56:33编辑过]

搜索更多相关主题的帖子: datareader datatable 类型 
2007-06-01 12:45
yulphoenix
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2005-10-27
收藏
得分:0 
我试试看
2007-06-01 12:57
yulphoenix
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2005-10-27
收藏
得分:0 
错误一样
2007-06-01 13:00
快速回复:类型转换出错!datareader 到datatable
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025141 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved