etlds
etlds

Reputation: 5890

DropDownList number or text?

Our projects user a lot dropdownlist control. My question is what should we store in the database for them, the order number or text itself.

For example:

<option value="1">This is the first option</option>
<option value="2">This is the second option</option>

Convert the "1" and "2" to smallint 1, 2 to the database. --OR--

<option value="This is the first option">This is the first option</option>
<option value="This is the second option">This is the second option</option>

use varchar data type in the field.

order number is good for the statistics, but Text is dynamic for modification, for example insert another option between 1 and 2 and do not have to worry about the order. Also if it is embedded in the GridView, we can simply pull the text rather than put an DropDownList control in the GridView in order to see the text.

Upvotes: 0

Views: 102

Answers (4)

Darian Lewin
Darian Lewin

Reputation: 182

I suggest you create a table with both the order number and the text, with the order number as a primary key which "autoincrement".

If you, for example, need "This is the first option" in another table, use the primary key of the first tabel as a "foreign key" in the second one. That way, you can just change the text value without having to change any other table.

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

If you don't want to load the drop down lists from database tables then here is what I would do. Use the integer values in the drop down lists. Then what you're going to want to do is create tables in the database that mimic those values because that will give you the ability to build foreign keys on the tables and perform reporting that makes sense as well as statistics (like you talked about).

Finally, you may want to create Enum's that match the values because that will make them easier to work with internally in the program. For example:

public enum MyLookupTable
{
    Value1 = 1,
    Value2 = 2,
    etc...
}

Now, what I just described is only one step from not having to maintain the drop downs because you just need to load them from the database instead, which you can find a good example of that here.

Upvotes: 1

RollRoll
RollRoll

Reputation: 8472

if the will be only two options I would create a IsFirst boolean column in the database and persist from what was selected. False for the second option True for the first

Upvotes: 0

Michal Klouda
Michal Klouda

Reputation: 14521

Use some kind of identificator. Definitely go for a number, but not just order number. As you say, you might add another options in the future, or you might want to rearrange existing values.

Upvotes: 0

Related Questions