过程或函数'过程名称'需要参数'@comments',这是未提供的。

问题描述:

我正在使用asp.net,我有一个数据库,其中我的一个字段注释在SQL Server 2012中具有数据类型varchar(max),并且值也可以为null。在数据库中使用存储过程来到asp.net时插入数据库表没有问题我没有在文本框中提供任何意味着空字符串的信息。我收到错误 过程或函数'过程名称'期望参数'@Comments',这是未提供的。



我尝试过:



数据库专栏:

I am using asp.net for which i have the database in which one of my field comments having datatype varchar(max) in SQL Server 2012 and the value can be null inserted also. In database there is no problem inserting into the database table while coming to asp.net using stored procedure i am not giving any information in the textbox that means empty string. I am getting error "Procedure or function 'procedure name' expects parameter '@Comments', which was not supplied."

What I have tried:

Database column:

[Comments] VARCHAR (MAX) NULL



.Net代码:


.Net Code:

string commentsText = (!string.IsNullOrEmpty(txtComments.Text)) ? txtComments.Text : null



命令Addwithvalue:


Command Addwithvalue:

cmd.Parameters.AddWithValue("@Comments", commentsText);



获取此行错误:


Getting Error this line:

cmd.ExecuteNonQuery();

错误的含义是你有一个存储过程需要一个名为@Comments的参数,并且参数不能是空值,这正是你传递的用户不输入任何内容:

What the error means is that you have a stored procedure which needs a parameter called @Comments and that the parameter cannot be a null value, which is exactly what you pass if the user doesn't enter anything:
string commentsText = (!string.IsNullOrEmpty(txtComments.Text)) ? txtComments.Text : null;

而不是这样,请尝试始终传递一个字符串:

Instead of this, try always passing a string:

cmd.Parameters.AddWithValue("@Comments", txtComments.Text.Trim());

这应该有效,因为TextBox的Text属性永远不能为空。

And that should work because the Text property of a TextBox can never be null.


问题出在我包含的数据库ISNULL ()方法,用于存储过程中所需的列,此值设置为列。
The problem is at the database i included ISNULL() method for the column i required in the stored procedure this value is setting to the column.