[英]Pass ID parameter to SQL from ASP .Net using stored procedure
I am trying to call up an UPDATE stored procedure in SQL by passing parameters and it need to update at a specific ID value.我试图通过传递参数来调用 SQL 中的 UPDATE 存储过程,它需要在特定的 ID 值处进行更新。 This is the error I keep getting:这是我不断收到的错误:
System.Data.SqlClient.SqlException: Procedure or function 'updateEmployeeRecord' expects parameter '@Id', which was not supplied.
Stack trace;堆栈跟踪;
[SqlException (0x80131904): Procedure or function 'updateEmployeeRecord' expects parameter '@Id', which was not supplied.]
EmployeeClass.Data.EmployeeList.EditEmpInfo(String connectionString, EmployeeList empInfo) in C:\Users\nsamuels\Documents\Induction Exercises\MasterPageTest\Exercise2\EmployeeData\Data\EmployeeClass.cs:120
EmployeeData.About.btnAdd_Click(Object sender, EventArgs e) in C:\Users\nsamuels\Documents\Induction Exercises\MasterPageTest\Exercise2\EmployeeData\About.aspx.cs:80
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9774694
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +211
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1696
Code in ASP .NET: ASP .NET 中的代码:
public void EditEmpInfo(string connectionString, EmployeeList empInfo)
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
string proc2 = "updateEmployeeRecord";
SqlCommand cmd = new SqlCommand(proc2, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@IDNUmber", empInfo.IDNumber));
cmd.Parameters.Add(new SqlParameter("@EmployeeNumber", empInfo.EmployeeNumber));
cmd.Parameters.Add(new SqlParameter("@EmployeeSurname", empInfo.employeeSurname));
cmd.Parameters.Add(new SqlParameter("@EmployeeName", empInfo.employeeName));
cmd.Parameters.Add(new SqlParameter("@NumOfDependants", empInfo.numberOfDependants));
cmd.Parameters.Add(new SqlParameter("@Employee_RaceId", empInfo.RaceId));
cmd.Parameters.Add(new SqlParameter("@Employee_GenderId", empInfo.GenderId));
cmd.ExecuteNonQuery();
con.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
SQL Stored Procedure: SQL 存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[updateEmployeeRecord]
@Id int,
@EmployeeNumber varchar(50),
@IDNumber varchar(50),
@EmployeeSurname varchar(50),
@EmployeeName varchar(50),
@NumOfDependants int,
@Employee_RaceId int,
@Employee_GenderId int
AS
BEGIN
UPDATE dbo.Employees
SET
EmployeeNumber = @EmployeeNumber,
IDNumber = @IDNumber,
EmployeeSurname = @EmployeeSurname,
EmployeeName = @EmployeeName,
NumOfDependants = @NumOfDependants,
Employees_RaceId = @Employee_RaceId,
Employees_GenderId = @Employee_GenderId
WHERE Id =@Id
END
If I add the below line, nothing happens, it just returns to Default.aspx.如果我添加下面的行,什么也不会发生,它只是返回到 Default.aspx。
cmd.Parameters.Add(new SqlParameter("Id", empInfo.id));
What I need it to do essentially is update the record of the database at a specific ID even if it's only one value.我需要它做的本质上是更新特定 ID 的数据库记录,即使它只有一个值。
EDIT: I figured out the issue.编辑:我想出了这个问题。 I didn't need to reference the ID value.我不需要引用 ID 值。
You have an inconsistency in your parameters in that all your others are "@parmName", but your ID is just "Id" instead of "@Id".您的参数不一致,因为所有其他参数都是“@parmName”,但您的 ID 只是“Id”而不是“@Id”。 Dont know if thats it or not.不知道是不是这样。 However, I have always hated having parameter names match the exact column name in a table.但是,我一直讨厌参数名称与表中的确切列名称相匹配。 I would typically have my parameter names start with "@p" to indicate its a PARAMETER variable and not just the name of the column.我通常让我的参数名称以“@p”开头,以表示它是一个 PARAMETER 变量,而不仅仅是列的名称。 Prevents ambiguity when reading.防止阅读时产生歧义。 So, that is one suggestion.所以,这是一个建议。
Also, the stored procedure might be choking because you are not calling the procedure identifying the parms such as此外,存储过程可能会阻塞,因为您没有调用识别参数的过程,例如
string proc2 = "updateEmployeeRecord( @Id, @EmployeeNumber, @IDNumber, @restOfParms )";
THEN defining your "@" parameters as command parameters to match the placeholders in the updateEmployeeRecord from proc2 string.然后将“@”参数定义为命令参数,以匹配 proc2 字符串中 updateEmployeeRecord 中的占位符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.