THESE FORUMS ARE NOW FROZEN
Please choose "Forums" from the Main menu of www.entityspaces.net to get to our new forums.

String.Empty = Null in DB

rated by 0 users
This post has 24 Replies | 1 Follower

Top 150 Contributor
Posts 18
no1ross Posted: 04-10-2009 7:00 AM

Hi,

 I just upgraded to 2009 and I have noticed that when i save an empty string in MSSQL it now stores the value as '' instead of NULL like it used to in 2008 i.e.

CurrentEvent.EventTrigger = rcbEventTrigger.SelectedValue

CurrentEvent.Save()

If the SelectedValue was '' it would put NULL in the Table now it puts '' in the table and it is messing my whole application up, and so you know I do this in more than 3,000 places in my code, its a huge application :).  So now where i used to do a where on

tblE.Where(tblE.TriggerControl.IsNull)

 It does not work becuase there is a '' value in the column instead of NULL.

 I hope i explained this decently,

Thanks,
Andrew

Top 10 Contributor
Posts 1,675

As far as I know, nothing has changed in this regard since ES 2007. EntitySpaces passes whatever it is told to on to the server. Whether a VARCHAR is set to a value, an empty string, or NULL is up to the developer and the database schema. Here is an excerpt from an NUnit test that is in the ES 2007, 2008, and 2009 SVN branches, and passes in all three. FirstName and LastName allow NULLs and have no DEFAULT set in the schema.

Code:
// Set the LastName to null
// FirstName to a empty string
aggTest.FirstName = String.Empty;
aggTest.LastName = null;
aggTest.Save();

// Re-read it, confirm null/empty
aggTest = new AggregateTest();
aggTest.LoadByPrimaryKey(id);

Assert.AreEqual("", aggTest.FirstName);
Assert.IsNotNull(aggTest.FirstName);
Assert.IsNull(aggTest.LastName);

Are you sure nothing else has changed? Maybe you were using StoredProcedure mode in ES 2008 with an SP that prevented empty strings, and are now using ES DynamicSQL mode? Or, maybe a column DEFAULT of '' has been added to the column in the schema?

David Neal Parsons
www.entityspaces.net

Top 150 Contributor
Posts 18

It might be a vb  thing then, here use the following, as it does like i say, its really odd.

Code:
        Dim tbl1 As New FormEvents
        tbl1.AddNew()
        tbl1.TriggerControl = "" 'inserts '' in the DB
        tbl1.Save()
        Dim tbl2 As New FormEvents
        tbl2.AddNew()
        tbl2.TriggerControl = Nothing 'inserts a Null in the DB
        tbl2.Save()
Code:
18	1	1	1	
19	1	1	1	NULL

Could it have to do with me moving from asp.net 2.0 to 3.5? or is there something else i'm missing?  Before i upgraded to 3.5 and es 2009, the "" always inserted Null into the tables, i'm not sure what else could have changed, nothing in the DB has i'm pretty positive.

Thanks for your help,
Andrew

Top 150 Contributor
Posts 18

BTW the app may have been writtin in 2007, I can't remember.  The main reason this is bugging me is that now I have to and change TONS of code, just so it will insert NULL's in the DB, I have over 200 reports, with about 1,000 stored procedures that rely on the IS NULL or IS NOT NULL, which the '' is not a null, so most all of my reports are broke right now :(.

Top 10 Contributor
Posts 1,675

I do not believe it's a VB/C# thing. The behavior of the code you posted is not odd. It is exactly what is tested for by the NUnit test. If you pass an empty string, it stores ''. If you pass a null, it stores NULL. ES has given the developer this flexibility for years.

Is it possible that this line for ASP.NET 2.0 set the value to null (not an empty string), and that is why it was saved as a NULL to the db?

CurrentEvent.EventTrigger = rcbEventTrigger.SelectedValue

I'm not sure what control that is. Has its behavior changed for ASP.NET 3.5, so that SelectedValue is now an empty string, rather than null?

David Neal Parsons
www.entityspaces.net

Top 150 Contributor
Posts 18

Ok, thanks for the info,

 I'm just going to take your word for it, and change all the code some 5,000 controls that used to work, i'm going to attribute it to upgrade pains, but that is alright my client is paying for it :).

The conrol being used in the example above is a Telerik RadComboBox, but I have the same type of things with TextBox.Text, Dropdown.SelectedValue, all of these used to insert null  when the values where ''.  It's been over 2 years since i've worked on this project so I might be missing something else, but I upgraded from 2.0 to 3.5, es2007 or 8 to 2009, Telerik from 2007 to 2009.  I have a mixture of both Telerik and ASP.net controls, not sure what happen, but like I said i'm just going to charge the client to fix everything :).

 Andrew

Top 50 Contributor
Posts 47

I'm with Andrew on this one.

I am using 2009.1.209.0, C#, .NET 3.5, SQL Server 2005 and when I insert/update using an empty ASP TextBox, an empty string is inserted into the database.   I am pretty sure that previously ES would insert NULL in such cases.

Ref: http://community.entityspaces.net/forums/permalink/2196/2219/ShowThread.aspx#2219

This is what I tried as a test

 

Code:
1     protected void Button1_Click(object sender, EventArgs e)
2 {
3 SiteConfiguration config = new SiteConfiguration();
4 config.LoadByPrimaryKey(1);
5 //config.TestString = TextBox1.Text;
6 config.TestString = "";
7 //config.TestString = null;
8
9 config.Save();
10 }
 
Setting the property to "" results in a "" in the database, not a NULL. Setting it to null puts a NULL in the database.

 Any chance this is bug?

 

 

Top 10 Contributor
Posts 3,881
Yes, we should be converting an empty string to null, we will have to look into this, seems strange this could have been broken. Are you guys using Stored Proc's or DynamicSQL?  It seems as though this was accidentally broken in some previous release, not to refix it would cause problems. We can make this a checkbox in 2010 ...

EntitySpaces | Twitter | BLOG | Please honor our Software License

Top 50 Contributor
Posts 47

I am using dynamic SQL

Let me know if there's anything I can do to help --- The workaround for this would be painful.

Top 50 Contributor
Posts 47
I have a site that is using 2009.1.126.0 (don't have access to the code right now) but I do see NULLs in the database -- meaning I think it was working "correctly" in that version.  HTH
Top 10 Contributor
Posts 3,881
Okay, I will talk to the team, your are correct, it used to work like that, I broke it down the line somewhere, we can easily fix this with a checkbox on the Generated master, that way folks can have it both ways, I agree though, having it save emptry strings is a real pain and should not be the default way it should work, otherwise you would have to constantly check for that and who in reality wants to store empty strings in a database. In my long carrer I have never worked on application that wanted to store blank strings rather than null.

EntitySpaces | Twitter | BLOG | Please honor our Software License

Top 50 Contributor
Posts 47

This is nuts but I just downgraded to 2009.1.126.0 and it still doesn't work.  I've tried 3.5 and 2.0 version dlls (BTW, what difference does that make?)

 The main difference is before I was developing on XP and now I'm on VISTA but I don't see how that would make a difference.

 

 

 

Top 10 Contributor
Posts 3,881

We will be going through our Subversion history and will let you know when this was broken, but we are going to fix it, it's an easy fix and we are very greatful you have found it.

 

EntitySpaces | Twitter | BLOG | Please honor our Software License

Top 50 Contributor
Posts 47

I'd make sure it's really a bug and not something stupid I'm doing before you thank me.

The fact that I can make it happen in one environment and not in another makes me wonder if it's me.

Top 10 Contributor
Posts 3,881
No, it's a bug, there is no way given the current code it can work, and it was broken in prior versions too, it's just now been caught, I am pretty sure it was broken in the first 2009 release as well. The bug has already been fixed and will be in the Sept 28th release. I have been programming for 25+ years and never worked on a single application that wanted to store an empty string, if folks need to they will have to call the low level SetColumn(). We broke this and didn't realize it when this particular post was first posted, but it has been fixed now.

EntitySpaces | Twitter | BLOG | Please honor our Software License

Page 1 of 2 (25 items) 1 2 Next > | RSS
Copyright © 2005 - 2009, EntitySpaces, LLC