过程或函数'sp_InsertRecord_tblEmployee'需要参数'@Name',这是未提供的。 ? [英] Procedure or function 'sp_InsertRecord_tblEmployee' expects parameter '@Name', which was not supplied. ?
问题描述
过程或函数'sp_InsertRecord_tblEmployee'需要参数'@Name',这是未提供的。
执行代码时出现上述错误原因?任何人都可以回答。
这里是源代码
BusinessLayer:
公开名单< 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 );
}
返回员工;
}
catch(例外情况)
{
throw;
}
}
实体是
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
public class Employee
{
public string Name { set; get; }
public string Gender { set; get; }
public string City { set; get; }
public DateTime Dateofjoining { set; get; }
}
控制器中的代码是
< br $> b $ b
员工员工=新员工();
[HttpGet]
公共ActionResult索引()
{
返回查看();
}
[HttpPost]
公共ActionResult索引(FormCollection表单)
{
BusinessLogic logic = new BusinessLogic();
employee.Name = form [Name];
employee.Gender = form [Gender];
employee.City = form [City ];
employee.Dateofjoining = Convert.ToDateTime(form [Dateofjoining]);
logic.AddEmployee();
返回查看(表格);
}
}
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);
}
}
推荐答案
cmd.Parameters.AddWithValue("@Name", objemp.Name);
检查objemp的值及其Name属性:确保它不是 null
。
如果是,它将作为BDNull.Value传递给SQL,除非它具有实际值,否则将假定它不存在。
只需在行上放置断点并在运行应用程序时查看该值。
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.
这篇关于过程或函数'sp_InsertRecord_tblEmployee'需要参数'@Name',这是未提供的。 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!