ASP.NET Web 窗体 C# 指定的转换无效
ASP.NET Web-Form C# Specified cast is not valid
我是 ASP.NET 的新手,我正在尝试制作网上商店。
问题是当我尝试执行一个方法时,出现错误。
我想知道问题是否出在 SQL 表中?
这是产生错误的方法
public List<Product> productRead(int Product_ID)
{
IDataReader data = SqlHelper.ExecuteReader(con, "Product_Read", Product_ID);
List<Product> prod = new List<Product>();
while(data.Read())
{
Product pr = new Product();
pr.Product_ID=(int)data["Product_ID"];
pr.Product_Name=data["Product_Name"]as string;
pr.Product_Description=data["Product_Description"]as string;
pr.Product_Image =(char)data["Product_Image"];
pr.Product_Price=(int)data["Product_Price"];
pr.Product_Count=(int)data["Product_Count"];
prod.Add(pr);
}
return prod;
}
这就是我执行上述方法的方式:
List<Product> pr = db.Instance.productRead(12);
if (pr != null)
{
foreach (Product product in pr)
{
Panel prPanel = new Panel();
ImageButton imgB = new ImageButton();
Label lbName = new Label();
Label lblPrice = new Label();
imgB.ImageUrl = "~/Images/" + product.Product_Image;
imgB.PostBackUrl = "~/Home.aspx?id=" + product.Product_ID;
lbName.Text = product.Product_Name;
lblPrice.Text = "$" + product.Product_Price;
prPanel.Controls.Add(imgB);
prPanel.Controls.Add(new Literal { Text = "<br/>" });
prPanel.Controls.Add(lbName);
prPanel.Controls.Add(new Literal { Text = "<br/>" });
prPanel.Controls.Add(lblPrice);
pnlProducts.Controls.Add(prPanel);
}
}
else
{
pnlProducts.Controls.Add(new Literal { Text = "No product found !" });
}
错误:
Specified cast is not valid
从您代码中的这一行开始
imgB.ImageUrl = "~/Images/" + product.Product_Image;
您似乎希望 data["Product_Image"]
属于 string
类型,因此或许可以将您的演员表更改为:
pr.Product_Image =(string)data["Product_Image"];
但是,添加以下行对您更有用:
var productImage = data["Product_Image"];
Type productImageType = productImage.getType();
然后您可以调试并单步执行代码并找到 data["Product_Image"]
的实际类型。然后,您可以将虚线中的演员表修改为所需的任何内容。
我是 ASP.NET 的新手,我正在尝试制作网上商店。 问题是当我尝试执行一个方法时,出现错误。
我想知道问题是否出在 SQL 表中? 这是产生错误的方法
public List<Product> productRead(int Product_ID)
{
IDataReader data = SqlHelper.ExecuteReader(con, "Product_Read", Product_ID);
List<Product> prod = new List<Product>();
while(data.Read())
{
Product pr = new Product();
pr.Product_ID=(int)data["Product_ID"];
pr.Product_Name=data["Product_Name"]as string;
pr.Product_Description=data["Product_Description"]as string;
pr.Product_Image =(char)data["Product_Image"];
pr.Product_Price=(int)data["Product_Price"];
pr.Product_Count=(int)data["Product_Count"];
prod.Add(pr);
}
return prod;
}
这就是我执行上述方法的方式:
List<Product> pr = db.Instance.productRead(12);
if (pr != null)
{
foreach (Product product in pr)
{
Panel prPanel = new Panel();
ImageButton imgB = new ImageButton();
Label lbName = new Label();
Label lblPrice = new Label();
imgB.ImageUrl = "~/Images/" + product.Product_Image;
imgB.PostBackUrl = "~/Home.aspx?id=" + product.Product_ID;
lbName.Text = product.Product_Name;
lblPrice.Text = "$" + product.Product_Price;
prPanel.Controls.Add(imgB);
prPanel.Controls.Add(new Literal { Text = "<br/>" });
prPanel.Controls.Add(lbName);
prPanel.Controls.Add(new Literal { Text = "<br/>" });
prPanel.Controls.Add(lblPrice);
pnlProducts.Controls.Add(prPanel);
}
}
else
{
pnlProducts.Controls.Add(new Literal { Text = "No product found !" });
}
错误:
Specified cast is not valid
从您代码中的这一行开始
imgB.ImageUrl = "~/Images/" + product.Product_Image;
您似乎希望 data["Product_Image"]
属于 string
类型,因此或许可以将您的演员表更改为:
pr.Product_Image =(string)data["Product_Image"];
但是,添加以下行对您更有用:
var productImage = data["Product_Image"];
Type productImageType = productImage.getType();
然后您可以调试并单步执行代码并找到 data["Product_Image"]
的实际类型。然后,您可以将虚线中的演员表修改为所需的任何内容。