3 Replies - 663 Views - Last Post: 17 February 2018 - 02:11 AM

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 24-January 14

Procedure or function spAddDepartment has too many arguments specified

Posted 16 February 2018 - 02:37 PM

Problem
I get error Procedure or function spAddDepartment has too many arguments specified
How to solve it

Details
when insert data to table department using stored procedure spAddDepartment using fluent api in mvc5
I get error above
Table department
CREATE TABLE [dbo].[Departments](
	[DepartmentID] [int] IDENTITY(1,1) NOT NULL,
	[DepartmentName] [nvarchar](50) NULL,
	[IsActive] [bit] NULL


spAddDepartment
ALTER Procedure [dbo].[spAddDepartment]
@DepartmentName    nvarchar(50)
as  
Begin  
  Insert into Departments values(@DepartmentName,1)  
End

public partial class Department
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Department()
        {
            Employees = new HashSet<Employee>();
        }

        public int DepartmentID { get; set; }

        [StringLength(50)]
        public string DepartmentName { get; set; }

        public bool? IsActive { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Employee> Employees { get; set; }
    }

database context
protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           
            modelBuilder.Entity<Department>()
.MapToStoredProcedures(p => p.Insert(sp => sp.HasName("spAddDepartment").Parameter(pm => pm.DepartmentName, "DepartmentName")));
            
        }

[HttpPost]
        public ActionResult Insert(Department depart)
        {
           
          
            depart.IsActive = true;
            
            hr.Departments.Add(depart);
            hr.SaveChanges();
            return View(depart);
        }

view as following
 <div class="form-horizontal">
        <h4>Department</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.DepartmentName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DepartmentName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DepartmentName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.IsActive)
                    @Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>


Is This A Good Question/Topic? 0
  • +

Replies To: Procedure or function spAddDepartment has too many arguments specified

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14852
  • View blog
  • Posts: 59,263
  • Joined: 12-June 08

Re: Procedure or function spAddDepartment has too many arguments specified

Posted 16 February 2018 - 02:42 PM

6  Insert into Departments values(@DepartmentName,1) 

Typically INSERTs like you to list out the column names AFTER the table name, but BEFORE the VALUES.

INSERT INTO <table name> (<col1>, <col2>, ...)
VALUES (<val1>, <val2>, ...)

Was This Post Helpful? 0
  • +
  • -

#3 ahmedba   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 24-January 14

Re: Procedure or function spAddDepartment has too many arguments specified

Posted 16 February 2018 - 02:56 PM

meaning what i do to solve problem
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6736
  • View blog
  • Posts: 27,734
  • Joined: 12-December 12

Re: Procedure or function spAddDepartment has too many arguments specified

Posted 17 February 2018 - 02:11 AM

Alternatively, using your current approach, you need to supply a value for every field of the table, and in the correct order. For the identity field I believe that you would supply NULL (or DEFAULT?) for the value so that it is still automatically generated.

Personally, I prefer modi's advice: always specify the column names, and just ignore the identity column-name.

Quote

meaning what i do to solve problem


Did you read and understand modi's post? If not, ask a specific question about it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1