0

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

4

1 に答える 1

0

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 )
于 2012-08-10T13:25:29.700 に答える