指定演员表无效

我正在尝试使用以下代码从我的数据库中获取Score_Waarde值:

 critid = critid_arr[teller2].ToString(); int scorehulp = 0; string userid = Session["userid"].ToString(); SqlCommand cmd3 = new SqlCommand("SELECT Score_Waarde FROM Score WHERE Crit_ID = '" + critid + "' AND User_ID = '" + userid + "' ", con); scorehulp = (int)cmd3.ExecuteScalar(); 

当我尝试运行它时,我收到以下错误: 指定的强制转换无效 。 我不明白为什么我收到此错误,因为critiduserid给出了正确的值。

您的SQL可能会返回不同的数字类型,例如longdecimal ,或者显然是string

调用Convert.ToInt32 ,它没有取消装箱强制转换的限制 。

我最初得到这个错误(我的C#代码

 someobject.TotalItemCount = (Int32)dbManager.GetParameterValue("TotalRows")) 

其中TotalItemCountint类型,即使SP很好并且给出了正确的结果。 我通过添加以下代码修复SP来摆脱这个问题

 Set @TotalRows = @@rowcount.