将INT传递给存储过程失败

Chr*_*ker 5 c# sql-server int stored-procedures

我试图传递一个CommandArgumentas int我的存储过程,但我得到:

无法将参数值从String转换为Int32

即使它在我通过它之前被转换了......

C#代码:

cmd.CommandText = "deleteProd";
int delprodID = Convert.ToInt32(e.CommandArgument.ToString());

Response.Write(delprodID);

cmd.Parameters.Add(new SqlParameter("@prodID", SqlDbType.Int)).Value = delprodID;
cmd.CommandType = CommandType.StoredProcedure;

sqlTools.cmdToNonQuery(cmd)
Run Code Online (Sandbox Code Playgroud)

我的Response.Write节目表明我有正确的身份证delprodID.

存储过程:

ALTER PROCEDURE dbo.deleteProd @prodID int
AS
IF @prodID > 0
BEGIN
    DELETE FROM products WHERE prodID = @prodID
END
Run Code Online (Sandbox Code Playgroud)

aio*_*sov 1

尝试使用SqlParameterCollection.AddWithValue 方法 它将大大简化事情。

cmd.CommandText = "deleteProd";
int delprodID = Convert.ToInt32(e.CommandArgument.ToString());
Response.Write(delprodID);
cmd.Parameters.AddWithValue("@prodID", delprodID);
cmd.CommandType = CommandType.StoredProcedure;
sqlTools.cmdToNonQuery(cmd)
Run Code Online (Sandbox Code Playgroud)