ASP.NET Web-Form C# Specified cast is not valid -
i new asp.net , i'm trying make webshop. problem when try execute method, error.
i'm wondering if problem in sql tables? here's method produces error
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; }
and how execute above method:
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 !" }); }
and error:
specified cast not valid
from line in code
imgb.imageurl = "~/images/" + product.product_image;
it looks you're expecting data["product_image"]
of type string
, perhaps change cast to:
pr.product_image =(string)data["product_image"];
however, more useful add lines:
var productimage = data["product_image"]; type productimagetype = productimage.gettype();
you debug , step through code , find actual type of data["product_image"]
. amend cast in broken line whatever needs be.
Comments
Post a Comment