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

  • 1
    are you using hardcoded zero there or you have an expression there in your code? – Shyju Aug 10 '12 at 13:19
  • 1
    @DJKRAZE did you mean IDENTITY? IDENTITY and PRIMARY KEY are *not* the same thing. – Aaron Bertrand Aug 10 '12 at 13:24
  • @Shyju - I have used Hardcoded zero to test the code & for dj kraze - yes id is identity and pk as well. I am using if else for adding /updating with a single query.. If id =0 thn insert else update the id –  Aug 10 '12 at 13:27
  • Also make Id the IDENTITY if so change it to be Auto Field in the Database and set its Not null = true and remove @id from the first param. – MethodMan Aug 10 '12 at 13:27
  • Solved i replaced 0 with -1.. thanks –  Aug 10 '12 at 13:27
  • Amit that appears to be an incorrect way to do Inserts and updates, can you show how you are assigning @Id Hardcoding the values does not actually solve the issue so will all your rows have a value of -1 for Id and @id...? maybe I am missing something that you omitted from your code snippet – MethodMan Aug 10 '12 at 13:32

1 Answers1

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 )
MethodMan
  • 18,625
  • 6
  • 34
  • 52