描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.InvalidCastException:指定的强制转换无效。
我无法弄清楚为什么即使我的detailview正确完成,我仍然会收到错误。 变量已事先声明,大多数类都是正确的。 我正在使用visual C#为学校模块做一个电子商务网站。
//extract the QuantityOnHand from the database - based on the product ID
strSQLSelect = "SELECT pQuantity FROM Product WHERE pProdNo = @ProductID";
cmd = new OleDbCommand(strSQLSelect, mDB);
cmd.Parameters.Add("@ProductId", OleDbType.VarChar).Value = strProductId;
object oQty = cmd.ExecuteScalar();
intQuantityOnHand = (int)oQty; //error here
建议和帮助将不胜感激!
以下页面的整个编码:
static readonly string scriptStockOut = "<script language=\"javascript\">\n" +
"alert (\"Sorry Stock Out! Please choose a smaller quantity or another product \");\n" +
"</script>";
static readonly string scriptErrorLogin = "<script language=\"javascript\">\n" +
"alert (\"Please login or create account first to facilitate buying\");\n</script>";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnBuy_Click(object sender, EventArgs e)
{
//declare variables
string strProductId, strSQLSelect, strSQL;
int intQuantityOnHand, intBuyQuantity, newQty, intOrderNo;
decimal decUnitPrice;
// test to remind customer to login first
if ((string)Session["sFlag"] != "T")
{
Type csType = this.GetType();
ClientScript.RegisterStartupScript(csType, "Error", scriptErrorLogin);
}
// connect to database and then check stock quantity
OleDbConnection mDB = new OleDbConnection();
mDB.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data source="
+ Server.MapPath("~/Database.accdb");
mDB.Open(); OleDbCommand cmd;
//retrieve the product ID from the first row of the DetailsView
DetailsViewRow row0 = DetailsView1.Rows[0];
strProductId = row0.Cells[1].Text;
//extract the QuantityOnHand from the database - based on the product ID
strSQLSelect = "SELECT pQuantity FROM Product WHERE pProdNo = @ProductID";
cmd = new OleDbCommand(strSQLSelect, mDB);
cmd.Parameters.Add("@ProductId", OleDbType.VarChar).Value = strProductId;
object oQty = cmd.ExecuteScalar();
intQuantityOnHand = (int)oQty;
//extract the Promotion Price from the database - based on the product ID
strSQLSelect = "SELECT pPromoCost FROM Product WHERE pProdNo = @ProductID";
cmd = new OleDbCommand(strSQLSelect, mDB);
cmd.Parameters.Add("@ProductId", OleDbType.VarChar).Value = strProductId;
object oUnitPrice = cmd.ExecuteScalar();
decUnitPrice = (decimal)oUnitPrice;
//extract the quantity purchased from the dropdown list
intBuyQuantity = int.Parse(ddlQty.Items[ddlQty.SelectedIndex].ToString());
//calculate the new quantity left
newQty = intQuantityOnHand - intBuyQuantity;
//checking for out of stock situation
if (intQuantityOnHand < intBuyQuantity)
{
Type csType = this.GetType();
ClientScript.RegisterStartupScript(csType, "StockOut", scriptStockOut);
}
// save productID, unitPrice and quantity as Session variables
Session["sProductId"] = strProductId;
Session["sUnitPrice"] = decUnitPrice.ToString();
Session["sQuantity"] = newQty.ToString();
// insert product ordered to the orderItems table
intOrderNo = (int)Session["sOrderNo"];
strSQL = "INSERT INTO InvoiceDetails(idOrderNo, idProdID, idQty, idUnitPrice)"
+ "VALUES (@OrderNo, @ProductId, @Qty, @UnitPrice)";
cmd = new OleDbCommand(strSQL, mDB);
cmd.Parameters.Add("@OrderNO", OleDbType.Integer).Value = intOrderNo;
cmd.Parameters.Add("@ProductId", OleDbType.VarChar).Value = strProductId;
cmd.Parameters.Add("@Qty", OleDbType.Integer).Value = intBuyQuantity;
cmd.Parameters.Add("@UnitPrice", OleDbType.Currency).Value = decUnitPrice;
cmd.ExecuteNonQuery();
//update the quantity on hand in the products table
strSQL = "UPDATE Product SET pQuantity = @NewQty WHERE pProdNo = @ProductId";
cmd = new OleDbCommand(strSQL, mDB);
cmd.Parameters.Add("@ProductId", OleDbType.Integer).Value = newQty;
cmd.Parameters.Add("@NewQty", OleDbType.VarChar).Value = strProductId;
cmd.ExecuteNonQuery();
mDB.Close();
// redirect the user to the Shopping Cart page
Response.Redirect("ShoppingCart.aspx");
}
}