简体   繁体   English

使用存储过程将 ID 参数从 ASP .Net 传递给 SQL

[英]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.

相关问题 无法将字符串作为参数传递给ASP.NET MVC中的存储过程 - Unable to pass string as a parameter to a stored procedure in ASP.NET MVC ASP.NET MVC 将参数从 controller 传递到没有实体框架的存储过程 - ASP.NET MVC pass parameter to stored procedure from controller without Entity Framework 如何将表 OBJECT 输出参数从 C# ASP.NET 核心传递到 Z30162ED78B6C10F231411F2F4 - How to pass a TABLE OBJECT out parameter from C# ASP.NET Core to Oracle Stored Procedure 使用输出参数在ASP.NET中调用SQL存储过程 - Call a SQL stored procedure in ASP.NET with an output parameter ASP.NET和SQL将类传递给存储过程? - ASP.NET and SQL pass a class to a stored procedure? 是否可以从API传递模型数据作为SQL存储过程的参数 - Is it possible to pass a model data as a parameter of SQL stored procedure from API 从ASP.NET WinForm向存储过程传递参数 - Passing parameter to stored procedure from ASP.NET WinForm 如何使用asp.net读取没有参数的存储过程返回的日期? - How to read the date that is return from a stored procedure which have no parameter using asp.net? 在 ASP.Net Core 项目中使用 ADO.Net 将 JSON 类型作为参数传递给 SQL Server 2016 存储过程 - Passing JSON type as parameter to SQL Server 2016 stored procedure using ADO.Net in ASP.Net Core project 使用ObjectDataSource将参数传递给存储过程 - Pass a Parameter to a Stored Procedure using ObjectDataSource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM