Pass ID parameter to SQL from ASP .Net using stored procedure

1

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. 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:

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:

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.

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.

EDIT: I figured out the issue. I didn't need to reference the ID value.

c#
sql
asp.net
.net
stored-procedures
asked on Stack Overflow Nov 4, 2020 by N101Seggwaye • edited Nov 5, 2020 by Ian Kemp

1 Answer

1

You have an inconsistency in your parameters in that all your others are "@parmName", but your ID is just "Id" instead of "@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. 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.

answered on Stack Overflow Nov 4, 2020 by DRapp

User contributions licensed under CC BY-SA 3.0