简体   繁体   中英

Procedure or function 'AddUpdateContactDetails' expects parameter '@Id', which was not supplied

I have my method

 using (var helper = new DbHelper())
        {

            _commandText = "AddUpdateContactDetails";

            var parameter = new[]
                                {
                                    new SqlParameter("@Id", 0), // error here
                                    new SqlParameter("@CustomerId", id),
                                    new SqlParameter("@FirstName", customerDetails.FirstName),
                                    new SqlParameter("@LastName", customerDetails.LastName),
                                    new SqlParameter("@CompanyName", customerDetails.Company),
                                    new SqlParameter("@Designation", customerDetails.DesigId),
                                    new SqlParameter("@Title", customerDetails.Title),
                                    new SqlParameter("@Email", customerDetails.Email),
                                    new SqlParameter("@Phone", customerDetails.Phone),
                                };
            helper.ExecuteNonQuery(_commandText, CommandType.StoredProcedure, parameter);
        }

and Sp is below

Alter Procedure AddUpdateContactDetails
(

@Id int,
@CustomerId int,
@FirstName varchar(50),
@LastName varchar(50),
@CompanyName varchar(150),
@Designation int,
@Title int,
@Email varchar(50),
@Phone varchar(50)
)
AS
    BEGIN
        IF @Id<=0
            BEGIN
                INSERT INTO CustomerDetails
                           (FirstName,CustomerId,LastName, CompanyName, Designation, Title,Email,Phone)
                     VALUES
                           (@FirstName,@CustomerId,@LastName,@CompanyName,@Designation,@Title,@Email,@Phone )

            END
        ELSE
            BEGIN
               UPDATE CustomerDetails
               SET FirstName= @FirstName ,
                  LastName= @LastName ,
                  CompanyName= @CompanyName ,
                  Designation= @Designation ,
                  Title= @Title ,
                  Email= @Email ,
                  Phone= @Phone 
                WHERE Id= @Id
            END

END

But still getting the exception that "Procedure or function 'AddUpdateContactDetails' expects parameter '@Id', which was not supplied."

If this talbel that you are updating or inserting into does not have a IDENTITY , then your issues lies in this line of code

IF @Id<=0
            BEGIN
                INSERT INTO CustomerDetails
                           (FirstName,CustomerId,LastName, CompanyName, Designation, Title,Email,Phone)
                     VALUES
                           (@FirstName,@CustomerId,@LastName,@CompanyName,@Designation,@Title,@Email,@Phone )

just add @id to the Values and Id to your Insert command like this

IF @Id<=0
            BEGIN
                INSERT INTO CustomerDetails
                           (Id,FirstName,CustomerId,LastName, CompanyName, Designation, Title,Email,Phone)
                     VALUES
                           (@Id,@FirstName,@CustomerId,@LastName,@CompanyName,@Designation,@Title,@Email,@Phone )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM