如何解决这个异常? [英] how to solve this exception ?

查看:88
本文介绍了如何解决这个异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公开名单< employee> AddEmployee()

{

List< employee> employee = new List< employee>();

Employee objemp = new Employee();

try

{

using(SqlConnection con = new SqlConnection(cs))

{

SqlCommand cmd = new SqlCommand(sp_insert_employee_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;

}

返回员工;



}

}



这个方法有什么问题请有人帮帮我



得到以下异常。





过程或函数'sp_insert_employee_tblEmployee'需要参数'@Name',这是未提供的。



程序是 -



创建程序[dbo]。[sp_insert_employee_tblEmployee]

@Name varchar(50)NULL,

@Gender varchar(50)NULL,

@City Varchar(50)NULL,

@Dateofjoining datetime

as

begin

插入tblEmployee值(@ Name,@ Gender,@ City,@ Dateofjoining)

结束

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_insert_employee_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(Exception ex)
{
throw;
}
return employee;

}
}

What is wrong with this method please can anybody help me

getting following exception.


Procedure or function 'sp_insert_employee_tblEmployee' expects parameter '@Name', which was not supplied.

Procedure is --

Create procedure [dbo].[sp_insert_employee_tblEmployee]
@Name varchar(50) NULL,
@Gender varchar(50) NULL,
@City Varchar (50) NULL,
@Dateofjoining datetime
as
begin
Insert into tblEmployee values(@Name,@Gender,@City,@Dateofjoining)
End

推荐答案

当objemp发生时.Name为null。当发生这种情况时,你必须传递DBNull.Value。
This happens when objemp.Name is null. You have to pass DBNull.Value instead when that happens.


这篇关于如何解决这个异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆