DropDownList Lookups Failing Due to SQL Char DataType

Here’s today’s episode of “Losing Time on Something Dumb”…

In my ASP.NET 3.5 project (using Telerik’s AJAX controls) I filled a RadComboBox with Items and needed to set the selected item to the current value of the object (I’m using Entity Framework).

For the life of me, I couldn’t get this to work:

ddlCountryOfOrigin.SelectedValue = itm.Origin

The items were there in the RadComboBox, and there was definitely a value in itm.Origin. No matter what I did, the current value wasn’t selected.

I neglected to look closely enough. I eventually discovered that the lookup value was “UK “ instead of “UK” (the first has a trailing space). Obviously,  “UK” doesn’t match “UK “ in computer logic.

How was that jeezely space getting in there? Definitely not from the existing RadComboBox values. Well, it was SQL Server killing me again. I’m pretty lame when it comes to database stuff, but I can do research. It turned out that the designer of the table schema chose Char(3) for the Origin field. While I was inserting “UK”, the database was padding a space without asking.

The immediate fix was to trim the space before using the value for a lookup. At some point, the new DBA will probably change the definition.

Sheesh. Am I the only one who loses time on these things??? Oh well, maybe you won’t.

Ken

No Comments