无法将类型为“System.Int32”的对象强制转换为类型“System.String”。

e78640946 2011-05-04 09:31:05
无法将类型为“System.Int32”的对象强制转换为类型“System.String”。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidCastException: 无法将类型为“System.Int32”的对象强制转换为类型“System.String”。

源错误:


行 23: while (sdr.Read())
行 24: {
行 25: item.BookId = sdr.GetString(0);
行 26: item.BookAuthor = sdr.GetString(1);
行 27: item.Publisher = sdr.GetString(2);


源文件: E:\BlueStarBookShop\DAL\ItemAccess.cs 行: 25

堆栈跟踪:


[InvalidCastException: 无法将类型为“System.Int32”的对象强制转换为类型“System.String”。]
System.Data.SqlClient.SqlBuffer.get_String() +1006415
System.Data.SqlClient.SqlDataReader.GetString(Int32 i) +52
BookShop.DAL.ItemAccess.GetItem(String bookId) in E:\BlueStarBookShop\DAL\ItemAccess.cs:25
BookShop.BLL.ItemManager.GetItem(String bookId) in E:\BlueStarBookShop\BLL\ItemManager.cs:25
BookShop.BLL.CartManager.Add(String bookId) in E:\BlueStarBookShop\BLL\CartManager.cs:67
Cart.Page_Load(Object sender, EventArgs e) in e:\BlueStarBookShop\Web\ShoppingCart.aspx.cs:35
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061




--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.1882; ASP.NET 版本:2.0.50727.1879
...全文
3859 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
e78640946 2011-05-18
  • 打赏
  • 举报
回复
item.BookId = Convert.ToString(bookId);

用这句就可以了。不过又有新的问题。
fanglei10 2011-05-09
  • 打赏
  • 举报
回复
Convant.ToInt32(字段名称)
niaoked 2011-05-07
  • 打赏
  • 举报
回复
1楼正解
BK 2011-05-04
  • 打赏
  • 举报
回复
那么你试过用列名来获取了么?
e78640946 2011-05-04
  • 打赏
  • 举报
回复
这个网站我一直都有备份的,我现在是调出最初的那个备份网站,还是提示同样的问题~~以前从没有发现过这样的~~~各位大侠~~呜呜救救小女子我
BK 2011-05-04
  • 打赏
  • 举报
回复
也许是数据库的问题,类似于7楼所说的那样
e78640946 2011-05-04
  • 打赏
  • 举报
回复
之前一直没有问题,后来隔了一个月才出现这种问题的。我觉得不太像是这个网站本身的问题~~
BK 2011-05-04
  • 打赏
  • 举报
回复
item.BookId = sdr.GetString(0);
这条本身就有问题,因为sdr.GetString(0)中,0字段的类型是System.Data.SqlTypes.SqlInt32
所以无法和string进行转换。
建议改成sdr.GetInt(0);

以前也遇到过类似的问题,有时候直接用Get对应类型(列)来接收是可以的,
不过有一次却不行,后来没办法只能用sdr["列名"]来获取,然后再根据需要转型了
xuStanly 2011-05-04
  • 打赏
  • 举报
回复
行 25: item.BookId = sdr.GetString(0);
item.BookId = sdr.GetInt32(0);

如果数据库里是int的要用GetInt32
smallint/tinyint用GetInt16
bigint用GetInt64
e78640946 2011-05-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 taomanman 的回复:]
public ItemInfo GetItem(string bookId)
{
SqlParameter[] sp = { new SqlParameter("@BookId", SqlDbType.Int) };
sp[0].Value = bookId;
。。。
}

看见没,是不是类型问题啊?

public ItemInfo GetItem(string b……
[/Quote]
======================
string BookId = Request.QueryString["BookId"];
if (!string.IsNullOrEmpty(BookId))
{
if (Session["flag"].ToString().Equals(""))
{
Profile.ShoppingCart.Add(BookId);
Session["flag"] = BookId;
}

}
BindCart(Profile.ShoppingCart);

Profile.ShoppingCart.SetCartItems(Profile.UserName);
====================================
public void Add(string bookId)
{
CartInfo cartItem;
if (!cartItems.TryGetValue(bookId, out cartItem))
{
ItemManager item = new ItemManager();
ItemInfo data = item.GetItem(bookId);
if (data != null)
{
CartInfo newcartitem = new CartInfo(data.BookId, data.ItemName, data.ListPrice, 1);
cartItems.Add(bookId, newcartitem);

}
}
else
{
cartItem.Quantity = cartItem.Quantity + 1;
}
}
=====================
private static readonly ItemAccess dal = new ItemAccess();
public ItemInfo GetItem(string bookId)
{
if (string.IsNullOrEmpty(bookId))
{
return null;

}
else
{
return dal.GetItem(bookId);
}
}
==================
public class ItemAccess
{

public ItemInfo GetItem(string bookId)
{
SqlParameter[] sp = { new SqlParameter("@BookId", SqlDbType.Int) };
sp[0].Value = int.Parse(bookId);
DataBase db = new DataBase();
SqlDataReader sdr = null;
sdr = db.ExcuteDataReader(CommandType.StoredProcedure, "GetItemByBookId", sp);
ItemInfo item = new ItemInfo();
while (sdr.Read())
{
item.BookId = sdr.GetString(0);
item.BookAuthor = sdr.GetString(1);
item.Publisher = sdr.GetString(2);
item.PublishDate = sdr.GetString(3);
item.BookPrice = sdr.GetDecimal(4);
item.ListPrice = sdr.GetDecimal(5);
item.UnitCost = sdr.GetDecimal(6);
item.ItemName = sdr.GetString(7);
item.ItemImage = sdr.GetString(8);
item.Qty = sdr.GetInt32(9);
e78640946 2011-05-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lcl_data 的回复:]
item.BookId = sdr.GetString(0);
BookId 是int类型,但是的GetString返回string.试试sdr.GetInt(0);
[/Quote]

我试了 item.BookId = sdr.GetInt32(0);,结果成这样了:

------ 已启动生成: 项目: DAL, 配置: Debug Any CPU ------
未能找到所需版本的 Microsoft Windows SDK。查找了注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.0A”的“InstallationFolder”值中指定的位置。如果您的生成过程不需要 SDK,则可以忽略此问题。否则,您可以通过执行下列操作之一来解决这一问题: 1) 安装 Microsoft Windows SDK for Windows Server 2008 和 .NET Framework 3.5。 2) 安装 Visual Studio 2008。 3) 将上面的注册表项手动设置到正确的位置。
C:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:..\Common\bin\Debug\Common.dll /reference:..\Model\bin\Debug\Model.dll /reference:"D:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /reference:"D:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll" /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /reference:"D:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\Debug\DAL.dll /target:library BookBriefAccess.cs CartAccess.cs CategoryAccess.cs Class1.cs ItemAccess.cs OrderAccess.cs Properties\AssemblyInfo.cs
E:\BlueStarBookShop\DAL\ItemAccess.cs(25,31): 错误 CS0029: 无法将类型“System.Data.SqlTypes.SqlInt32”隐式转换为“string”
暖枫无敌 2011-05-04
  • 打赏
  • 举报
回复
public ItemInfo GetItem(string bookId)
{
SqlParameter[] sp = { new SqlParameter("@BookId", SqlDbType.Int) };
sp[0].Value = bookId;
。。。
}

看见没,是不是类型问题啊?

public ItemInfo GetItem(string bookId)
{
SqlParameter[] sp = { new SqlParameter("@BookId", SqlDbType.Int) };
sp[0].Value = int.Parse(bookId);
。。。
}
你打断点,跟踪一下就知道哪出问题了。
十八道胡同 2011-05-04
  • 打赏
  • 举报
回复
item.BookId = sdr.GetString(0);
BookId 是int类型,但是的GetString返回string.试试sdr.GetInt(0);
e78640946 2011-05-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 taomanman 的回复:]
你实体类BookId是int型的,你获取的是字符串,怎能相互赋值呢?

C# code

item.BookId = int.Parse(sdr.GetString(0));
或者是
item.BookId = sdr.GetInt(0);
[/Quote]
调试的时候,是ItemAccess类有问题:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookShop.Model;
using System.Data.SqlClient;
using System.Data;
using BookShop.Common;

namespace BookShop.DAL
{
public class ItemAccess
{

public ItemInfo GetItem(string bookId)
{
SqlParameter[] sp = { new SqlParameter("@BookId", SqlDbType.Int) };
sp[0].Value = bookId;
DataBase db = new DataBase();
SqlDataReader sdr = null;
sdr = db.ExcuteDataReader(CommandType.StoredProcedure, "GetItemByBookId", sp);
ItemInfo item = new ItemInfo();
while (sdr.Read())
{
item.BookId = sdr.GetString(0);
item.BookAuthor = sdr.GetString(1);
item.Publisher = sdr.GetString(2);
item.PublishDate = sdr.GetString(3);
item.BookPrice = sdr.GetDecimal(4);
item.ListPrice = sdr.GetDecimal(5);
item.UnitCost = sdr.GetDecimal(6);
item.ItemName = sdr.GetString(7);
item.ItemImage = sdr.GetString(8);
item.Qty = sdr.GetInt32(9);

}
return item;
}
//更新Item表数据
public void AddItem(ItemInfo info)
{
SqlParameter[] parm = {
new SqlParameter("@BookId", SqlDbType.Int),
new SqlParameter("BookAuthor",SqlDbType.NVarChar,50),
new SqlParameter("Publisher",SqlDbType.NVarChar,50),
new SqlParameter("PublishDate",SqlDbType.VarChar,10),
new SqlParameter("@BookPrice", SqlDbType.Decimal,8),
new SqlParameter("@ListPrice", SqlDbType.Decimal,8),
new SqlParameter("@UnitCost", SqlDbType.Decimal,8),
new SqlParameter("ItemImage",SqlDbType.VarChar,80),
new SqlParameter("@Qty",SqlDbType.Int)};
parm[0].Value = info.BookId;
parm[1].Value = info.BookAuthor;
parm[2].Value = info.Publisher;
parm[3].Value = info.PublishDate;
parm[4].Value = info.BookPrice;
parm[5].Value = info.ListPrice;
parm[6].Value = info.UnitCost;
parm[7].Value = info.ItemImage;
parm[8].Value = info.Qty;

DataBase db = new DataBase();
SqlDataReader sdr = db.ExcuteDataReader(CommandType.StoredProcedure, "SelectItemByBookId", parm);


//如果有库存,则更新产品数量,否则插入新记录
if (sdr.Read())
{
db.ExcuteNonQuery(CommandType.StoredProcedure, "UpdateItemQty", parm);

}
else
{
SqlParameter[] parm1 = {
new SqlParameter("@BookId", SqlDbType.Int),
new SqlParameter("BookAuthor",SqlDbType.NVarChar,50),
new SqlParameter("Publisher",SqlDbType.NVarChar,50),
new SqlParameter("PublishDate",SqlDbType.VarChar,10),
new SqlParameter("@BookPrice", SqlDbType.Decimal,8),
new SqlParameter("@ListPrice", SqlDbType.Decimal,8),
new SqlParameter("@UnitCost", SqlDbType.Decimal,8),
new SqlParameter("ItemImage",SqlDbType.VarChar,80),
new SqlParameter("@Qty",SqlDbType.Int)};
parm1[0].Value = info.BookId;
parm1[1].Value = info.BookAuthor;
parm1[2].Value = info.Publisher;
parm1[3].Value = info.PublishDate;
parm1[4].Value = info.BookPrice;
parm1[5].Value = info.ListPrice;
parm1[6].Value = info.UnitCost;
parm1[7].Value = info.ItemImage;
parm1[8].Value = info.Qty;

DataBase db1 = new DataBase();
db1.ExcuteNonQuery(CommandType.StoredProcedure, "InsertItem", parm1);
}

}
/// <summary>
/// 产品检索方法
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns>DataSet</returns>

public DataSet Search(string sql)
{
DataSet ds = new DataSet();//创建数据集对象
SqlDataAdapter da = null;//创建数据适配器对象
DataBase db = new DataBase();//创建数据库连接类实例
da = db.CreateDataAdapter(CommandType.Text, sql, null, 1);//获得用于查询的数据适配器对象
da.Fill(ds);//将查询结果填充到数据集中
return ds;//返回数据集

}

}
}
=====================可是修改之后不能运行~大侠救救小女子
暖枫无敌 2011-05-04
  • 打赏
  • 举报
回复
你实体类BookId是int型的,你获取的是字符串,怎能相互赋值呢?

item.BookId = int.Parse(sdr.GetString(0));
或者是
item.BookId = sdr.GetInt(0);

17,741

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧