Empty strings to null: A declarative approach
-
Are there any existing mechanisms to achieve this, or do I need to roll my own? I have a small subset of text fields that cause problems when I assign an empty string from the UI to a field, which should be null if not specified.
-
Are there any existing mechanisms to achieve this, or do I need to roll my own? I have a small subset of text fields that cause problems when I assign an empty string from the UI to a field, which should be null if not specified.
-
Well I agree that this is a design question but since it is so specific we probably need to know the platform and language you are working with, yes?
Abandon hope all ye who answer here.
Pete O'Hanlon - the General Discussions forum
Sorry, that was quite an oversight. ASP.NET, in C#. I'm not specifically asking how to do this, just if it is done already by others, and how they do it. Input regarding other areas I would also appreciate.
-
Sorry, that was quite an oversight. ASP.NET, in C#. I'm not specifically asking how to do this, just if it is done already by others, and how they do it. Input regarding other areas I would also appreciate.
Brady Kelly wrote:
when I assign an empty string from the UI to a field
So this field is a Database column I guess? I imagine many people today are using Object Relation Mapping on the .NET platform and the tools and/or libraries/frameworks handle that stuff for them. Have you looked at the Castle project, specifically ActiveRecord which is built on NHibernate? Also if you are using 3.5 I have seen lots of positive noise about Linq.
-
Brady Kelly wrote:
when I assign an empty string from the UI to a field
So this field is a Database column I guess? I imagine many people today are using Object Relation Mapping on the .NET platform and the tools and/or libraries/frameworks handle that stuff for them. Have you looked at the Castle project, specifically ActiveRecord which is built on NHibernate? Also if you are using 3.5 I have seen lots of positive noise about Linq.
Thanks Mike, I have planned to look at Linq, as I'm giving the other devs an intro to it next month.
-
Are there any existing mechanisms to achieve this, or do I need to roll my own? I have a small subset of text fields that cause problems when I assign an empty string from the UI to a field, which should be null if not specified.
I've just found one way of doing this with standard ASP.NET. The SqlDataSource Parameter elements all support a ConvertEmptyStringToNull attribute.
-
I've just found one way of doing this with standard ASP.NET. The SqlDataSource Parameter elements all support a ConvertEmptyStringToNull attribute.
Brady - If you are reading things in with a datareader, then one way would be to use some code to convert your DB null string to a null string. A simple function would cope with this:
public string GetString(IDataReader item, string column)
{
int ordinal = item.GetOrdinal(column);
if (item.IsDBNull(ordinal))
return null;
return item.GetString(ordinal);
}Deja View - the feeling that you've seen this post before.