"The specified cast is invalid" by casting ExecuteScalar results

I am trying to create code for a project, but an invalid specific error at startup continues to exit. Can someone help me since I'm at a dead end. Thanks in advance.

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 
+4
source share
5 answers

Line 41: apparently oQty cannot be added to Int32 . Try

 int intQuantityOnHand = Convert.ToInt32(oQty); 
+4
source

I believe the error is on line 41. Put a breakpoint on line 41 and see what oQty is. It can be zero.

0
source

You should have a zero check if your result set is empty.

 if(oQty!=null){ int intQuantityOnHand = (int)oQty; } 

This or default value ...

  int intQuantityOnHand = (oQty==null) ? 0 : (int)oQty; 

In MSDN, ExecuteScalar returns

[t] the first column of the first row in the result set or null reference (Nothing in Visual Basic) if the result set is empty. Returns a maximum of 2033 characters.

0
source

try the following:

 object oQty = cmd.ExecuteScalar(); int? intQuantityOnHand = (oQty as int); 

and then check if(intQuantityOnHand !=null)

0
source

ExecuteScalar returns the first column of the first row in the result set or a null reference . It could be anything; integer, zero, string, varbinary. You must examine the type of the first column of your query and assign a variable of this type.

Also, why are you doing this:

 int intBuyQuantity = int.Parse(ddlQty.Items[ddlQty.SelectedIndex].ToString()); 

You convert something to a string, and then a string to an integer. What for? Is this a string? Then this may throw an exception. Is this an integer? Then read it as an integer. Are you using System.Data.SqlClient ? It contains methods of type GetInt32 that return data of the corresponding type; You don’t need to throw, disassemble or anything else.

0
source

All Articles