2

I'm having to work with a database table, that for whatever reason, requires all entries to have a value(NOT NULL). Not all entries are relevant to the user and they will be leaving entries blank when entering them through a webform. I'm using a FormView databound using Entity Framework.

The problem I'm running into is, that instead of entering an empty or blank string for TextBoxes with no text, it is trying to use a NULL value. Is there a way to just enter a blank string to appease the database constraints?

I wish I could just change these in the database to allow nulls, but unfortunately I don't have access.

Here's a snippet from the aspx:

 <asp:TextBox ID="CustIdTextBox" runat="server" Text='<%# Bind("CustId") %>' />

It looks like entity framework is taking the empty string and deciding to save it to the database as NULL.

3
  • 1
    Can you show us your code? Commented May 1, 2013 at 15:08
  • That's from the aspx file. Anything else in particular you'd want to see? Commented May 1, 2013 at 15:09
  • 2
    stackoverflow.com/questions/6548304/… Commented May 1, 2013 at 15:10

1 Answer 1

1

Well we definietly need some code, but I'm guessing the textbox.text property is not initialized, so it's going to give you NULL. Just initialize it to an empty string, or replace the null with string.empty before you update the DB.

Sign up to request clarification or add additional context in comments.

7 Comments

Upon seeing the code, make sure the CustId field is initialized to string.empty, otherwize it is going to be NULL by default.
Not really, because if you do string str; then str will be null. String is an object, which means you need to instantiate it first.
Yes, wherever you are declaring the CustId variable. Or, before you send to DB, change the NULL to string.Empty
I'm initializing the strings, they're there, just blank. For whatever reason, entity framework is taking the empty string and converting it to NULL.
Entity Framework on the DbContext call to SaveChanges did validation and interpreted empty strings as NULL. To make EF interpret empty strings as empty, I added the annotation, [Required(AllowEmptyStrings=true)] , to my Code First POCO.
|

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.