Click here to Skip to main content
15,908,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Procedure or function 'sp_InsertRecord_tblEmployee' expects parameter '@Name', which was not supplied.

getting the above mentioned error when executing the code why? can any one answer please.

here is the source code

BusinessLayer:

public List<employee> AddEmployee()
{
List<employee> employee = new List<employee>();
Employee objemp = new Employee();
try
{
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("sp_InsertRecord_tblEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", objemp.Name);
cmd.Parameters.AddWithValue("@Gender", objemp.Gender);
cmd.Parameters.AddWithValue("@City", objemp.City);
cmd.Parameters.AddWithValue("@DateofJoining", objemp.Dateofjoining.ToShortTimeString());
con.Open();
cmd.ExecuteNonQuery();
employee.Add(objemp);
}

return employee;
}
catch(Exception ex)
{
throw;
}


}

Entity Is

C#
public class Employee
    {
       public string Name { set; get; }
       public string Gender { set; get; }

       public string City { set; get; }

       public DateTime Dateofjoining { set; get; }
    }



Code in Controllers is


Employee employee = new Employee();
[HttpGet]
public ActionResult Index()
{


return View();
}
[HttpPost]
public ActionResult Index(FormCollection form)
{
BusinessLogic logic = new BusinessLogic();
employee.Name = form["Name"];
employee.Gender = form["Gender"];
employee.City = form["City"];
employee.Dateofjoining = Convert.ToDateTime(form["Dateofjoining"]);


logic.AddEmployee();

return View(form);
}

}
Posted
Comments
John C Rayan 14-Mar-15 8:32am    
Did you check if form["Name"] has got a value and debug to see whether or not objemp.Name has a valid valid when you pass it to SP.

1 solution

C#
cmd.Parameters.AddWithValue("@Name", objemp.Name);

Check the value of objemp and it's Name property: make sure it isn't null.
If it is, it'll be passed to SQL as BDNull.Value, which will be assumed to be not present unless it has an actual value.

Just put a breakpoint on the line and look at the value when you run your application.
 
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