Wednesday, March 9, 2011

empty string -- ASP.NET textbox

When I work with TextBox in C# ASP.NET, empty string in TextBox cause problems when it is an input to the sql queries. Because TextBox.Text does not return null value while the textbox is empty.

Empty text is converted to null value in default configuration as I use "SqlDataSource" connection string. But when I need to tackle this manually, here is a handy way:

TextBox tb;
String value = String.IsNullOrEmpty(tb.Text) ? null:tb.Text;

value is the new input to the database.

No comments:

Post a Comment