Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

SQL
CREATE TABLE [dbo].[Departments](

    [DepartmentID] [int] IDENTITY(1,1) NOT NULL,

    [DepartmentName] [nvarchar](50) NULL,

    [IsActive] [bit] NULL


Stored procedure spAddDepartment

SQL
ALTER Procedure [dbo].[spAddDepartment]

@DepartmentName    nvarchar(50)

as 

Begin 

  Insert into Departments values(@DepartmentName,1) 

End


Department Model

C#
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

C#
protected override void OnModelCreating(DbModelBuilder modelBuilder)

        {



            modelBuilder.Entity<Department>()

.MapToStoredProcedures(p => p.Insert(sp => sp.HasName("spAddDepartment").Parameter(pm => pm.DepartmentName, "DepartmentName")));



        }


Department controller

C#
[HttpPost]

        public ActionResult Insert(Department depart)

        {





            depart.IsActive = true;



            hr.Departments.Add(depart);

            hr.SaveChanges();

            return View(depart);

        }


Department view

HTML
<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>


What I have tried:

Procedure Or Function SpAddDepartment Has Too Many Arguments Specified
Posted
Updated 18-Feb-18 15:52pm

1 solution

This is a very clear error message and you'll have to debug what is happening as we cannot run all your code. You are passing more than one argument to SQL and the Stored Procedure only expects 1.

It may help to run SQL Profiler to see exactly what is being sent to SQL and that may help you track down where the problem is.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900