大家帮我看看这个类,为什么我在我自己机子上不会抛出类型转换出错,而我在我同学机子上就抛出类型转换出错,而且一律的都是数据库中的自增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编辑过]