将 sqldatasource 字符串插入参数的默认值设置为 “” 创建 NULL 错误

本文关键字:创建 错误 NULL 设置 默认值 字符串 sqldatasource 插入 参数 | 更新日期: 2023-09-27 18:34:09

>我有一个非空的插入参数 nvarchar(MAX),我使用以下方法设置它:

sds_accountDetail.InsertParameters["firstName"].DefaultValue = "";

但它仍然说"无法将值 NULL 插入到'firstName'列中",我做错了什么?

将 sqldatasource 字符串插入参数的默认值设置为 “” 创建 NULL 错误

我只是在寻找这个问题的解决方案,我遇到了这个页面作为第一个谷歌结果。我知道这个问题来自 2012 年,但我决定为了其他人有同样的问题而发布。解决此问题的"正确"方法是将有问题的ParameterConvertEmptyStringToNull属性设置为false。它可以在.aspx文件中完成,如下所示:

<InsertParameters>
    <!-- (...) -->
    <asp:Parameter Name="firstName" ConvertEmptyStringToNull="false" />
    <!-- (...) -->
</InsertParameters>

或者在代码隐藏中,如下所示:

sds_accountDetail.InsertParameters["firstName"].ConvertEmptyStringToNull = false;

通过在字符串中附加一个空格来"解决"它:

sds_accountDetail.InsertParameters["firstName"].DefaultValue = " ";