[英]How do I pass a null value to a SqlDataSource that is using a stored procedure?
Using .NET 4 and C#. 使用.NET 4和C#。 All of these throw an error:
所有这些都会引发错误:
sdsTypeinfo.SelectParameters.Add("@TypeCode", DbType.Boolean, DBNull.Value);
sdsTypeinfo.SelectParameters.Add("@TypeCode", DBNull.Value);
sdsTypeinfo.SelectParameters.Add("@TypeCode", null);
sdsTypeinfo.SelectParameters.Add("@TypeCode", "");
The error indicates that the procedure is not getting any value at all. 该错误表明该过程根本没有获得任何值。
Procedure or function 'Typeinfo' expects parameter '@TypeCode', which was not supplied.
Answer 回答
You don't need the @ in the name of the parameter. 您不需要在参数名称中使用@。 This is different than adding parameters when calling the stored procedure using a SqlCommand, in which case you want something like this:
这与使用SqlCommand调用存储过程时添加参数不同,在这种情况下,您需要这样的内容:
cmd.Parameters.AddWithValue("@TypeCode", DBNull.Value);
Also, DBNull.value is an acceptable parameter in the above, but the accepted NULL value when adding SelectParameters to an SqlDataSource is simply "null". 同样,DBNull.value在上面是可接受的参数,但是将SelectParameters添加到SqlDataSource时可接受的NULL值就是“ null”。
There should be an option when adding a parameter to convert empty string to null. 添加将空字符串转换为null的参数时,应该有一个选项。
<SelectParameters>
<asp:ControlParameter ControlID="lstCategories" Name="ProductSubcategoryID" ConvertEmptyStringToNull="true" PropertyName="SelectedValue" />
</SelectParameters>
Since you're using a stored procedure, you'll want to make the parameter optional: 由于您使用的是存储过程,因此需要将参数设为可选:
@SomeParm INT = NULL
you can try like this... 你可以这样尝试...
string x = null;
command.Parameters.AddParameter(
new SqlParameter("@column", (object)x ?? DBNull.Value);
would give you a parm with a value of DBNull.Value, but 会给您一个具有DBNull.Value值的parm,但是
string x = "A String";
command.Parameters.AddParameter(
new SqlParameter("@column", (object)x ?? DBNull.Value);
In your stored procedure set the parameter to have a value eg 在您的存储过程中,将参数设置为一个值,例如
@TypeCode = ''
or if its an int 或者如果它是一个int
@typecode = 1
etc you get the idea. 等你明白了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.