我在嘗試編寫項目代碼,但無效的特定轉換錯誤不斷出現。任何人都可以幫助我,因爲我難倒了。提前致謝。「指定的轉換無效」轉換ExecuteScalar的結果
Server Error in '/c#project' Application.
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 39: cmd.Parameters.Add("@ProductId", OleDbType.Char).Value = strProductId;
Line 40: object oQty = cmd.ExecuteScalar();
Line 41: int intQuantityOnHand = (int)oQty;
Line 42: mDB.Close();
Line 43: int intBuyQuantity = int.Parse(ddlQty.Items[ddlQty.SelectedIndex].ToString());
Source File: c:\Users\jacob\Desktop\c#project\ProductDetails.aspx.cs Line: 41
Stack Trace:
[InvalidCastException: Specified cast is not valid.]
ProductDetails.btnBuy_Click(Object sender, EventArgs e) in c:\Users\jacob\Desktop\c#project\ProductDetails.aspx.cs:41
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
cmd.ExecuteScalar()返回什麼?你檢查過了嗎? – BlackVegetable
我沒有看到任何錯誤的..嘗試int oQty = cmd.ExecuteScalar();那麼你不需要將其整理爲一個整數 – Thousand
嘗試在第40行放置斷點並檢查ExecuteScalar之後的oQty。這可能是SQL異常拋出和oQty爲空?你能否也顯示命令文本? – Dimitri