日期:2014-05-18  浏览次数:20814 次

调用存储过程时将value 设置为null 报错,请大家帮助看一下!
当txtLeaveDate .Text为空 时 ST_mycommand.Parameters["@enddate"].Value =null 出错误。提示没有为enddate 提供参数。对应表user_info 字段enddate 可以为null ,不知道为什么呀???
-------------------------------------
------winform 源码如下:
---------------------------------------
SqlConnection myConnection = new SqlConnection(str_conn);
  myConnection.Open();
  //利用Command对象调用存储过程
  SqlCommand ST_mycommand = new SqlCommand("User_info_add", myConnection);
  //将命令类型转为存储类型
  ST_mycommand.CommandType = CommandType.StoredProcedure;
  //往存储过程中添加参数
  ST_mycommand.Parameters.Add("@userno", SqlDbType.VarChar, 10);
  ST_mycommand.Parameters.Add("@username", SqlDbType.VarChar, 50);
  ST_mycommand.Parameters.Add("@userpwd", SqlDbType.VarChar, 6);
  ST_mycommand.Parameters.Add("@Empname", SqlDbType.VarChar, 50);
  ST_mycommand.Parameters.Add("@userrole", SqlDbType.VarChar, 50);
  ST_mycommand.Parameters.Add("@usersts", SqlDbType.VarChar, 10);
  ST_mycommand.Parameters.Add("@startdate", SqlDbType.DateTime);
  ST_mycommand.Parameters.Add("@enddate", SqlDbType.DateTime );
  ST_mycommand.Parameters.Add("@regioncode", SqlDbType.NChar, 10);
  ST_mycommand.Parameters.Add("@deptno", SqlDbType.NChar, 10);
  //给存储过程的参数赋值
  ST_mycommand.Parameters["@userno"].Value = txtEmpNo.Text.ToString().Trim();
  ST_mycommand.Parameters["@username"].Value = txtUserName.Text.Trim();
  ST_mycommand.Parameters["@userpwd"].Value = txtUserPwd.Text.Trim();
  ST_mycommand.Parameters["@userrole"].Value = "";// cmbUserRole.Text.Trim();
  ST_mycommand.Parameters["@Empname"].Value = cmbEmpName.Text;
  ST_mycommand.Parameters["@usersts"].Value = cmbUserSts.Text.Trim();
  ST_mycommand.Parameters["@startdate"].Value = DateTime.Now;/// txtStartDate.Text.ToString();
  ST_mycommand.Parameters["@enddate"].Value =txtLeaveDate .Text .ToString ().Trim ().Length==0?null:txtLeaveDate .Text .ToString ().Trim ();
  ST_mycommand.Parameters["@regioncode"].Value = frmlogin.M_str_region;
  ST_mycommand.Parameters["@deptno"].Value = txtDeptName.Text.Trim();
  ST_mycommand.ExecuteNonQuery();
--------------------------------------------
PRCERURE CODE :
--------------------------------------------
 PROCEDURE User_info_ADD
@userno varchar(10),
@username varchar(50),
@userpwd varchar(6),
@Empname varchar(50),
@userrole varchar(50),
@usersts varchar(10),
@startdate datetime,
@enddate datetime,
@regioncode nchar(10),
@deptno nchar(10)
 AS 
INSERT INTO [gy_User_info](
[userno],[username],[userpwd],[Empname],[userrole],[usersts],[startdate],[regioncode],[deptno],[enddate]
)VALUES(@userno,@username,@userpwd,@Empname,@userrole,@usersts,@startdate,@regioncode,@deptno,@enddate  
)





------解决方案--------------------
null换成DBNull.Value测试
------解决方案--------------------
ST_mycommand.Parameters["@enddate"].Value = txtLeaveDate.Text.Trim ().Length==0?DBNull.Value:txtLeaveDate.Text.Trim ();
------解决方案--------------------