none
How to insert Empty String RRS feed

  • Question

  • In my database I hava a varchar column not null and with default value '' -(empty string)

    But when my client want to add an object with empty string value, the entityframework auto  changes it to null ,then an error occured and prompt me xxx must be not null.

    How to insert an empty string value into a not null column
    or let it use the default value?
    Thanks for help

    Monday, October 10, 2011 9:20 PM

Answers

  • I think there are two things that might work that you can try:

    1.  Put the default value as " " instead of "" in the edmx.

    2.  In your code, if there is no value for your property (for example if you're getting it from a textbox and the user didn't populate anything), then don't even set the property at all on the entity.  This will ensure there is not even an attempt in the generated insert statement of putting anything into that column.  That is the only way to trigger the default value in sql.   So do something like this:

     

    //NOTE: assume lastname is set as NOT NULL in the database and has a default value for the sql table
    
    MyEntity entity = new MyEntity();
    
    if (textBoxLastName.Text == "")
    {
    MyEntity.FirstName = textBoxFirstName.Text;
    }
    else
    {
    MyEntity.FirstName = textBoxFirstName.Text;
    MyEntity.LastName = textBoxLastName.Text;
    }
    
    ctx.MyEntities.Add(MyEntity);
    ctx.SaveChanges();
    

    For this to work, you may have to take the default value out of the EDMX otherwise it may always try to pass a "" which will then be translated to NULL when it gets to sql.  

     

     

     


    Tom Overton


    Tuesday, October 11, 2011 10:45 PM

All replies

  • Hi weakinverse,

    What you want to do, is instead put a default value for your Entity.  Go to your EDMX designer and select your Entity.  Right-click on the column that is needing the default value and view the properties.  As you can see, there is a place for a Default Value for the entity.  Put "" there.

    That should now pass a true empty string when the user doesn't enter anything.   


    Tom Overton
    Monday, October 10, 2011 11:25 PM
  • Thanks for your advice

    I set the database table column to not null ( to force the value not null)

    and in the edmx designer i set the default value to ""

    But In my program if I don't set any value,it will return an Validation Error( Said the value must be required), And if I set the value to "", It alst return the error

     

     

    Tuesday, October 11, 2011 10:05 PM
  • I think there are two things that might work that you can try:

    1.  Put the default value as " " instead of "" in the edmx.

    2.  In your code, if there is no value for your property (for example if you're getting it from a textbox and the user didn't populate anything), then don't even set the property at all on the entity.  This will ensure there is not even an attempt in the generated insert statement of putting anything into that column.  That is the only way to trigger the default value in sql.   So do something like this:

     

    //NOTE: assume lastname is set as NOT NULL in the database and has a default value for the sql table
    
    MyEntity entity = new MyEntity();
    
    if (textBoxLastName.Text == "")
    {
    MyEntity.FirstName = textBoxFirstName.Text;
    }
    else
    {
    MyEntity.FirstName = textBoxFirstName.Text;
    MyEntity.LastName = textBoxLastName.Text;
    }
    
    ctx.MyEntities.Add(MyEntity);
    ctx.SaveChanges();
    

    For this to work, you may have to take the default value out of the EDMX otherwise it may always try to pass a "" which will then be translated to NULL when it gets to sql.  

     

     

     


    Tom Overton


    Tuesday, October 11, 2011 10:45 PM