views:

317

answers:

5

I have a table in the database that store 4 category and the structure of the table is ID (GUID), description. I load the category into a dropdown list (asp.net webform) to allow people to select a category and based on what they selected. I'll then display info associated with their selection and hide the others.

Currently, i do a "select case" based on the GUID that i hard coded in code behind to display the associated info. Is there a better way to do this without hard code in GUID on the code behind?

+4  A: 

What is the Data that's associated with the Guid/Description...

The data you've hardcoded sound's like a candidate for being added to the database itself.

If it's one piece of information per Category/Guid, then consider extending your Database Table to store that info to.

If it's multiple piece of information per Category/Guid, then consider creating a new Table With a CategoryID on it, and a foreign key relationship between your Category Table and your ExtraInfo table

Eoin Campbell
The problem is not the table. The guid is the primary key in the table and the description is the category's name. I want to determine which category that user select based on the category id (GUID) then show the hidden panel based on what user selected. The hard code part is the category id where i can determine which panel should display. Does that make sense?
Jack
So you have 6 GUIDs/Categories in your DB Table... and you have 6 seperate panels on your ASP.NET Page? well you could do 1 of 2 things, you could either add the panel id's to your database table, or name your panels in such away that it's easy to convert the category name into the panel ID. e.g. Category="The First Category" / PanelID = "pnlTheFirstCategory" - That way you can programatically strip the spaces out of the categoryName, search for the panel and make it visible... or something
Eoin Campbell
+1  A: 

You could query the database for the GUIDs when the app starts and cache them in a static Dictionary.

Ryan Emerle
+1  A: 

you could store the GUID in your web-config and load it at run time. then, you can easily replace that GUID with another w/o having to recompile.

Muad'Dib
Is there no way to avoid hard coding? Your suggestion is the same as my solution expect you put it on the web.config. I guess the up side is you only have to edit in one place. Is there no way to do it without hard coding those category GUID?
Jack
not without keeping it elsewhere, as many of these others suggest.
Muad'Dib
A: 

You should have a Categories table and a Posts table (or whatever it is that you will tag with your categories). In the Posts table you have a column for the CategoryID (assuming each post can only belong to one category), so that you only have the category name in one place (normalize your data).

When you render the dropdownlist, you select the GUID:s from the database. No hard coding, and if you add another category (or remove one) the dropdownlist will automatically reflect the available categories.

Tomas Lycken
A: 

If you bind the dropdown list to the Category row or a Tuple that contains the category name and the value you can load the Guid in your codebehind using the SelectedValue property. You will then set the DataTextField and DataValueField on the Dropdownlist.

bendewey