【问题标题】:"Specified cast is not valid" casting the results of ExecuteScalar“指定的转换无效”转换 ExecuteScalar 的结果
【发布时间】:2013-06-13 03:39:39
【问题描述】:

我正在尝试为一个项目编写代码,但不断出现无效的特定转换错误。任何人都可以帮助我,因为我很难过。提前致谢。

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() 返回什么?你检查了吗?
  • 我看不出有什么问题。尝试 int oQty = cmd.ExecuteScalar();那么您不需要将其装箱为整数
  • 尝试在第 40 行放置断点并检查 ExecuteScalar 之后的 oQty 是什么。可能是抛出了sql异常并且oQty为null?你能也显示命令文本吗?
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。

标签: c# asp.net


【解决方案1】:

第 41 行:显然 oQty 不能转换为 Int32。试试

int intQuantityOnHand = Convert.ToInt32(oQty);

【讨论】:

    【解决方案2】:

    我相信错误在第 41 行。在第 41 行下一个断点,看看 oQty 的值是多少。它可能为空。

    【讨论】:

      【解决方案3】:

      如果您的结果集为空,您应该进行空检查。

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

      那个,或者默认你的值...

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

      根据 MSDN,ExecuteScalar 返回

      [t]结果集中第一行的第一列,或者为空 如果结果集为空,则引用(在 Visual Basic 中为 Nothing)。 返回最多 2033 个字符。

      【讨论】:

        【解决方案4】:

        试试这个:

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

        然后检查if(intQuantityOnHand !=null)

        【讨论】:

          【解决方案5】:

          ExecuteScalar 返回The first column of the first row in the result set, or a null reference。这可以是任何东西;整数、null、字符串、varbinary。您必须检查查询的第一列的类型并分配给该类型的变量。

          还有,你为什么要这样做:

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

          您将某些内容转换为字符串,然后将字符串转换为整数。为什么?它是一个字符串吗?那么这可能会引发异常。是整数吗?然后将其读取为整数。你在用System.Data.SqlClient吗?它包含像GetInt32 这样的方法,它们返回正确类型的数据;您不需要强制转换、解析或其他任何操作。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-25
            • 1970-01-01
            • 2017-03-12
            • 2012-01-15
            • 1970-01-01
            相关资源
            最近更新 更多