1

I have an insertparameter that is a non null nvarchar(MAX) and I set it using:

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

But it still say "Cannot insert the value NULL into column 'firstName'", what am I doing wrong?

4
  • Sounds like its comming from the DB side, does your coulmn allow it to be null? FirstName is a column where I wouldn't allow a null. Commented May 15, 2012 at 14:00
  • No column in that table allow NULL. Commented May 15, 2012 at 14:03
  • Have you tried using String.Empty to see if that works? Like this: sds_accountDetail.InsertParameters["firstName"].DefaultValue = String.Empty; Commented May 15, 2012 at 14:09
  • Didn't work, check my answer for the work around. Commented May 15, 2012 at 14:12

2 Answers 2

2

I was just looking for a solution to this problem, and I encountered this page as the first Google result. I know the question is from 2012, but I decided to post for the sake of other people having the same problem. The "right" way of solving this is setting the ConvertEmptyStringToNull property of the problematic Parameter to false. It can be done in the .aspx file, like this:

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

or in the code-behind, like this:

sds_accountDetail.InsertParameters["firstName"].ConvertEmptyStringToNull = false;
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent choice, I will mark this as the correct answer and use it in the future. Now after more than a year, the inferiority in using a white space to represent empty string is clear. This would have been a lot better.
1

"Solved" it by appending a space in the string:

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

1 Comment

Remember, thats not a null though, it may allow your code to run, but you could end up with bad data once users start editing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.