I am using VS 2015 and SQL Server 2016.
我正在使用VS 2015和SQL Server 2016。
In my C# code, I am assigning the parameters which my stored procedure expects, but am getting an error as my stored procedure is expecting a parameter.
在我的C#代码中,我正在分配我的存储过程所期望的参数,但由于我的存储过程需要一个参数,因此收到错误。
My stored procedure:
我的存储过程:
CREATE PROCEDURE [dbo].[SDDeleteTAR]
@TARKey int
AS
BEGIN
DELETE technical_assistance_requests
WHERE request_id = @TARKey
END
My C# code:
我的C#代码:
Label id = (Label)e.Item.FindControl("REQUEST_ID");
var x = id.Text;
int rid = Convert.ToInt32(x);
string cs = ConfigurationManager.AppSettings.Get("connectionString").ToString();
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("SDDeleteTAR", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@RequestID", rid);
con.Open();
cmd.ExecuteNonQuery();
}
When I run this, I get this error:
当我运行它时,我收到此错误:
Procedure or function 'SDDeleteTAR' expects parameter '@TARKey', which was not supplied.
过程或函数'SDDeleteTAR'需要参数'@TARKey',它未提供。
I am passing the required parameters and executing my command type as CommandType.StoredProcedure
, and am not able to resolve this.
我传递所需的参数并执行我的命令类型作为CommandType.StoredProcedure,并且我无法解决此问题。
I also checked the same issue in * here but the submitter is missing the command type declaration.
我还在这里检查了*中的相同问题,但提交者缺少命令类型声明。
Any help is appreciated, in pointing out where I am going wrong.
在指出我出错的地方时,我们非常感谢任何帮助。
2 个解决方案
#1
4
You should change @RequestID
to @TARKey
. Because your stored procedure expects a parameter with that name.
您应该将@RequestID更改为@TARKey。因为您的存储过程需要具有该名称的参数。
#2
1
You can safely execute a stored procedure without directly specifying the names, by using SqlCommandBuilder.DeriveParameters. This will retrieve the parameters from the server and create the SQLParameter collection for you.
通过使用SqlCommandBuilder.DeriveParameters,您可以安全地执行存储过程而无需直接指定名称。这将从服务器检索参数并为您创建SQLParameter集合。
SqlCommand cmd = new SqlCommand("SDDeleteTAR", con);
SqlCommandBuilder.DeriveParameters(cmd);
cmd.Parameters[0].Value = rid;
#1
4
You should change @RequestID
to @TARKey
. Because your stored procedure expects a parameter with that name.
您应该将@RequestID更改为@TARKey。因为您的存储过程需要具有该名称的参数。
#2
1
You can safely execute a stored procedure without directly specifying the names, by using SqlCommandBuilder.DeriveParameters. This will retrieve the parameters from the server and create the SQLParameter collection for you.
通过使用SqlCommandBuilder.DeriveParameters,您可以安全地执行存储过程而无需直接指定名称。这将从服务器检索参数并为您创建SQLParameter集合。
SqlCommand cmd = new SqlCommand("SDDeleteTAR", con);
SqlCommandBuilder.DeriveParameters(cmd);
cmd.Parameters[0].Value = rid;