指定的转换无效。

Jermy120 2012-04-24 09:30:49
public List< PetShop.Model.ItemInfo> GetItemById(string ItemId)

{

string sql = "select item.itemid, item.name, item.listprice, product.name, item.image, product.categoryid, product.productid from item inner join product on item.productid = product.productid where item.itemid = @itemid";
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ssa"].ConnectionString;


List<PetShop.Model.ItemInfo> list = new List<PetShop.Model.ItemInfo>();
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();

command.Parameters.Add("@itemid", SqlDbType.NVarChar).Value = ItemId;

SqlDataReader reader = command.ExecuteReader();



if (reader.Read())
{


PetShop.Model.ItemInfo item = new PetShop.Model.ItemInfo()
{
ItemId = reader.GetString(0),
Name = reader.GetString(1),
Quantity = reader.GetInt32(2),
ListPrice = reader.GetDecimal(3),
ProductName = reader.GetString(4),
Image = reader.GetString(5),
CategoryId = reader.GetString(6),
ProductId = reader.GetString(7)
};

list.Add(item);

}
return list;

}





//////////////////////////////////////以下是报错的行/////////
PetShop.Model.ItemInfo item = new PetShop.Model.ItemInfo()
{
ItemId = reader.GetString(0),
Name = reader.GetString(1),
Quantity = reader.GetInt32(2),
ListPrice = reader.GetDecimal(3),
ProductName = reader.GetString(4),
Image = reader.GetString(5),
CategoryId = reader.GetString(6),
ProductId = reader.GetString(7)
...全文
75 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
快乐的小二兔 2012-04-24
  • 打赏
  • 举报
回复
还有可能是视图的结构与你的model结构不同
主要原因就是数据类型错误 一般是int和字符串类型
快乐的小二兔 2012-04-24
  • 打赏
  • 举报
回复
数据库的格式与你model的格式不同找到内个列改掉

如果是linq to sql的话 把表从新拖拽一下

7,765

社区成员

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

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