Currently, whenever I pass a string to the db, if that string is empty, then i set that object to NULL by doing the following:
IIf(DescriptionTxt.Text.length > 0, DescriptionTxt.Text, DBNull.Value)
I was thinking about writing a function to reduce the length of this code, and make it more consistent.
However, I was wondering, is there already a way of doing this in .NET 3.5?
You could write an extension method:
Then in code just use
But otherwise there is nothing in .NET that does it by default. In fact, most developers that I know prefer to make a distinction between a NULL and an empty string. They are different in .NET, so it’s a lot easier (and consistent) if they are different in the DB as well.
Added: OOps just noticed this is a VB.NET question. Sorry, my VB.NET is a bit rusty, but I’m sure you will be able to translate the simple function above to VB.NET.