August 27, 2004

ToTitleCase issue with ASP.NET

When I store data in my databases, I try to store everything in uppercase for ease of manipulation and speed in searching. However, when I display that data, I usually want it in title case (or proper case) because it's easier for humans to read.

In Windows Forms applications, I have a helper function that takes care of this for me by calling an insanely long shared function in the .NET Framework.

Public Function FormatTitleCase(ByVal s As String) As String

Return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s)
End Function
This function works beautifully. If I pass in "FUBAR", it returns "Fubar." However, in an ASP.NET application, it doesn't. It returns "FUBAR". Something is FUBAR here.

After talking to Scott McNairy, my ISV buddy at Microsoft, he made a small change that made it work in both WinForms and ASP.NET applications.

Public Function FormatTitleCase(ByVal s As String) As String

Return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.ToLower(System.Globalization.CultureInfo.InvariantCulture))
End Function
Now I'm really glad I made a helper function. Typing all that in every time to get a title case string...I shudder to think about the blisters.

1 comment:

Anonymous said...

thank god. i thought i was going nuts.